Skip to content

Commit 9e74a8f

Browse files
authored
Merge pull request #3763 from grandixximo/inifile-fix
inifile: Fix non-reentrant Find() returning pointer to static buffer
2 parents c7ea4db + 3575974 commit 9e74a8f

26 files changed

Lines changed: 304 additions & 324 deletions

File tree

src/emc/ini/emcIniFile.hh

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,9 @@ public:
7676
return(IniFile::Find(result,
7777
tag, section, num));
7878
}
79-
const char * Find(const char *tag, const char *section=NULL,
79+
std::optional<std::string> Find(const char *tag, const char *section=NULL,
8080
int num = 1){
81-
return(IniFile::Find(tag, section, num)
82-
.value_or(nullptr));
81+
return(IniFile::Find(tag, section, num));
8382
}
8483

8584
private:

src/emc/ini/inijoint.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ extern value_inihal_data old_inihal_data;
7373
static int loadJoint(int joint, EmcIniFile *jointIniFile)
7474
{
7575
char jointString[16];
76-
const char *inistring;
7776
EmcJointType jointType;
7877
double units;
7978
double backlash;
@@ -234,8 +233,9 @@ static int loadJoint(int joint, EmcIniFile *jointIniFile)
234233

235234
comp_file_type = 0; // default
236235
jointIniFile->Find(&comp_file_type, "COMP_FILE_TYPE", jointString);
237-
if (NULL != (inistring = jointIniFile->Find("COMP_FILE", jointString))) {
238-
if (0 != emcJointLoadComp(joint, inistring, comp_file_type)) {
236+
auto comp_file = jointIniFile->Find("COMP_FILE", jointString);
237+
if (comp_file) {
238+
if (0 != emcJointLoadComp(joint, comp_file->c_str(), comp_file_type)) {
239239
return -1;
240240
}
241241
}

src/emc/ini/initraj.cc

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -109,33 +109,33 @@ static int loadTraj(EmcIniFile *trajInifile)
109109

110110
try{
111111
int axismask = 0;
112-
const char *coord = trajInifile->Find("COORDINATES", "TRAJ");
112+
auto coord = trajInifile->Find("COORDINATES", "TRAJ");
113113
if(coord) {
114-
if(strchr(coord, 'x') || strchr(coord, 'X')) {
114+
if(coord->find_first_of("xX") != std::string::npos) {
115115
axismask |= 1;
116116
}
117-
if(strchr(coord, 'y') || strchr(coord, 'Y')) {
117+
if(coord->find_first_of("yY") != std::string::npos) {
118118
axismask |= 2;
119119
}
120-
if(strchr(coord, 'z') || strchr(coord, 'Z')) {
120+
if(coord->find_first_of("zZ") != std::string::npos) {
121121
axismask |= 4;
122122
}
123-
if(strchr(coord, 'a') || strchr(coord, 'A')) {
123+
if(coord->find_first_of("aA") != std::string::npos) {
124124
axismask |= 8;
125125
}
126-
if(strchr(coord, 'b') || strchr(coord, 'B')) {
126+
if(coord->find_first_of("bB") != std::string::npos) {
127127
axismask |= 16;
128128
}
129-
if(strchr(coord, 'c') || strchr(coord, 'C')) {
129+
if(coord->find_first_of("cC") != std::string::npos) {
130130
axismask |= 32;
131131
}
132-
if(strchr(coord, 'u') || strchr(coord, 'U')) {
132+
if(coord->find_first_of("uU") != std::string::npos) {
133133
axismask |= 64;
134134
}
135-
if(strchr(coord, 'v') || strchr(coord, 'V')) {
135+
if(coord->find_first_of("vV") != std::string::npos) {
136136
axismask |= 128;
137137
}
138-
if(strchr(coord, 'w') || strchr(coord, 'W')) {
138+
if(coord->find_first_of("wW") != std::string::npos) {
139139
axismask |= 256;
140140
}
141141
} else {
@@ -295,22 +295,22 @@ static int loadTraj(EmcIniFile *trajInifile)
295295
return -1;
296296
}
297297
try{
298-
const char *inistring;
299298
unsigned char coordinateMark[6] = { 1, 1, 1, 0, 0, 0 };
300299
int t;
301300
int len;
302301
char homes[LINELEN];
303302
char home[LINELEN];
304303
EmcPose homePose = { {0.0, 0.0, 0.0}, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 };
305304
double d;
306-
if (NULL != (inistring = trajInifile->Find("HOME", "TRAJ"))) {
305+
auto inistring = trajInifile->Find("HOME", "TRAJ");
306+
if (inistring) {
307307
// [TRAJ]HOME is important for genhexkins.c kinetmaticsForward()
308308
// and probably other non-identity kins that solve the forward
309309
// kinematics with an iterative algorithm when the homePose
310310
// is not all zeros
311311

312312
// found it, now interpret it according to coordinateMark[]
313-
rtapi_strxcpy(homes, inistring);
313+
rtapi_strxcpy(homes, inistring->c_str());
314314
len = 0;
315315
for (t = 0; t < 6; t++) {
316316
if (!coordinateMark[t]) {
@@ -354,7 +354,7 @@ static int loadTraj(EmcIniFile *trajInifile)
354354
} else {
355355
// badly formatted entry
356356
rcs_print("invalid INI file value for [TRAJ] HOME: %s\n",
357-
inistring);
357+
inistring->c_str());
358358
return -1;
359359
}
360360
} // end of for-loop on coordinateMark[]

src/emc/pythonplugin/python_plugin.cc

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,6 @@ int PythonPlugin::configure(const char *iniFilename,
339339
const char *section)
340340
{
341341
IniFile inifile;
342-
std::optional<const char*> inistring;
343342

344343
if (section == NULL) {
345344
logPP(1, "no section");
@@ -360,16 +359,16 @@ int PythonPlugin::configure(const char *iniFilename,
360359

361360
char real_path[PATH_MAX];
362361
char expandinistring[PATH_MAX];
363-
if ((inistring = inifile.Find("TOPLEVEL", section))) {
364-
if (inifile.TildeExpansion(*inistring,expandinistring,sizeof(expandinistring))) {
362+
if (auto inistring = inifile.Find("TOPLEVEL", section)) {
363+
if (inifile.TildeExpansion(inistring->c_str(),expandinistring,sizeof(expandinistring))) {
365364
logPP(-1, "TildeExpansion failed '%s'", toplevel);
366365
status = PLUGIN_BAD_PATH;
367366
return status;
368367
}
369368
toplevel = strstore(expandinistring);
370369

371-
if ((inistring = inifile.Find("RELOAD_ON_CHANGE", section)))
372-
reload_on_change = (atoi(*inistring) > 0);
370+
if (auto reload_str = inifile.Find("RELOAD_ON_CHANGE", section))
371+
reload_on_change = (atoi(reload_str->c_str()) > 0);
373372

374373
if (realpath(toplevel, real_path) == NULL) {
375374
logPP(-1, "can\'t resolve path to '%s'", toplevel);
@@ -394,16 +393,16 @@ int PythonPlugin::configure(const char *iniFilename,
394393
abs_path = strstore(real_path);
395394
}
396395

397-
if ((inistring = inifile.Find("LOG_LEVEL", section)))
398-
log_level = atoi(*inistring);
396+
if (auto inistring = inifile.Find("LOG_LEVEL", section))
397+
log_level = atoi(inistring->c_str());
399398
else log_level = 0;
400399

401400
char pycmd[PATH_MAX + 64];
402401
int n = 1;
403402
int lineno;
404-
while ((inistring = inifile.Find("PATH_PREPEND", "PYTHON",
405-
n, &lineno))) {
406-
if (inifile.TildeExpansion(*inistring,expandinistring,sizeof(expandinistring))) {
403+
while (auto inistring = inifile.Find("PATH_PREPEND", "PYTHON",
404+
n, &lineno)) {
405+
if (inifile.TildeExpansion(inistring->c_str(),expandinistring,sizeof(expandinistring))) {
407406
logPP(-1, "TildeExpansion failed '%s'", toplevel);
408407
status = PLUGIN_EXCEPTION_DURING_PATH_PREPEND;
409408
return status;
@@ -420,9 +419,9 @@ int PythonPlugin::configure(const char *iniFilename,
420419
n++;
421420
}
422421
n = 1;
423-
while ((inistring = inifile.Find("PATH_APPEND", "PYTHON",
424-
n, &lineno))) {
425-
if (inifile.TildeExpansion(*inistring,expandinistring,sizeof(expandinistring))) {
422+
while (auto inistring = inifile.Find("PATH_APPEND", "PYTHON",
423+
n, &lineno)) {
424+
if (inifile.TildeExpansion(inistring->c_str(),expandinistring,sizeof(expandinistring))) {
426425
logPP(-1, "TildeExpansion failed '%s'", toplevel);
427426
status = PLUGIN_EXCEPTION_DURING_PATH_APPEND;
428427
return status;

src/emc/rs274ngc/interp_namedparams.cc

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -964,7 +964,6 @@ double Interp::inicheck()
964964
{
965965
IniFile inifile;
966966
const char *filename;
967-
std::optional<const char*> inistring;
968967
double result = -1.0;
969968

970969
if ((filename = getenv("INI_FILE_NAME")) == NULL) {
@@ -976,8 +975,8 @@ double Interp::inicheck()
976975
return -1.0;
977976
}
978977

979-
if ((inistring = inifile.Find("LINEAR_UNITS", "TRAJ"))) {
980-
if (!strcmp(*inistring, "inch")) {
978+
if (auto inistring = inifile.Find("LINEAR_UNITS", "TRAJ")) {
979+
if (*inistring == "inch") {
981980
result = 0.0;
982981
} else {
983982
result = 1.0;

src/emc/rs274ngc/rs274ngc_pre.cc

Lines changed: 32 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -884,7 +884,6 @@ int Interp::init()
884884
fprintf(stderr,"Unable to open inifile:%s:\n", iniFileName);
885885
} else {
886886
bool opt;
887-
std::optional<const char*> inistring;
888887

889888
inifile.Find(&_setup.tool_change_at_g30, "TOOL_CHANGE_AT_G30", "EMCIO");
890889
inifile.Find(&_setup.tool_change_quill_up, "TOOL_CHANGE_QUILL_UP", "EMCIO");
@@ -920,14 +919,14 @@ int Interp::init()
920919
inifile.Find(&opt, "OWORD_WARNONLY", "RS274NGC");
921920
if (opt) _setup.feature_set |= FEATURE_OWORD_WARNONLY;
922921

923-
if ((inistring =inifile.Find("LOCKING_INDEXER_JOINT", "AXIS_A"))) {
924-
_setup.a_indexer_jnum = atol(*inistring);
922+
if (auto inistring = inifile.Find("LOCKING_INDEXER_JOINT", "AXIS_A")) {
923+
_setup.a_indexer_jnum = atol(inistring->c_str());
925924
}
926-
if ((inistring =inifile.Find("LOCKING_INDEXER_JOINT", "AXIS_B"))) {
927-
_setup.b_indexer_jnum = atol(*inistring);
925+
if (auto inistring = inifile.Find("LOCKING_INDEXER_JOINT", "AXIS_B")) {
926+
_setup.b_indexer_jnum = atol(inistring->c_str());
928927
}
929-
if ((inistring =inifile.Find("LOCKING_INDEXER_JOINT", "AXIS_C"))) {
930-
_setup.c_indexer_jnum = atol(*inistring);
928+
if (auto inistring = inifile.Find("LOCKING_INDEXER_JOINT", "AXIS_C")) {
929+
_setup.c_indexer_jnum = atol(inistring->c_str());
931930
}
932931
inifile.Find(&_setup.orient_offset, "ORIENT_OFFSET", "RS274NGC");
933932
inifile.Find(&_setup.parameter_g73_peck_clearance, "G73_PECK_CLEARANCE", "RS274NGC");
@@ -937,18 +936,18 @@ int Interp::init()
937936

938937
_setup.debugmask |= EMC_DEBUG_UNCONDITIONAL;
939938

940-
if((inistring = inifile.Find("LOG_LEVEL", "RS274NGC")))
939+
if(auto inistring = inifile.Find("LOG_LEVEL", "RS274NGC"))
941940
{
942-
_setup.loggingLevel = atol(*inistring);
941+
_setup.loggingLevel = atol(inistring->c_str());
943942
}
944943

945944
// default the log_file to stderr.
946-
if((inistring = inifile.Find("LOG_FILE", "RS274NGC")))
945+
if(auto inistring = inifile.Find("LOG_FILE", "RS274NGC"))
947946
{
948-
if ((log_file = fopen(*inistring, "a")) == NULL) {
947+
if ((log_file = fopen(inistring->c_str(), "a")) == NULL) {
949948
log_file = stderr;
950949
logDebug( "(%d): Unable to open log file:%s, using stderr",
951-
getpid(), *inistring);
950+
getpid(), inistring->c_str());
952951
}
953952
} else {
954953
log_file = stderr;
@@ -957,38 +956,38 @@ int Interp::init()
957956
_setup.use_lazy_close = 1;
958957

959958
_setup.wizard_root[0] = 0;
960-
if((inistring = inifile.Find("WIZARD_ROOT", "WIZARD")))
959+
if(auto inistring = inifile.Find("WIZARD_ROOT", "WIZARD"))
961960
{
962-
logDebug("[WIZARD]WIZARD_ROOT:%s", *inistring);
963-
if (realpath(*inistring, _setup.wizard_root) == NULL) {
961+
logDebug("[WIZARD]WIZARD_ROOT:%s", inistring->c_str());
962+
if (realpath(inistring->c_str(), _setup.wizard_root) == NULL) {
964963
//realpath didn't find the file
965-
logDebug("realpath failed to find wizard_root:%s:", *inistring);
964+
logDebug("realpath failed to find wizard_root:%s:", inistring->c_str());
966965
}
967966
}
968967
logDebug("_setup.wizard_root:%s:", _setup.wizard_root);
969968

970969
_setup.program_prefix[0] = 0;
971-
if((inistring = inifile.Find("PROGRAM_PREFIX", "DISPLAY")))
970+
if(auto inistring = inifile.Find("PROGRAM_PREFIX", "DISPLAY"))
972971
{
973972
// found it
974973
char expandinistring[LINELEN];
975-
if (inifile.TildeExpansion(*inistring,expandinistring,sizeof(expandinistring))) {
976-
logDebug("TildeExpansion failed for: %s",*inistring);
974+
if (inifile.TildeExpansion(inistring->c_str(),expandinistring,sizeof(expandinistring))) {
975+
logDebug("TildeExpansion failed for: %s",inistring->c_str());
977976
}
978977
if (realpath(expandinistring, _setup.program_prefix) == NULL){
979978
//realpath didn't find the file
980-
logDebug("realpath failed to find program_prefix:%s:", *inistring);
979+
logDebug("realpath failed to find program_prefix:%s:", inistring->c_str());
981980
}
982981
logDebug("program prefix:%s: prefix:%s:",
983-
*inistring, _setup.program_prefix);
982+
inistring->c_str(), _setup.program_prefix);
984983
}
985984
else
986985
{
987986
logDebug("PROGRAM_PREFIX not found");
988987
}
989988
logDebug("_setup.program_prefix:%s:", _setup.program_prefix);
990989

991-
if((inistring = inifile.Find("SUBROUTINE_PATH", "RS274NGC")))
990+
if(auto inistring = inifile.Find("SUBROUTINE_PATH", "RS274NGC"))
992991
{
993992
// found it
994993
int dct;
@@ -999,7 +998,7 @@ int Interp::init()
999998
_setup.subroutines[dct] = NULL;
1000999
}
10011000

1002-
rtapi_strxcpy(tmpdirs,*inistring);
1001+
rtapi_strxcpy(tmpdirs,inistring->c_str());
10031002
nextdir = strtok(tmpdirs,":"); // first token
10041003
dct = 0;
10051004
while (1) {
@@ -1031,15 +1030,15 @@ int Interp::init()
10311030
}
10321031
// subroutine to execute on aborts - for instance to retract
10331032
// toolchange HAL pins
1034-
if ((inistring = inifile.Find("ON_ABORT_COMMAND", "RS274NGC"))) {
1035-
_setup.on_abort_command = strstore(*inistring);
1033+
if (auto inistring = inifile.Find("ON_ABORT_COMMAND", "RS274NGC")) {
1034+
_setup.on_abort_command = strstore(inistring->c_str());
10361035
logDebug("_setup.on_abort_command=%s", _setup.on_abort_command);
10371036
} else {
10381037
_setup.on_abort_command = NULL;
10391038
}
10401039

10411040
// initialize the Python plugin singleton
1042-
if ((inistring = inifile.Find("TOPLEVEL", "PYTHON"))) {
1041+
if (inifile.Find("TOPLEVEL", "PYTHON")) {
10431042
int status = python_plugin->configure(iniFileName,"PYTHON");
10441043
if (status != PLUGIN_OK) {
10451044
Error("Python plugin configure() failed, status = %d", status);
@@ -1051,10 +1050,10 @@ int Interp::init()
10511050
_setup.g_remapped.clear();
10521051
_setup.m_remapped.clear();
10531052
_setup.remaps.clear();
1054-
while ((inistring = inifile.Find("REMAP", "RS274NGC",
1055-
n, &lineno))) {
1053+
while (auto inistring = inifile.Find("REMAP", "RS274NGC",
1054+
n, &lineno)) {
10561055

1057-
CHP(parse_remap( *inistring, lineno));
1056+
CHP(parse_remap( inistring->c_str(), lineno));
10581057
n++;
10591058
}
10601059

@@ -2513,7 +2512,6 @@ VARIABLE_FILE = rs274ngc.var
25132512
int Interp::ini_load(const char *filename)
25142513
{
25152514
IniFile inifile;
2516-
std::optional<const char*> inistring;
25172515

25182516
// open it
25192517
if (inifile.Open(filename) == false) {
@@ -2525,12 +2523,13 @@ int Interp::ini_load(const char *filename)
25252523

25262524

25272525
char parameter_file_name[LINELEN]={};
2528-
if ((inistring = inifile.Find("PARAMETER_FILE", "RS274NGC"))) {
2529-
if (strlen(*inistring) >= sizeof(parameter_file_name)) {
2526+
if (auto inistring = inifile.Find("PARAMETER_FILE", "RS274NGC")) {
2527+
if (inistring->length() >= sizeof(parameter_file_name)) {
25302528
logDebug("%s:[RS274NGC]PARAMETER_FILE is too long (max len %zu)",
25312529
filename, sizeof(parameter_file_name)-1);
25322530
} else {
2533-
strncpy(parameter_file_name, *inistring, sizeof(parameter_file_name));
2531+
strncpy(parameter_file_name, inistring->c_str(), sizeof(parameter_file_name)-1);
2532+
parameter_file_name[sizeof(parameter_file_name)-1] = '\0';
25342533
logDebug("found PARAMETER_FILE:%s:", parameter_file_name);
25352534
}
25362535
} else {

src/emc/sai/driver.cc

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -675,16 +675,15 @@ int main (int argc, char ** argv)
675675
}
676676
_sai._external_length_units = 0.03937007874016;
677677
if (inifile!= 0) {
678-
std::optional<const char*> inistring;
679678
IniFile ini;
680679
// open it
681680
if (ini.Open(inifile) == false) {
682681
fprintf(stderr, "could not open supplied INI file %s\n", inifile);
683682
exit(1);
684683
}
685684

686-
if ((inistring = ini.Find("LINEAR_UNITS", "TRAJ"))) {
687-
if (!strcmp(*inistring, "mm")) {
685+
if (auto inistring = ini.Find("LINEAR_UNITS", "TRAJ")) {
686+
if (*inistring == "mm") {
688687
_sai._external_length_units = 1.0;
689688
}
690689
}

0 commit comments

Comments
 (0)