Skip to content

Commit 3a03a10

Browse files
smoepetterreinholdtsen
authored andcommitted
strncpy -> rtapi_strlcpy for string termination
Moved #incl of rtapi_string outside extern C Remove request for consts Added FIXME about memory leak and fixed whitespace issue (Petter) Rebased by Petter Reinhodltsen on top of current 2.9.
1 parent 5dde589 commit 3a03a10

File tree

19 files changed

+295
-290
lines changed

19 files changed

+295
-290
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: 4 additions & 4 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);

src/emc/rs274ngc/interp_read.cc

Lines changed: 5 additions & 5 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

@@ -1597,7 +1597,7 @@ int Interp::read_o( /* ARGUMENTS */
15971597
// context
15981598
if (strlen(_setup.sub_context[_setup.call_level].subName) >= sizeof(oNameBuf))
15991599
ERS(NCE_UNABLE_TO_OPEN_FILE, _setup.sub_context[_setup.call_level].subName);
1600-
strncpy(oNameBuf, _setup.sub_context[_setup.call_level].subName,
1600+
rtapi_strlcpy(oNameBuf, _setup.sub_context[_setup.call_level].subName,
16011601
sizeof(oNameBuf));
16021602
} else
16031603
// any other m-code should have been handled by read_m()
@@ -3154,16 +3154,16 @@ int Interp::read_text(
31543154
index--) { // remove space at end of raw_line, especially CR & LF
31553155
raw_line[index] = 0;
31563156
}
3157-
strncpy(line, raw_line, LINELEN);
3157+
rtapi_strlcpy(line, raw_line, LINELEN);
31583158
CHP(close_and_downcase(line));
31593159
if ((line[0] == '%') && (line[1] == 0) && (_setup.percent_flag)) {
31603160
FINISH();
31613161
return INTERP_ENDFILE;
31623162
}
31633163
} else {
31643164
CHKS((strlen(command) >= LINELEN), NCE_COMMAND_TOO_LONG);
3165-
strncpy(raw_line, command, LINELEN);
3166-
strncpy(line, command, LINELEN);
3165+
rtapi_strlcpy(raw_line, command, LINELEN);
3166+
rtapi_strlcpy(line, command, LINELEN);
31673167
CHP(close_and_downcase(line));
31683168
}
31693169

src/emc/rs274ngc/rs274ngc_pre.cc

Lines changed: 1 addition & 1 deletion
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

src/emc/task/emctask.cc

Lines changed: 2 additions & 3 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)
@@ -130,7 +129,7 @@ int emcTaskInit()
130129
strncpy(mdir[0], inistring, sizeof(mdir[0]));
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
}
135134
dmax = 1; //one directory mdir[0], USER_M_PATH specifies additional dirs
136135

src/emc/task/emctaskmain.cc

Lines changed: 5 additions & 5 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
@@ -3171,16 +3171,16 @@ static int iniLoad(const char *filename)
31713171
if (emc_debug & EMC_DEBUG_VERSIONS) {
31723172
if (NULL != (inistring = inifile.Find("VERSION", "EMC"))) {
31733173
if(sscanf(inistring, "$Revision: %s", version) != 1) {
3174-
strncpy(version, "unknown", LINELEN-1);
3174+
rtapi_strlcpy(version, "unknown", LINELEN-1);
31753175
}
31763176
} else {
3177-
strncpy(version, "unknown", LINELEN-1);
3177+
rtapi_strlcpy(version, "unknown", LINELEN-1);
31783178
}
31793179

31803180
if (NULL != (inistring = inifile.Find("MACHINE", "EMC"))) {
3181-
strncpy(machine, inistring, LINELEN-1);
3181+
rtapi_strlcpy(machine, inistring, LINELEN-1);
31823182
} else {
3183-
strncpy(machine, "unknown", LINELEN-1);
3183+
rtapi_strlcpy(machine, "unknown", LINELEN-1);
31843184
}
31853185
rcs_print("task: machine: '%s' version '%s'\n", machine, version);
31863186
}

0 commit comments

Comments
 (0)