... | ... |
@@ -27,6 +27,7 @@ char* parse_first_line(char* buffer, unsigned int len, struct msg_start * fl) |
27 | 27 |
char* third; |
28 | 28 |
char* nl; |
29 | 29 |
int offset; |
30 |
+ char* end; |
|
30 | 31 |
|
31 | 32 |
/* grammar: |
32 | 33 |
request = method SP uri SP version CRLF |
... | ... |
@@ -35,9 +36,10 @@ char* parse_first_line(char* buffer, unsigned int len, struct msg_start * fl) |
35 | 36 |
*/ |
36 | 37 |
|
37 | 38 |
|
39 |
+ end=buffer+len; |
|
38 | 40 |
/* see if it's a reply (status) */ |
39 | 41 |
tmp=eat_token(buffer, len); |
40 |
- if (tmp==buffer){ |
|
42 |
+ if ((tmp==buffer)||(tmp>=end)){ |
|
41 | 43 |
DPrint("ERROR: empty or bad first line\n"); |
42 | 44 |
goto error1; |
43 | 45 |
} |
... | ... |
@@ -52,7 +54,7 @@ char* parse_first_line(char* buffer, unsigned int len, struct msg_start * fl) |
52 | 54 |
offset=tmp-buffer; |
53 | 55 |
second=eat_space(tmp, len-offset); |
54 | 56 |
offset+=second-tmp; |
55 |
- if (second==tmp){ |
|
57 |
+ if ((second==tmp)||(tmp>=end)){ |
|
56 | 58 |
goto error; |
57 | 59 |
} |
58 | 60 |
*tmp=0; /* mark the end of the token */ |
... | ... |
@@ -60,24 +62,41 @@ char* parse_first_line(char* buffer, unsigned int len, struct msg_start * fl) |
60 | 62 |
|
61 | 63 |
/* next element */ |
62 | 64 |
tmp=eat_token(second, len-offset); |
65 |
+ if (tmp>=end){ |
|
66 |
+ goto error; |
|
67 |
+ } |
|
63 | 68 |
offset+=tmp-second; |
64 | 69 |
third=eat_space(tmp, len-offset); |
65 | 70 |
offset+=third-tmp; |
66 |
- if(third==tmp){ |
|
71 |
+ if ((third==tmp)||(tmp>=end)){ |
|
67 | 72 |
goto error; |
68 | 73 |
} |
69 | 74 |
*tmp=0; /* mark the end of the token */ |
70 | 75 |
fl->u.request.uri=second; |
71 |
- /* last part */ |
|
72 |
- tmp=eat_token(third,len-offset); |
|
73 |
- offset+=tmp-third; |
|
74 |
- if (tmp==third){ |
|
75 |
- goto error; |
|
76 |
+ /* last part: for a request it must be the version, for a reply |
|
77 |
+ * it can contain almost anything, including spaces, so we don't care |
|
78 |
+ * about it*/ |
|
79 |
+ if (fl->type==SIP_REQUEST){ |
|
80 |
+ tmp=eat_token(third,len-offset); |
|
81 |
+ offset+=tmp-third; |
|
82 |
+ if ((tmp==third)||(tmp>=end)){ |
|
83 |
+ goto error; |
|
84 |
+ } |
|
85 |
+ if (! is_empty(tmp, len-offset)){ |
|
86 |
+ goto error; |
|
87 |
+ } |
|
88 |
+ }else{ |
|
89 |
+ tmp=eat_token2(third,len-offset,'\r'); /* find end of line |
|
90 |
+ ('\n' or '\r') */ |
|
91 |
+ if (tmp>=end){ /* no crlf in packet => invalid */ |
|
92 |
+ goto error; |
|
93 |
+ } |
|
94 |
+ offset+=tmp-third; |
|
76 | 95 |
} |
77 |
- if (! is_empty(tmp, len-offset)){ |
|
96 |
+ nl=eat_line(tmp,len-offset); |
|
97 |
+ if (nl>=end){ /* no crlf in packet or only 1 line > invalid */ |
|
78 | 98 |
goto error; |
79 | 99 |
} |
80 |
- nl=eat_line(tmp,len-offset); |
|
81 | 100 |
*tmp=0; |
82 | 101 |
fl->u.request.version=third; |
83 | 102 |
|
... | ... |
@@ -238,9 +257,9 @@ char* parse_via_body(char* buffer,unsigned int len, struct via_body * vb) |
238 | 257 |
switch (*tmp){ |
239 | 258 |
case ' ': |
240 | 259 |
*tmp=0; |
260 |
+ tmp++; |
|
241 | 261 |
/*the rest is comment? */ |
242 |
- if (tmp+1-buffer<len){ |
|
243 |
- tmp++; |
|
262 |
+ if (tmp-buffer<len){ |
|
244 | 263 |
comment=tmp; |
245 | 264 |
/* eat the comment */ |
246 | 265 |
for(;((tmp-buffer)<len)&& |
... | ... |
@@ -258,8 +277,8 @@ char* parse_via_body(char* buffer,unsigned int len, struct via_body * vb) |
258 | 277 |
|
259 | 278 |
case ';': |
260 | 279 |
*tmp=0; |
261 |
- if (tmp+1-buffer>=len) goto error; |
|
262 | 280 |
tmp++; |
281 |
+ if (tmp-buffer>=len) goto error; |
|
263 | 282 |
params=tmp; |
264 | 283 |
/* eat till end, first space or ',' */ |
265 | 284 |
for(;((tmp-buffer)<len)&& |
... | ... |
@@ -283,10 +302,10 @@ char* parse_via_body(char* buffer,unsigned int len, struct via_body * vb) |
283 | 302 |
|
284 | 303 |
case ',': |
285 | 304 |
*tmp=0; |
286 |
- if (tmp+1-buffer<len){ |
|
305 |
+ tmp++; |
|
306 |
+ if (tmp-buffer<len){ |
|
287 | 307 |
/* eat space and ',' */ |
288 |
- for(tmp=tmp+1; |
|
289 |
- ((tmp-buffer)<len)&& |
|
308 |
+ for(;((tmp-buffer)<len)&& |
|
290 | 309 |
(*tmp==' '|| *tmp==','); |
291 | 310 |
tmp++); |
292 | 311 |
} |
... | ... |
@@ -425,7 +444,7 @@ skip: |
425 | 444 |
if (second_via) { |
426 | 445 |
tmp=parse_via_body(second_via, strlen(second_via), &vb2); |
427 | 446 |
if (vb2.error!=VIA_PARSE_OK){ |
428 |
- DPrint("ERROR: parsing via body: %s\n", second_via); |
|
447 |
+ DPrint("ERROR: parsing via2 body: %s\n", second_via); |
|
429 | 448 |
goto error; |
430 | 449 |
} |
431 | 450 |
vb2.size=tmp-second_via; |
... | ... |
@@ -7,7 +7,7 @@ |
7 | 7 |
|
8 | 8 |
#include "parser_f.h" |
9 | 9 |
|
10 |
-/* returns pointer to next line or end of buffer */ |
|
10 |
+/* returns pointer to next line or after the end of buffer */ |
|
11 | 11 |
char* eat_line(char* buffer, unsigned int len) |
12 | 12 |
{ |
13 | 13 |
char* nl; |
... | ... |
@@ -24,7 +24,7 @@ char* eat_line(char* buffer, unsigned int len) |
24 | 24 |
|
25 | 25 |
|
26 | 26 |
|
27 |
-/* returns pointer to first non white char or to the end of the buffer */ |
|
27 |
+/* returns pointer to first non white char or after the end of the buffer */ |
|
28 | 28 |
char* eat_space(char* buffer, unsigned int len) |
29 | 29 |
{ |
30 | 30 |
char* p; |
... | ... |
@@ -21,7 +21,7 @@ int receive_msg(char* buf, unsigned int len) |
21 | 21 |
orig=(char*) malloc(len); |
22 | 22 |
if (orig==0){ |
23 | 23 |
DPrint("ERROR: memory allocation failure\n"); |
24 |
- goto error; |
|
24 |
+ goto error1; |
|
25 | 25 |
} |
26 | 26 |
memcpy(orig, buf, len); |
27 | 27 |
|
... | ... |
@@ -74,7 +74,8 @@ skip: |
74 | 74 |
free(orig); |
75 | 75 |
return 0; |
76 | 76 |
error: |
77 |
+ free(orig); |
|
78 |
+error1: |
|
77 | 79 |
return -1; |
78 |
- |
|
79 | 80 |
} |
80 | 81 |
|