| 225 | |
| 226 | |
| 227 | char* parse_content_length( char* buffer, char* end, int* length) |
| 228 | { |
| 229 | int number; |
| 230 | char *p, *numstart; |
| 231 | |
| 232 | p = buffer; |
| 233 | /* search the beginning of the number */ |
| 234 | while ( p<end && (*p==' ' || *p=='\t' |
| 235 | || (*p=='\r' && p+1<end && *(p+1)=='\n') |
| 236 | || (*p=='\n' && p+1<end && (*(p+1)==' '||*(p+1)=='\t')) )) |
| 237 | p++; |
| 238 | if (p==end) |
| 239 | goto error; |
| 240 | /* parse the number */ |
| 241 | numstart = p; |
| 242 | number = 0; |
| 243 | while (p<end && *p>='0' && *p<='9') { |
| 244 | /* do not actually cause an integer overflow, as it is UB! --liviu */ |
| 245 | if (number >= INT_MAX/10) { |
| 246 | LM_ERR("integer overflow risk at pos %d in length value [%.*s]\n", |
| 247 | (int)(p-buffer),(int)(end-buffer), buffer); |
| 248 | return NULL; |
| 249 | } |
| 250 | |
| 251 | number = number*10 + ((*p)-'0'); |
| 252 | p++; |
| 253 | } |
| 254 | if (p==end || p==numstart) |
| 255 | goto error; |
| 256 | |
| 257 | /* now we should have only spaces at the end */ |
| 258 | while ( p<end && (*p==' ' || *p=='\t' |
| 259 | || (*p=='\n' && p+1<end && (*(p+1)==' '||*(p+1)=='\t')) )) |
| 260 | p++; |
| 261 | if (p==end) |
| 262 | goto error; |
| 263 | /* the header ends proper? */ |
| 264 | if ( (*(p++)!='\n') && (*(p-1)!='\r' || p==end || *(p++)!='\n' ) ) |
| 265 | goto error; |
| 266 | |
| 267 | *length = number; |
| 268 | return p; |
| 269 | error: |
| 270 | LM_ERR("parse error at pos %ld, dec-char: %d, start/p/end: %p/%p/%p\n", |
| 271 | (long)(p - buffer), p < end && (end-buffer) ? *p:-1, buffer, p, end); |
| 272 | return NULL; |
| 273 | } |
| 274 | |
| 275 | |
| 276 | |