Skip to content

Commit

Permalink
strncpy -> rtapi_strlcpy for string termination
Browse files Browse the repository at this point in the history
  • Loading branch information
smoe committed Oct 7, 2023
1 parent af0a524 commit 5be3e0a
Show file tree
Hide file tree
Showing 20 changed files with 308 additions and 311 deletions.
9 changes: 5 additions & 4 deletions src/emc/rs274ngc/interp_internal.hh
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "libintl.h"
#include <boost/python/object_fwd.hpp>
#include <cmath>
#include <rtapi_string.h> // rtapi_strlcpy()
#include "interp_parameter_def.hh"
#include "interp_fwd.hh"
#include "interp_base.hh"
Expand Down Expand Up @@ -859,7 +860,7 @@ macros totally crash-proof. If the function call stack is deeper than
do { \
setError (fmt, ## __VA_ARGS__); \
_setup.stack_index = 0; \
(strncpy(_setup.stack[_setup.stack_index], __PRETTY_FUNCTION__, STACK_ENTRY_LEN)); \
(rtapi_strlcpy(_setup.stack[_setup.stack_index], __PRETTY_FUNCTION__, STACK_ENTRY_LEN)); \
_setup.stack[_setup.stack_index][STACK_ENTRY_LEN-1] = 0; \
_setup.stack_index++; \
_setup.stack[_setup.stack_index][0] = 0; \
Expand All @@ -870,7 +871,7 @@ macros totally crash-proof. If the function call stack is deeper than
do { \
setError (fmt, ## __VA_ARGS__); \
_setup.stack_index = 0; \
(strncpy(_setup.stack[_setup.stack_index], __PRETTY_FUNCTION__, STACK_ENTRY_LEN)); \
(rtapi_strlcpy(_setup.stack[_setup.stack_index], __PRETTY_FUNCTION__, STACK_ENTRY_LEN)); \
_setup.stack[_setup.stack_index][STACK_ENTRY_LEN-1] = 0; \
_setup.stack_index++; \
_setup.stack[_setup.stack_index][0] = 0; \
Expand All @@ -881,7 +882,7 @@ macros totally crash-proof. If the function call stack is deeper than
#define ERN(error_code) \
do { \
_setup.stack_index = 0; \
(strncpy(_setup.stack[_setup.stack_index], __PRETTY_FUNCTION__, STACK_ENTRY_LEN)); \
(rtapi_strlcpy(_setup.stack[_setup.stack_index], __PRETTY_FUNCTION__, STACK_ENTRY_LEN)); \
_setup.stack[_setup.stack_index][STACK_ENTRY_LEN-1] = 0; \
_setup.stack_index++; \
_setup.stack[_setup.stack_index][0] = 0; \
Expand All @@ -893,7 +894,7 @@ macros totally crash-proof. If the function call stack is deeper than
#define ERP(error_code) \
do { \
if (_setup.stack_index < STACK_LEN - 1) { \
(strncpy(_setup.stack[_setup.stack_index], __PRETTY_FUNCTION__, STACK_ENTRY_LEN)); \
(rtapi_strlcpy(_setup.stack[_setup.stack_index], __PRETTY_FUNCTION__, STACK_ENTRY_LEN)); \
_setup.stack[_setup.stack_index][STACK_ENTRY_LEN-1] = 0; \
_setup.stack_index++; \
_setup.stack[_setup.stack_index][0] = 0; \
Expand Down
12 changes: 6 additions & 6 deletions src/emc/rs274ngc/interp_o_word.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
#include "rs274ngc_interp.hh"
#include "python_plugin.hh"
#include "interp_python.hh"
#include <rtapi_string.h>
#include <rtapi_string.h> // rtapi_strlcpy()

namespace bp = boost::python;

Expand All @@ -60,7 +60,7 @@ int Interp::findFile( // ARGUMENTS
snprintf(targetPath, PATH_MAX, "%s/%s", direct, target);
file = fopen(targetPath, "r");
if (file) {
strncpy(foundFileDirect, direct, PATH_MAX);
rtapi_strlcpy(foundFileDirect, direct, PATH_MAX);
fclose(file);
return INTERP_OK;
}
Expand All @@ -71,8 +71,8 @@ int Interp::findFile( // ARGUMENTS

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

char path[PATH_MAX+1];
snprintf(path, PATH_MAX, "%s/%s", direct, aFile->d_name);
Expand Down Expand Up @@ -561,7 +561,7 @@ int Interp::control_back_to( block_pointer block, // pointer to block
newFP = fopen(op->filename, "r");
// set the line number
settings->sequence_number = 0;
strncpy(settings->filename, op->filename, sizeof(settings->filename));
rtapi_strlcpy(settings->filename, op->filename, sizeof(settings->filename));
if (settings->filename[sizeof(settings->filename)-1] != '\0') {
fclose(settings->file_pointer);
logOword("filename too long: %s", op->filename);
Expand Down Expand Up @@ -597,7 +597,7 @@ int Interp::control_back_to( block_pointer block, // pointer to block
if (settings->file_pointer)
fclose(settings->file_pointer);
settings->file_pointer = newFP;
strncpy(settings->filename, newFileName, sizeof(settings->filename));
rtapi_strlcpy(settings->filename, newFileName, sizeof(settings->filename));
if (settings->filename[sizeof(settings->filename)-1] != '\0') {
logOword("new filename '%s' is too long (max len %zu)\n", newFileName, sizeof(settings->filename)-1);
settings->filename[sizeof(settings->filename)-1] = '\0'; // oh well, truncate the filename
Expand Down
11 changes: 5 additions & 6 deletions src/emc/rs274ngc/interp_read.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
#include "rs274ngc_interp.hh"
#include "rtapi_math.h"
#include <cmath>
#include <rtapi_string.h>
#include <rtapi_string.h> // rtapi_strlcpy()

using namespace interp_param_global;

Expand Down Expand Up @@ -1595,8 +1595,7 @@ int Interp::read_o( /* ARGUMENTS */

// Subroutine name not provided in Fanuc syntax, so pull from
// context
strncpy(oNameBuf, _setup.sub_context[_setup.call_level].subName,
LINELEN+1);
rtapi_strlcpy(oNameBuf, _setup.sub_context[_setup.call_level].subName, LINELEN+1);
} else
// any other m-code should have been handled by read_m()
OERR(_("%d: Bug: Non-m98/m99 M-code passed to read_o(): '%s'"),
Expand Down Expand Up @@ -3152,16 +3151,16 @@ int Interp::read_text(
index--) { // remove space at end of raw_line, especially CR & LF
raw_line[index] = 0;
}
strncpy(line, raw_line, LINELEN);
rtapi_strlcpy(line, raw_line, LINELEN);
CHP(close_and_downcase(line));
if ((line[0] == '%') && (line[1] == 0) && (_setup.percent_flag)) {
FINISH();
return INTERP_ENDFILE;
}
} else {
CHKS((strlen(command) >= LINELEN), NCE_COMMAND_TOO_LONG);
strncpy(raw_line, command, LINELEN);
strncpy(line, command, LINELEN);
rtapi_strlcpy(raw_line, command, LINELEN);
rtapi_strlcpy(line, command, LINELEN);
CHP(close_and_downcase(line));
}

Expand Down
4 changes: 2 additions & 2 deletions src/emc/rs274ngc/rs274ngc_pre.cc
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ include an option for suppressing superfluous commands.
#include <set>
#include <stdexcept>
#include <new>
#include <rtapi_string.h>
#include <rtapi_string.h> // rtapi_strlcpy()

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

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

if (parameter_file_name[LINELEN-1] != '\0') {
logDebug("%s:[RS274NGC]PARAMETER_FILE is too long (max len %d)", filename, LINELEN-1);
Expand Down
11 changes: 5 additions & 6 deletions src/emc/task/emctask.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
********************************************************************/

#include <stdlib.h>
#include <string.h> // strncpy()
#include <rtapi_string.h> // rtapi_strlcpy()
#include <sys/stat.h> // struct stat
#include <unistd.h> // stat()
#include <limits.h> // PATH_MAX
Expand All @@ -34,7 +34,6 @@
#include "python_plugin.hh"
#include "taskclass.hh"
#include "motion.h"
#include <rtapi_string.h>

#define USER_DEFINED_FUNCTION_MAX_DIRS 5
#define MAX_M_DIRS (USER_DEFINED_FUNCTION_MAX_DIRS+1)
Expand Down Expand Up @@ -123,14 +122,14 @@ int emcTaskInit()

// Identify user_defined_function directories
if (NULL != (inistring = inifile.Find("PROGRAM_PREFIX", "DISPLAY"))) {
strncpy(mdir[0],inistring, sizeof(mdir[0]));
rtapi_strlcpy(mdir[0],inistring, sizeof(mdir[0]));
if (mdir[0][sizeof(mdir[0])-1] != '\0') {
rcs_print("[DISPLAY]PROGRAM_PREFIX too long (max len %zu)\n", sizeof(mdir[0]));
return -1;
}
} else {
// default dir if no PROGRAM_PREFIX
strncpy(mdir[0],"nc_files", sizeof(mdir[0]));
rtapi_strlcpy(mdir[0],"nc_files", sizeof(mdir[0]));
if (mdir[0][sizeof(mdir[0])-1] != '\0') {
rcs_print("default nc_files too long (max len %zu)\n", sizeof(mdir[0]));
return -1;
Expand All @@ -146,7 +145,7 @@ int emcTaskInit()

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

strncpy(tmpdirs,inistring, sizeof(tmpdirs));
rtapi_strlcpy(tmpdirs,inistring, sizeof(tmpdirs));
if (tmpdirs[sizeof(tmpdirs)-1] != '\0') {
rcs_print("[RS274NGC]USER_M_PATH too long (max len %zu)\n", sizeof(tmpdirs));
return -1;
Expand All @@ -156,7 +155,7 @@ int emcTaskInit()
dct = 1;
while (dct < MAX_M_DIRS) {
if (nextdir == NULL) break; // no more tokens
strncpy(mdir[dct],nextdir, sizeof(mdir[dct]));
rtapi_strlcpy(mdir[dct],nextdir, sizeof(mdir[dct]));
if (mdir[dct][sizeof(mdir[dct])-1] != '\0') {
rcs_print("[RS274NGC]USER_M_PATH component (%s) too long (max len %zu)\n", nextdir, sizeof(mdir[dct]));
return -1;
Expand Down
12 changes: 6 additions & 6 deletions src/emc/task/emctaskmain.cc
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
#include <libintl.h>
#include <locale.h>
#include "usrmotintf.h"
#include <rtapi_string.h>
#include <rtapi_string.h> // rtapi_strlcpy()
#include "tooldata.hh"

#if 0
Expand Down Expand Up @@ -284,7 +284,7 @@ static int argvize(const char *src, char *dst, char *argv[], int len)
char inquote;
char looking;

strncpy(dst, src, len);
rtapi_strlcpy(dst, src, len);
dst[len - 1] = 0;
bufptr = dst;
inquote = 0;
Expand Down Expand Up @@ -3172,16 +3172,16 @@ static int iniLoad(const char *filename)
if (emc_debug & EMC_DEBUG_VERSIONS) {
if (NULL != (inistring = inifile.Find("VERSION", "EMC"))) {
if(sscanf(inistring, "$Revision: %s", version) != 1) {
strncpy(version, "unknown", LINELEN-1);
rtapi_strlcpy(version, "unknown", LINELEN-1);
}
} else {
strncpy(version, "unknown", LINELEN-1);
rtapi_strlcpy(version, "unknown", LINELEN-1);
}

if (NULL != (inistring = inifile.Find("MACHINE", "EMC"))) {
strncpy(machine, inistring, LINELEN-1);
rtapi_strlcpy(machine, inistring, LINELEN-1);
} else {
strncpy(machine, "unknown", LINELEN-1);
rtapi_strlcpy(machine, "unknown", LINELEN-1);
}
rcs_print("task: machine: '%s' version '%s'\n", machine, version);
}
Expand Down
Loading

0 comments on commit 5be3e0a

Please sign in to comment.