Skip to content

Commit b6f94d4

Browse files
committed
Use std::string in function scope variables instead of strdup().
Replace malloc()/memset(0) with calloc(). Move the allocation return value check to below the call.
1 parent fd2abea commit b6f94d4

File tree

1 file changed

+12
-28
lines changed

1 file changed

+12
-28
lines changed

src/libnml/cms/cms.cc

Lines changed: 12 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ extern "C" {
3232
#ifdef __cplusplus
3333
}
3434
#endif
35+
#include <string>
3536
#include <rtapi_string.h>
3637
#include "cms_cfg.hh"
3738
#include "cms.hh" /* class CMS */
@@ -232,8 +233,10 @@ CMS::CMS(const char *bufline_in, const char *procline_in, int set_to_server)
232233
return;
233234
}
234235

235-
char *bufline = strdup(bufline_in);
236-
char *procline = strdup(procline_in);
236+
std::string buflineString(bufline_in); // Local copies without strdup()
237+
std::string proclineString(procline_in);
238+
char *bufline = buflineString.data();
239+
char *procline = proclineString.data();
237240

238241
convert2upper(buflineupper, bufline, LINELEN);
239242
convert2upper(proclineupper, procline, LINELEN);
@@ -279,8 +282,6 @@ CMS::CMS(const char *bufline_in, const char *procline_in, int set_to_server)
279282
rcs_print_error("CMS: Error in buffer line from config file.\n");
280283
rcs_print_error("%s\n", bufline);
281284
status = CMS_CONFIG_ERROR;
282-
free(bufline);
283-
free(procline);
284285
return;
285286
}
286287

@@ -318,8 +319,6 @@ CMS::CMS(const char *bufline_in, const char *procline_in, int set_to_server)
318319
rcs_print_error("CMS: Error in buffer line from config file.\n");
319320
rcs_print_error("%s\n", bufline);
320321
status = CMS_CONFIG_ERROR;
321-
free(bufline);
322-
free(procline);
323322
return;
324323
}
325324

@@ -336,8 +335,6 @@ CMS::CMS(const char *bufline_in, const char *procline_in, int set_to_server)
336335
} else {
337336
rcs_print_error("CMS: invalid buffer type (%s)\n", buffer_type_name);
338337
status = CMS_CONFIG_ERROR;
339-
free(bufline);
340-
free(procline);
341338
return;
342339
}
343340

@@ -346,8 +343,6 @@ CMS::CMS(const char *bufline_in, const char *procline_in, int set_to_server)
346343
rcs_print_error("CMS: Error in buffer line from config file.\n");
347344
rcs_print_error("%s\n", bufline);
348345
status = CMS_CONFIG_ERROR;
349-
free(bufline);
350-
free(procline);
351346
return;
352347
}
353348
for (i = 8; i < num_words && i < 32; i++) {
@@ -447,8 +442,6 @@ CMS::CMS(const char *bufline_in, const char *procline_in, int set_to_server)
447442
("CMS: Error parsing process line from config file.\n");
448443
rcs_print_error("%s\n", procline);
449444
status = CMS_CONFIG_ERROR;
450-
free(bufline);
451-
free(procline);
452445
return;
453446
}
454447
} else {
@@ -457,8 +450,6 @@ CMS::CMS(const char *bufline_in, const char *procline_in, int set_to_server)
457450
("CMS: Error parsing process line from config file.\n");
458451
rcs_print_error("%s\n", procline);
459452
status = CMS_CONFIG_ERROR;
460-
free(bufline);
461-
free(procline);
462453
return;
463454
}
464455
}
@@ -499,8 +490,6 @@ CMS::CMS(const char *bufline_in, const char *procline_in, int set_to_server)
499490
("CMS: connection number(%lu) must be less than total connections (%lu).\n",
500491
connection_number, total_connections);
501492
status = CMS_CONFIG_ERROR;
502-
free(bufline);
503-
free(procline);
504493
return;
505494
}
506495
}
@@ -509,8 +498,6 @@ CMS::CMS(const char *bufline_in, const char *procline_in, int set_to_server)
509498
rcs_print_error("CMS: Error in proc line from config file.\n");
510499
rcs_print_error("%s\n", procline);
511500
status = CMS_CONFIG_ERROR;
512-
free(bufline);
513-
free(procline);
514501
return;
515502
}
516503

@@ -607,8 +594,6 @@ CMS::CMS(const char *bufline_in, const char *procline_in, int set_to_server)
607594
if (enable_diagnostics) {
608595
setup_diag_proc_info();
609596
}
610-
free(bufline);
611-
free(procline);
612597
}
613598

614599
/* Function for allocating memory and initializing XDR streams, which */
@@ -652,20 +637,19 @@ void CMS::open(void)
652637
/* Save some memory and time if this is a PHANTOMMEM object. */
653638
if (!is_phantom) {
654639
/* Allocate memory for the local copy of global buffer. */
655-
data = malloc(size);
656-
memset(data, 0, size);
657-
subdiv_data = data;
658-
if (force_raw) {
659-
encoded_data = data;
660-
}
661-
rcs_print_debug(PRINT_CMS_CONSTRUCTORS, "%p = data = calloc(%lu,1);\n",
662-
data, size);
640+
data = calloc(size, 1);
663641
/* Check to see if allocating memory was successful. */
664642
if (data == NULL) {
665643
rcs_print_error("CMS: Can't allocate memory for local buffer.\n");
666644
status = CMS_CREATE_ERROR;
667645
return;
668646
}
647+
subdiv_data = data;
648+
if (force_raw) {
649+
encoded_data = data;
650+
}
651+
rcs_print_debug(PRINT_CMS_CONSTRUCTORS, "%p = data = calloc(%lu,1);\n",
652+
data, size);
669653
}
670654
if (isserver || neutral || ((ProcessType == CMS_REMOTE_TYPE) && !force_raw)) {
671655
switch (neutral_encoding_method) {

0 commit comments

Comments
 (0)