Releasing old buffer if realloc fails

This commit is contained in:
cardigliano
2019-01-31 09:12:04 +01:00
parent 6a98aec2ca
commit 2ec41c1732
+4 -2
View File
@@ -2763,7 +2763,7 @@ int ndpi_handle_rule(struct ndpi_detection_module_struct *ndpi_mod,
*/ */
int ndpi_load_protocols_file(struct ndpi_detection_module_struct *ndpi_mod, char* path) { int ndpi_load_protocols_file(struct ndpi_detection_module_struct *ndpi_mod, char* path) {
FILE *fd; FILE *fd;
char *buffer; char *buffer, *old_buffer;
int chunk_len = 512, buffer_len = chunk_len, old_buffer_len; int chunk_len = 512, buffer_len = chunk_len, old_buffer_len;
int i, rc = -1; int i, rc = -1;
@@ -2787,13 +2787,15 @@ int ndpi_load_protocols_file(struct ndpi_detection_module_struct *ndpi_mod, char
while((line = fgets(line, line_len, fd)) != NULL && line[strlen(line)-1] != '\n') { while((line = fgets(line, line_len, fd)) != NULL && line[strlen(line)-1] != '\n') {
i = strlen(line); i = strlen(line);
old_buffer = buffer;
old_buffer_len = buffer_len; old_buffer_len = buffer_len;
buffer_len += chunk_len; buffer_len += chunk_len;
buffer = ndpi_realloc(buffer, old_buffer_len, buffer_len); buffer = ndpi_realloc(old_buffer, old_buffer_len, buffer_len);
if(buffer == NULL) { if(buffer == NULL) {
NDPI_LOG_ERR(ndpi_mod, "Memory allocation failure"); NDPI_LOG_ERR(ndpi_mod, "Memory allocation failure");
free(old_buffer);
goto close_fd; goto close_fd;
} }