diff options
Diffstat (limited to 'plugins/check_http.c')
-rw-r--r-- | plugins/check_http.c | 21 |
1 files changed, 14 insertions, 7 deletions
diff --git a/plugins/check_http.c b/plugins/check_http.c index a9c22389..8dda046f 100644 --- a/plugins/check_http.c +++ b/plugins/check_http.c | |||
@@ -1399,7 +1399,6 @@ char *unchunk_content(const char *content) { | |||
1399 | char *endptr; | 1399 | char *endptr; |
1400 | long length_of_chunk = 0; | 1400 | long length_of_chunk = 0; |
1401 | size_t overall_size = 0; | 1401 | size_t overall_size = 0; |
1402 | char *result_ptr; | ||
1403 | 1402 | ||
1404 | while (true) { | 1403 | while (true) { |
1405 | size_of_chunk = strtol(pointer, &endptr, 16); | 1404 | size_of_chunk = strtol(pointer, &endptr, 16); |
@@ -1439,29 +1438,37 @@ char *unchunk_content(const char *content) { | |||
1439 | overall_size += length_of_chunk; | 1438 | overall_size += length_of_chunk; |
1440 | 1439 | ||
1441 | if (result == NULL) { | 1440 | if (result == NULL) { |
1442 | result = (char *)calloc(length_of_chunk, sizeof(char)); | 1441 | // Size of the chunk plus the ending NULL byte |
1442 | result = (char *)malloc(length_of_chunk +1); | ||
1443 | if (result == NULL) { | 1443 | if (result == NULL) { |
1444 | if (verbose) { | 1444 | if (verbose) { |
1445 | printf("Failed to allocate memory for unchunked body\n"); | 1445 | printf("Failed to allocate memory for unchunked body\n"); |
1446 | } | 1446 | } |
1447 | return NULL; | 1447 | return NULL; |
1448 | } | 1448 | } |
1449 | result_ptr = result; | ||
1450 | } else { | 1449 | } else { |
1451 | void *tmp = realloc(result, overall_size); | 1450 | // Enlarge memory to the new size plus the ending NULL byte |
1451 | void *tmp = realloc(result, overall_size +1); | ||
1452 | if (tmp == NULL) { | 1452 | if (tmp == NULL) { |
1453 | if (verbose) { | 1453 | if (verbose) { |
1454 | printf("Failed to allocate memory for unchunked body\n"); | 1454 | printf("Failed to allocate memory for unchunked body\n"); |
1455 | } | 1455 | } |
1456 | return NULL; | 1456 | return NULL; |
1457 | } else { | ||
1458 | result = tmp; | ||
1457 | } | 1459 | } |
1458 | } | 1460 | } |
1459 | 1461 | ||
1460 | memcpy(result_ptr, start_of_chunk, size_of_chunk); | 1462 | memcpy(result + (overall_size - size_of_chunk), start_of_chunk, size_of_chunk); |
1461 | result_ptr = result_ptr + size_of_chunk; | ||
1462 | } | 1463 | } |
1463 | 1464 | ||
1464 | result[overall_size] = '\0'; | 1465 | if (overall_size == 0 && result == NULL) { |
1466 | // We might just have received the end chunk without previous content, so result is never allocated | ||
1467 | result = calloc(1, sizeof(char)); | ||
1468 | // No error handling here, we can only return NULL anyway | ||
1469 | } else { | ||
1470 | result[overall_size] = '\0'; | ||
1471 | } | ||
1465 | return result; | 1472 | return result; |
1466 | } | 1473 | } |
1467 | 1474 | ||