Skip to content

Commit 0c8e9ac

Browse files
committed
Fixes more warnings for possible string truncation
This commit changes strncpy() for snprintf(), snprintf() guarantees a NULL terminating string. These places did not check the length at all, some hard-coded a NULL terminating string as the last place in the array. With this, some of the strings could potentially be without the NULL terminating character. By changing to snprintf() we run into the possibility of truncating the strings, no checks is currently in place to catch this. There is also a strncat() thrown on here, hard-coded value was changed from 1 to 2 to make room for the NULL terminating character.
1 parent cff07b7 commit 0c8e9ac

8 files changed

Lines changed: 20 additions & 25 deletions

File tree

src/emc/rs274ngc/interp_namedparams.cc

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,8 +217,7 @@ int Interp::fetch_ini_param( const char *nameBuf, int *status, double *value)
217217

218218
char capName[LINELEN];
219219

220-
strncpy(capName, nameBuf, n);
221-
capName[n] = '\0';
220+
snprintf(capName, LINELEN, "%s", nameBuf);
222221
for (char *p = capName; *p != 0; p++)
223222
*p = toupper(*p);
224223
capName[closeBracket] = '\0';

src/emc/rs274ngc/interp_remap.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ int Interp::add_parameters(setup_pointer settings,
294294
while (*s) {
295295
errored = true;
296296
char c = toupper(*s);
297-
strncat(tail,&c,1);
297+
strncat(tail,&c,2);
298298
if (*(s+1)) rtapi_strxcat(tail,",");
299299
s++;
300300
}

src/emc/task/emctaskmain.cc

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -284,8 +284,7 @@ static int argvize(const char *src, char *dst, char *argv[], int len)
284284
char inquote;
285285
char looking;
286286

287-
strncpy(dst, src, len);
288-
dst[len - 1] = 0;
287+
snprintf(dst, len, "%s", src);
289288
bufptr = dst;
290289
inquote = 0;
291290
argvix = 0;

src/hal/utils/halcmd_commands.cc

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1221,8 +1221,7 @@ int do_delsig_cmd(char *mod_name)
12211221
sig = SHMPTR(next);
12221222
/* we want to unload this signal, remember its name */
12231223
if ( n < ( MAX_EXPECTED_SIGS - 1 ) ) {
1224-
strncpy(sigs[n], sig->name, HAL_NAME_LEN );
1225-
sigs[n][HAL_NAME_LEN] = '\0';
1224+
snprintf(sigs[n], sizeof(sigs[n]), "%s", sig->name);
12261225
n++;
12271226
}
12281227
next = sig->next_ptr;
@@ -1315,8 +1314,7 @@ int do_unloadrt_cmd(char *mod_name)
13151314
if ( all || ( strcmp(mod_name, comp->name) == 0 )) {
13161315
/* we want to unload this component, remember its name */
13171316
if ( n < 63 ) {
1318-
strncpy(comps[n], comp->name, HAL_NAME_LEN );
1319-
comps[n][HAL_NAME_LEN] = '\0';
1317+
snprintf(comps[n], sizeof(comps[n]), "%s", comp->name);
13201318
n++;
13211319
}
13221320
}

src/hal/utils/halrmt.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1262,8 +1262,7 @@ static int doDelsig(char *mod_name, connectionRecType *context)
12621262
sig = SHMPTR(next);
12631263
/* we want to unload this signal, remember it's name */
12641264
if (n < ( MAX_EXPECTED_SIGS - 1)) {
1265-
strncpy(sigs[n], sig->name, HAL_NAME_LEN );
1266-
sigs[n][HAL_NAME_LEN] = '\0';
1265+
snprintf(sigs[n], sizeof(sigs[n]), "%s", sig->name);
12671266
n++;
12681267
}
12691268
next = sig->next_ptr;
@@ -1318,7 +1317,7 @@ static int doUnload(char *mod_name, connectionRecType *context)
13181317
if ( all || ( strcmp(mod_name, comp->name) == 0 )) {
13191318
/* we want to unload this component, remember its name */
13201319
if ( n < 63 ) {
1321-
strncpy(comps[n], comp->name, HAL_NAME_LEN );
1320+
snprintf(comps[n], sizeof(comps[n]), "%s", comp->name);
13221321
comps[n][HAL_NAME_LEN] = '\0';
13231322
n++;
13241323
}

src/hal/utils/scope_vert.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -765,7 +765,7 @@ static void offset_changed(GtkEditable * editable, struct offset_data *data)
765765

766766
/* maybe user typed something, save it in the buffer */
767767
text = gtk_entry_get_text(GTK_ENTRY(ctrl_usr->vert.offset_entry));
768-
strncpy(data->buf, text, BUFLEN);
768+
snprintf(data->buf, BUFLEN, "%s", text);
769769
}
770770

771771
/*

src/libnml/nml/nml.cc

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -178,12 +178,12 @@ NML::NML(NML_FORMAT_PTR f_ptr, const char *buf, const char *proc, const char *fi
178178
blocking_read_poll_interval = -1.0;
179179
forced_type = 0;
180180

181-
strncpy(bufname, buf, 40);
182-
strncpy(procname, proc, 40);
181+
snprintf(bufname, 40, "%s", buf);
182+
snprintf(procname, 40, "%s", proc);
183183
if (NULL == file) {
184184
file = default_nml_config_file;
185185
}
186-
strncpy(cfgfilename, file, 160);
186+
snprintf(cfgfilename, 160, "%s", file);
187187

188188
if (rcs_errors_printed >= max_rcs_errors_to_print
189189
&& max_rcs_errors_to_print > 0 && nml_reset_errors_printed) {
@@ -344,9 +344,9 @@ NML::NML(const char *buf, const char *proc, const char *file, int set_to_server,
344344
}
345345
registered_with_server = 0;
346346
cms_for_msg_string_conversions = 0;
347-
strncpy(bufname, buf, 40);
348-
strncpy(procname, proc, 40);
349-
strncpy(cfgfilename, file, 160);
347+
snprintf(bufname, 40 , "%s", buf);
348+
snprintf(procname, 40, "%s", proc);
349+
snprintf(cfgfilename, 160, "%s", file);
350350
blocking_read_poll_interval = -1.0;
351351
info_printed = 0;
352352
forced_type = 0;
@@ -2132,9 +2132,9 @@ void NML::print_info(const char *bufname, const char *procname, const char *cfg_
21322132
&& !strncmp(cfg_file, last_cfg_file, 40)) {
21332133
return;
21342134
}
2135-
strncpy(last_bufname, bufname, 10);
2136-
strncpy(last_procname, procname, 10);
2137-
strncpy(last_cfg_file, cfg_file, 40);
2135+
snprintf(last_bufname, 10, "%s", bufname);
2136+
snprintf(last_procname, 10, "%s", procname);
2137+
snprintf(last_cfg_file, 40, "%s", cfg_file);
21382138
}
21392139
if (!info_message_printed) {
21402140
rcs_print
@@ -2449,8 +2449,8 @@ void nmlSetHostAlias(const char *hostName, const char *hostAlias)
24492449
cmsHostAliases = new LinkedList;
24502450
}
24512451
CMS_HOST_ALIAS_ENTRY entry;
2452-
strncpy(entry.host, hostName, 64);
2453-
strncpy(entry.alias, hostAlias, 64);
2452+
snprintf(entry.host, 64, "%s", hostName);
2453+
snprintf(entry.alias, 64, "%s", hostAlias);
24542454
cmsHostAliases->store_at_tail(&entry, sizeof(entry), 1);
24552455
}
24562456

src/libnml/nml/nml_mod.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -818,7 +818,7 @@ NML_MODULE::write_status_out ()
818818
statusOutData->source_line = source_line;
819819
if (NULL != source_file)
820820
{
821-
strncpy (statusOutData->source_file, source_file, 64);
821+
snprintf(statusOutData->source_file, 64, "%s", source_file);
822822
}
823823

824824
// write STATUS

0 commit comments

Comments
 (0)