Skip to content

Commit ac484e5

Browse files
committed
Fixed client crashing if write an invalid command
1 parent 5d915cd commit ac484e5

File tree

2 files changed

+35
-13
lines changed

2 files changed

+35
-13
lines changed

client.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,12 @@ int main(int argc , char *argv[])
122122
{
123123
bzero(buffer , BUFFER_SIZE); // Clear buffer.
124124
read(sockfd , buffer , BUFFER_SIZE);
125-
126-
printf("%s",buffer);
127-
if(streaming == 0)
128-
break;
125+
if(strstr(buffer, "Not subscribed to any channels.") != NULL)
126+
{
127+
if(strstr(buffer, "IGNORE") == NULL)
128+
printf("%s",buffer);
129+
streaming = 0;
130+
}
129131

130132
char* msg = "​​";
131133
write(sockfd , msg , strlen(msg));
@@ -136,7 +138,8 @@ int main(int argc , char *argv[])
136138
bzero(buffer , BUFFER_SIZE);
137139
n = read(sockfd , buffer , BUFFER_SIZE);
138140
if(n < 0) break;
139-
printf("%s",buffer);
141+
if(strstr(buffer, "IGNORE") == NULL)
142+
printf("%s",buffer);
140143

141144
}
142145
close(sockfd);

server.c

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ struct Channel
146146
int curSubPos; // Point from where you can read future msgs.
147147
int curMsgReadPos; // Cur point of reading.
148148
int totalMsgs; // Total msgs in channel (readable and not).
149-
struct Message msgs[256];
149+
struct Message msgs[1000];
150150
};
151151

152152

@@ -300,7 +300,7 @@ char* Unsub(char buffer[BUFFER_SIZE]) // Unsubscribes client from specified chan
300300
{
301301
char* id = GetIdFromBuffer(buffer, 5, 4);
302302
if(ValidID(id) < 0) // Invalid id.
303-
return replace_str("Invalid channel: xxx.\n","xxx",id);
303+
return replace_str("Invalid channel: xxx\n","xxx",id);
304304

305305
int i = GetClientSubIndex(id);
306306
if(i < 0) // Not subscribed to this chan id.
@@ -332,7 +332,7 @@ char* Sub(char buffer[BUFFER_SIZE])
332332
{
333333
char* id = GetIdFromBuffer(buffer, 3, 4);
334334
if(ValidID(id) < 0) // Return if invalid id.
335-
return replace_str("Invalid channel: xxx.\n","xxx",id);
335+
return replace_str("Invalid channel: xxx\n","xxx",id);
336336

337337
if(GetClientSubIndex(id) >= 0)
338338
return replace_str("Already subscribed to channel xxx.\n","xxx",id);
@@ -495,9 +495,11 @@ char* Next(char* buffer)
495495
*/
496496
char* NextAll()
497497
{
498-
if(client.curChanPos == 0) // If not return min length, no subs.
498+
if(client.curChanPos == 0) // If not return min length, no subs.
499+
{
500+
streaming = 0;
499501
return "Not subscribed to any channels.\n";
500-
502+
}
501503
int nextChanID = 99999;
502504
long long nextMsgSentTime = 9999999999999;
503505

@@ -536,6 +538,14 @@ char* NextAll()
536538
*/
537539
void LiveStream(int newsockfd, char buffer[BUFFER_SIZE])
538540
{
541+
if(client.curChanPos == 0) // If not return min length, no subs.
542+
{
543+
544+
streaming = 0;
545+
WriteClient(newsockfd, "Not subscribed to any channels.\n");
546+
return;
547+
}
548+
539549
streaming = 1;
540550
char* newBuffer = calloc(BUFFER_SIZE, sizeof(char));
541551

@@ -544,6 +554,7 @@ void LiveStream(int newsockfd, char buffer[BUFFER_SIZE])
544554

545555
while(streaming == 1)
546556
{
557+
printf(")))\n");
547558
char* msg = calloc(BUFFER_SIZE, sizeof(char));
548559

549560
if(strlen(newBuffer) > 0) { // Livestream msgs for specified channel.
@@ -556,9 +567,12 @@ void LiveStream(int newsockfd, char buffer[BUFFER_SIZE])
556567
if(strlen(msg) > 0)
557568
WriteClient(newsockfd, msg);
558569
}
570+
if(streaming == 0)
571+
break;
559572
if(strlen(msg) <= 0) { // Livestream nothing if no msg.
560573
WriteClient(newsockfd, "​");
561574
}
575+
562576

563577
bzero(buffer , BUFFER_SIZE);
564578
read(newsockfd , buffer , BUFFER_SIZE);
@@ -657,6 +671,7 @@ int main(int argc, char * argv[])
657671
Error("ERROR, fork failed.");
658672
}
659673
if(pid == 0)
674+
{
660675
int id_index = *shm_cliForks;
661676
*shm_cliForks += 1;
662677

@@ -698,10 +713,14 @@ int main(int argc, char * argv[])
698713
msg = Next(bufferg);
699714
else
700715
msg = NextAll();
701-
else
702-
msg = "";
703-
if(strstr(bufferg, "LIVESTREAM") != NULL)
716+
else if(strstr(bufferg, "LIVESTREAM") != NULL)
717+
{
704718
LiveStream(newsockfd, bufferg);
719+
msg = "IGNORE";
720+
}
721+
722+
else
723+
msg = "IGNORE";
705724

706725
int n = WriteClient(newsockfd, str_append("​", msg));
707726
if(n < 0) // Write fail, try reconnect.

0 commit comments

Comments
 (0)