Skip to content

Commit 5be3e0a

Browse files
committed
strncpy -> rtapi_strlcpy for string termination
1 parent af0a524 commit 5be3e0a

File tree

20 files changed

+308
-311
lines changed

20 files changed

+308
-311
lines changed

src/emc/rs274ngc/interp_internal.hh

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "libintl.h"
2727
#include <boost/python/object_fwd.hpp>
2828
#include <cmath>
29+
#include <rtapi_string.h> // rtapi_strlcpy()
2930
#include "interp_parameter_def.hh"
3031
#include "interp_fwd.hh"
3132
#include "interp_base.hh"
@@ -859,7 +860,7 @@ macros totally crash-proof. If the function call stack is deeper than
859860
do { \
860861
setError (fmt, ## __VA_ARGS__); \
861862
_setup.stack_index = 0; \
862-
(strncpy(_setup.stack[_setup.stack_index], __PRETTY_FUNCTION__, STACK_ENTRY_LEN)); \
863+
(rtapi_strlcpy(_setup.stack[_setup.stack_index], __PRETTY_FUNCTION__, STACK_ENTRY_LEN)); \
863864
_setup.stack[_setup.stack_index][STACK_ENTRY_LEN-1] = 0; \
864865
_setup.stack_index++; \
865866
_setup.stack[_setup.stack_index][0] = 0; \
@@ -870,7 +871,7 @@ macros totally crash-proof. If the function call stack is deeper than
870871
do { \
871872
setError (fmt, ## __VA_ARGS__); \
872873
_setup.stack_index = 0; \
873-
(strncpy(_setup.stack[_setup.stack_index], __PRETTY_FUNCTION__, STACK_ENTRY_LEN)); \
874+
(rtapi_strlcpy(_setup.stack[_setup.stack_index], __PRETTY_FUNCTION__, STACK_ENTRY_LEN)); \
874875
_setup.stack[_setup.stack_index][STACK_ENTRY_LEN-1] = 0; \
875876
_setup.stack_index++; \
876877
_setup.stack[_setup.stack_index][0] = 0; \
@@ -881,7 +882,7 @@ macros totally crash-proof. If the function call stack is deeper than
881882
#define ERN(error_code) \
882883
do { \
883884
_setup.stack_index = 0; \
884-
(strncpy(_setup.stack[_setup.stack_index], __PRETTY_FUNCTION__, STACK_ENTRY_LEN)); \
885+
(rtapi_strlcpy(_setup.stack[_setup.stack_index], __PRETTY_FUNCTION__, STACK_ENTRY_LEN)); \
885886
_setup.stack[_setup.stack_index][STACK_ENTRY_LEN-1] = 0; \
886887
_setup.stack_index++; \
887888
_setup.stack[_setup.stack_index][0] = 0; \
@@ -893,7 +894,7 @@ macros totally crash-proof. If the function call stack is deeper than
893894
#define ERP(error_code) \
894895
do { \
895896
if (_setup.stack_index < STACK_LEN - 1) { \
896-
(strncpy(_setup.stack[_setup.stack_index], __PRETTY_FUNCTION__, STACK_ENTRY_LEN)); \
897+
(rtapi_strlcpy(_setup.stack[_setup.stack_index], __PRETTY_FUNCTION__, STACK_ENTRY_LEN)); \
897898
_setup.stack[_setup.stack_index][STACK_ENTRY_LEN-1] = 0; \
898899
_setup.stack_index++; \
899900
_setup.stack[_setup.stack_index][0] = 0; \

src/emc/rs274ngc/interp_o_word.cc

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
#include "rs274ngc_interp.hh"
3535
#include "python_plugin.hh"
3636
#include "interp_python.hh"
37-
#include <rtapi_string.h>
37+
#include <rtapi_string.h> // rtapi_strlcpy()
3838

3939
namespace bp = boost::python;
4040

@@ -60,7 +60,7 @@ int Interp::findFile( // ARGUMENTS
6060
snprintf(targetPath, PATH_MAX, "%s/%s", direct, target);
6161
file = fopen(targetPath, "r");
6262
if (file) {
63-
strncpy(foundFileDirect, direct, PATH_MAX);
63+
rtapi_strlcpy(foundFileDirect, direct, PATH_MAX);
6464
fclose(file);
6565
return INTERP_OK;
6666
}
@@ -71,8 +71,8 @@ int Interp::findFile( // ARGUMENTS
7171

7272
while ((aFile = readdir(aDir))) {
7373
if (aFile->d_type == DT_DIR &&
74-
(0 != strcmp(aFile->d_name, "..")) &&
75-
(0 != strcmp(aFile->d_name, "."))) {
74+
(0 != strncmp(aFile->d_name, "..", 3)) &&
75+
(0 != strncmp(aFile->d_name, ".", 2))) {
7676

7777
char path[PATH_MAX+1];
7878
snprintf(path, PATH_MAX, "%s/%s", direct, aFile->d_name);
@@ -561,7 +561,7 @@ int Interp::control_back_to( block_pointer block, // pointer to block
561561
newFP = fopen(op->filename, "r");
562562
// set the line number
563563
settings->sequence_number = 0;
564-
strncpy(settings->filename, op->filename, sizeof(settings->filename));
564+
rtapi_strlcpy(settings->filename, op->filename, sizeof(settings->filename));
565565
if (settings->filename[sizeof(settings->filename)-1] != '\0') {
566566
fclose(settings->file_pointer);
567567
logOword("filename too long: %s", op->filename);
@@ -597,7 +597,7 @@ int Interp::control_back_to( block_pointer block, // pointer to block
597597
if (settings->file_pointer)
598598
fclose(settings->file_pointer);
599599
settings->file_pointer = newFP;
600-
strncpy(settings->filename, newFileName, sizeof(settings->filename));
600+
rtapi_strlcpy(settings->filename, newFileName, sizeof(settings->filename));
601601
if (settings->filename[sizeof(settings->filename)-1] != '\0') {
602602
logOword("new filename '%s' is too long (max len %zu)\n", newFileName, sizeof(settings->filename)-1);
603603
settings->filename[sizeof(settings->filename)-1] = '\0'; // oh well, truncate the filename

src/emc/rs274ngc/interp_read.cc

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
#include "rs274ngc_interp.hh"
3030
#include "rtapi_math.h"
3131
#include <cmath>
32-
#include <rtapi_string.h>
32+
#include <rtapi_string.h> // rtapi_strlcpy()
3333

3434
using namespace interp_param_global;
3535

@@ -1595,8 +1595,7 @@ int Interp::read_o( /* ARGUMENTS */
15951595

15961596
// Subroutine name not provided in Fanuc syntax, so pull from
15971597
// context
1598-
strncpy(oNameBuf, _setup.sub_context[_setup.call_level].subName,
1599-
LINELEN+1);
1598+
rtapi_strlcpy(oNameBuf, _setup.sub_context[_setup.call_level].subName, LINELEN+1);
16001599
} else
16011600
// any other m-code should have been handled by read_m()
16021601
OERR(_("%d: Bug: Non-m98/m99 M-code passed to read_o(): '%s'"),
@@ -3152,16 +3151,16 @@ int Interp::read_text(
31523151
index--) { // remove space at end of raw_line, especially CR & LF
31533152
raw_line[index] = 0;
31543153
}
3155-
strncpy(line, raw_line, LINELEN);
3154+
rtapi_strlcpy(line, raw_line, LINELEN);
31563155
CHP(close_and_downcase(line));
31573156
if ((line[0] == '%') && (line[1] == 0) && (_setup.percent_flag)) {
31583157
FINISH();
31593158
return INTERP_ENDFILE;
31603159
}
31613160
} else {
31623161
CHKS((strlen(command) >= LINELEN), NCE_COMMAND_TOO_LONG);
3163-
strncpy(raw_line, command, LINELEN);
3164-
strncpy(line, command, LINELEN);
3162+
rtapi_strlcpy(raw_line, command, LINELEN);
3163+
rtapi_strlcpy(line, command, LINELEN);
31653164
CHP(close_and_downcase(line));
31663165
}
31673166

src/emc/rs274ngc/rs274ngc_pre.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ include an option for suppressing superfluous commands.
8989
#include <set>
9090
#include <stdexcept>
9191
#include <new>
92-
#include <rtapi_string.h>
92+
#include <rtapi_string.h> // rtapi_strlcpy()
9393

9494
#include "rtapi.h"
9595
#include "inifile.hh" // INIFILE
@@ -2492,7 +2492,7 @@ int Interp::ini_load(const char *filename)
24922492

24932493
char parameter_file_name[LINELEN]={};
24942494
if (NULL != (inistring = inifile.Find("PARAMETER_FILE", "RS274NGC"))) {
2495-
strncpy(parameter_file_name, inistring, LINELEN);
2495+
rtapi_strlcpy(parameter_file_name, inistring, LINELEN);
24962496

24972497
if (parameter_file_name[LINELEN-1] != '\0') {
24982498
logDebug("%s:[RS274NGC]PARAMETER_FILE is too long (max len %d)", filename, LINELEN-1);

src/emc/task/emctask.cc

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
********************************************************************/
1515

1616
#include <stdlib.h>
17-
#include <string.h> // strncpy()
17+
#include <rtapi_string.h> // rtapi_strlcpy()
1818
#include <sys/stat.h> // struct stat
1919
#include <unistd.h> // stat()
2020
#include <limits.h> // PATH_MAX
@@ -34,7 +34,6 @@
3434
#include "python_plugin.hh"
3535
#include "taskclass.hh"
3636
#include "motion.h"
37-
#include <rtapi_string.h>
3837

3938
#define USER_DEFINED_FUNCTION_MAX_DIRS 5
4039
#define MAX_M_DIRS (USER_DEFINED_FUNCTION_MAX_DIRS+1)
@@ -123,14 +122,14 @@ int emcTaskInit()
123122

124123
// Identify user_defined_function directories
125124
if (NULL != (inistring = inifile.Find("PROGRAM_PREFIX", "DISPLAY"))) {
126-
strncpy(mdir[0],inistring, sizeof(mdir[0]));
125+
rtapi_strlcpy(mdir[0],inistring, sizeof(mdir[0]));
127126
if (mdir[0][sizeof(mdir[0])-1] != '\0') {
128127
rcs_print("[DISPLAY]PROGRAM_PREFIX too long (max len %zu)\n", sizeof(mdir[0]));
129128
return -1;
130129
}
131130
} else {
132131
// default dir if no PROGRAM_PREFIX
133-
strncpy(mdir[0],"nc_files", sizeof(mdir[0]));
132+
rtapi_strlcpy(mdir[0],"nc_files", sizeof(mdir[0]));
134133
if (mdir[0][sizeof(mdir[0])-1] != '\0') {
135134
rcs_print("default nc_files too long (max len %zu)\n", sizeof(mdir[0]));
136135
return -1;
@@ -146,7 +145,7 @@ int emcTaskInit()
146145

147146
for (dct=1; dct < MAX_M_DIRS; dct++) mdir[dct][0] = 0;
148147

149-
strncpy(tmpdirs,inistring, sizeof(tmpdirs));
148+
rtapi_strlcpy(tmpdirs,inistring, sizeof(tmpdirs));
150149
if (tmpdirs[sizeof(tmpdirs)-1] != '\0') {
151150
rcs_print("[RS274NGC]USER_M_PATH too long (max len %zu)\n", sizeof(tmpdirs));
152151
return -1;
@@ -156,7 +155,7 @@ int emcTaskInit()
156155
dct = 1;
157156
while (dct < MAX_M_DIRS) {
158157
if (nextdir == NULL) break; // no more tokens
159-
strncpy(mdir[dct],nextdir, sizeof(mdir[dct]));
158+
rtapi_strlcpy(mdir[dct],nextdir, sizeof(mdir[dct]));
160159
if (mdir[dct][sizeof(mdir[dct])-1] != '\0') {
161160
rcs_print("[RS274NGC]USER_M_PATH component (%s) too long (max len %zu)\n", nextdir, sizeof(mdir[dct]));
162161
return -1;

src/emc/task/emctaskmain.cc

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
#include <libintl.h>
6060
#include <locale.h>
6161
#include "usrmotintf.h"
62-
#include <rtapi_string.h>
62+
#include <rtapi_string.h> // rtapi_strlcpy()
6363
#include "tooldata.hh"
6464

6565
#if 0
@@ -284,7 +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);
287+
rtapi_strlcpy(dst, src, len);
288288
dst[len - 1] = 0;
289289
bufptr = dst;
290290
inquote = 0;
@@ -3172,16 +3172,16 @@ static int iniLoad(const char *filename)
31723172
if (emc_debug & EMC_DEBUG_VERSIONS) {
31733173
if (NULL != (inistring = inifile.Find("VERSION", "EMC"))) {
31743174
if(sscanf(inistring, "$Revision: %s", version) != 1) {
3175-
strncpy(version, "unknown", LINELEN-1);
3175+
rtapi_strlcpy(version, "unknown", LINELEN-1);
31763176
}
31773177
} else {
3178-
strncpy(version, "unknown", LINELEN-1);
3178+
rtapi_strlcpy(version, "unknown", LINELEN-1);
31793179
}
31803180

31813181
if (NULL != (inistring = inifile.Find("MACHINE", "EMC"))) {
3182-
strncpy(machine, inistring, LINELEN-1);
3182+
rtapi_strlcpy(machine, inistring, LINELEN-1);
31833183
} else {
3184-
strncpy(machine, "unknown", LINELEN-1);
3184+
rtapi_strlcpy(machine, "unknown", LINELEN-1);
31853185
}
31863186
rcs_print("task: machine: '%s' version '%s'\n", machine, version);
31873187
}

0 commit comments

Comments
 (0)