summaryrefslogtreecommitdiffstats
path: root/plugins
diff options
context:
space:
mode:
authorLorenz <12514511+RincewindsHat@users.noreply.github.com>2023-01-31 11:18:14 (GMT)
committerGitHub <noreply@github.com>2023-01-31 11:18:14 (GMT)
commit2ad962c13473607ca0d974bfaa516a0ed53ec73d (patch)
treed7072386978c9f1e70cd7119643c24fd087d69e0 /plugins
parent67b472f9d1d29b9de5312c46f13ca4f5a656c4da (diff)
parentd3fbcd122012af7733de3b80a692f79ad69057b2 (diff)
downloadmonitoring-plugins-2ad962c13473607ca0d974bfaa516a0ed53ec73d.tar.gz
Merge pull request #1830 from monitoring-plugins/fix_1829
check_http: Fix memory reallocation error in chunk decoding logic
Diffstat (limited to 'plugins')
-rw-r--r--plugins/check_http.c13
1 files changed, 7 insertions, 6 deletions
diff --git a/plugins/check_http.c b/plugins/check_http.c
index a9c2238..5fa310f 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,26 +1438,28 @@ 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 result[overall_size] = '\0';