Skip to content

Commit 9a51ad8

Browse files
committed
Function realloc() can fail. Don't leak memory if it does.
Fix wrong variable in NULL test (copy/paste error).
1 parent 68f432e commit 9a51ad8

1 file changed

Lines changed: 14 additions & 13 deletions

File tree

src/libnml/rcs/rcs_print.cc

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -173,20 +173,22 @@ void convert_print_list_to_lines()
173173
temp_buf = (char *) malloc(strlen(string_from_list) + 1);
174174
rtapi_strlcpy(temp_buf, string_from_list, strlen(string_from_list) + 1);
175175
} else {
176-
temp_buf = (char *) realloc(temp_buf, strlen(temp_buf)
177-
+ strlen(string_from_list) + 1);
178-
strcat(temp_buf, string_from_list);
176+
char *ra = (char *) realloc(temp_buf, strlen(temp_buf) + strlen(string_from_list) + 1);
177+
if(ra) {
178+
temp_buf = ra;
179+
strcat(temp_buf, string_from_list);
180+
}
179181
}
180182
rcs_print_list->delete_current_node();
181183
} else {
182184
if (temp_buf != NULL) {
183-
temp_buf = (char *) realloc(temp_buf, strlen(temp_buf)
184-
+ strlen(string_from_list) + 1);
185-
strcat(temp_buf, string_from_list);
186-
rcs_print_list->delete_current_node();
187-
rcs_print_list->store_after_current_node(temp_buf,
188-
strlen(temp_buf)
189-
+ 1, 1);
185+
char *ra = (char *) realloc(temp_buf, strlen(temp_buf) + strlen(string_from_list) + 1);
186+
if(ra) {
187+
temp_buf = ra;
188+
strcat(temp_buf, string_from_list);
189+
rcs_print_list->delete_current_node();
190+
rcs_print_list->store_after_current_node(temp_buf, strlen(temp_buf) + 1, 1);
191+
}
190192
free(temp_buf);
191193
temp_buf = NULL;
192194
} else if (next_line[1] != 0) {
@@ -214,9 +216,8 @@ void update_lines_table()
214216
}
215217
if (NULL != rcs_print_list) {
216218
convert_print_list_to_lines();
217-
rcs_lines_table = (char **) malloc(sizeof(char *)
218-
* rcs_print_list->list_size);
219-
if (NULL != rcs_print_list) {
219+
rcs_lines_table = (char **) malloc(sizeof(char *) * rcs_print_list->list_size);
220+
if (NULL != rcs_lines_table) {
220221
char *string_from_list;
221222
string_from_list = (char *) rcs_print_list->get_head();
222223
int i = 0;

0 commit comments

Comments
 (0)