Skip to content

Commit 4129720

Browse files
committed
Use reentrant version of strtok(), i.e. strtok_r(), to get tokens
1 parent 2fdc614 commit 4129720

File tree

6 files changed

+30
-30
lines changed

6 files changed

+30
-30
lines changed

matlab/libsvmread.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ void read_problem(const char *filename, int nlhs, mxArray *plhs[])
5959
int max_index, min_index, inst_max_index;
6060
size_t elements, k, i, l=0;
6161
FILE *fp = fopen(filename,"r");
62-
char *endptr;
62+
char *endptr, *r = (char *)NULL;
6363
mwIndex *ir, *jc;
6464
double *labels, *samples;
6565

@@ -83,11 +83,11 @@ void read_problem(const char *filename, int nlhs, mxArray *plhs[])
8383
int index = 0;
8484

8585
inst_max_index = -1; // strtol gives 0 if wrong format, and precomputed kernel has <index> start from 0
86-
strtok(line," \t"); // label
86+
strtok_r(line," \t",&r); // label
8787
while (1)
8888
{
89-
idx = strtok(NULL,":"); // index:value
90-
val = strtok(NULL," \t");
89+
idx = strtok_r(NULL,":",&r); // index:value
90+
val = strtok_r(NULL," \t",&r);
9191
if(val == NULL)
9292
break;
9393

@@ -131,7 +131,7 @@ void read_problem(const char *filename, int nlhs, mxArray *plhs[])
131131

132132
readline(fp);
133133

134-
label = strtok(line," \t\n");
134+
label = strtok_r(line," \t\n",&r);
135135
if(label == NULL)
136136
{
137137
mexPrintf("Empty line at line %d\n",i+1);
@@ -149,8 +149,8 @@ void read_problem(const char *filename, int nlhs, mxArray *plhs[])
149149
// features
150150
while(1)
151151
{
152-
idx = strtok(NULL,":");
153-
val = strtok(NULL," \t");
152+
idx = strtok_r(NULL,":",&r);
153+
val = strtok_r(NULL," \t",&r);
154154
if(val == NULL)
155155
break;
156156

matlab/svmpredict.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -297,12 +297,12 @@ void mexFunction( int nlhs, mxArray *plhs[],
297297
if(nrhs==4)
298298
{
299299
int i, argc = 1;
300-
char cmd[CMD_LEN], *argv[CMD_LEN/2];
300+
char cmd[CMD_LEN], *argv[CMD_LEN/2], *r = (char *)NULL;
301301

302302
// put options in argv[]
303303
mxGetString(prhs[3], cmd, mxGetN(prhs[3]) + 1);
304-
if((argv[argc] = strtok(cmd, " ")) != NULL)
305-
while((argv[++argc] = strtok(NULL, " ")) != NULL)
304+
if((argv[argc] = strtok_r(cmd, " ", &r)) != NULL)
305+
while((argv[++argc] = strtok_r(NULL, " ", &r)) != NULL)
306306
;
307307

308308
for(i=1;i<argc;i++)

matlab/svmtrain.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ int parse_command_line(int nrhs, const mxArray *prhs[], char *model_file_name)
109109
{
110110
int i, argc = 1;
111111
char cmd[CMD_LEN];
112-
char *argv[CMD_LEN/2];
112+
char *argv[CMD_LEN/2], *r = (char *)NULL;
113113
void (*print_func)(const char *) = print_string_matlab; // default printing to matlab display
114114

115115
// default values
@@ -137,8 +137,8 @@ int parse_command_line(int nrhs, const mxArray *prhs[], char *model_file_name)
137137
{
138138
// put options in argv[]
139139
mxGetString(prhs[2], cmd, mxGetN(prhs[2]) + 1);
140-
if((argv[argc] = strtok(cmd, " ")) != NULL)
141-
while((argv[++argc] = strtok(NULL, " ")) != NULL)
140+
if((argv[argc] = strtok_r(cmd, " ", &r)) != NULL)
141+
while((argv[++argc] = strtok_r(NULL, " ", &r)) != NULL)
142142
;
143143
}
144144

svm-predict.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,10 @@ void predict(FILE *input, FILE *output)
7777
{
7878
int i = 0;
7979
double target_label, predict_label;
80-
char *idx, *val, *label, *endptr;
80+
char *idx, *val, *label, *endptr, *r = (char *)NULL;
8181
int inst_max_index = -1; // strtol gives 0 if wrong format, and precomputed kernel has <index> start from 0
8282

83-
label = strtok(line," \t\n");
83+
label = strtok_r(line," \t\n",&r);
8484
if(label == NULL) // empty line
8585
exit_input_error(total+1);
8686

@@ -96,8 +96,8 @@ void predict(FILE *input, FILE *output)
9696
x = (struct svm_node *) realloc(x,max_nr_attr*sizeof(struct svm_node));
9797
}
9898

99-
idx = strtok(NULL,":");
100-
val = strtok(NULL," \t");
99+
idx = strtok_r(NULL,":",&r);
100+
val = strtok_r(NULL," \t",&r);
101101

102102
if(val == NULL)
103103
break;

svm-train.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ void read_problem(const char *filename)
281281
size_t elements, j;
282282
FILE *fp = fopen(filename,"r");
283283
char *endptr;
284-
char *idx, *val, *label;
284+
char *idx, *val, *label, *r = (char *)NULL;
285285

286286
if(fp == NULL)
287287
{
@@ -296,12 +296,12 @@ void read_problem(const char *filename)
296296
line = Malloc(char,max_line_len);
297297
while(readline(fp)!=NULL)
298298
{
299-
char *p = strtok(line," \t"); // label
299+
char *p = strtok_r(line," \t", &r); // label
300300

301301
// features
302302
while(1)
303303
{
304-
p = strtok(NULL," \t");
304+
p = strtok_r(NULL," \t", &r);
305305
if(p == NULL || *p == '\n') // check '\n' as ' ' may be after the last feature
306306
break;
307307
++elements;
@@ -322,7 +322,7 @@ void read_problem(const char *filename)
322322
inst_max_index = -1; // strtol gives 0 if wrong format, and precomputed kernel has <index> start from 0
323323
readline(fp);
324324
prob.x[i] = &x_space[j];
325-
label = strtok(line," \t\n");
325+
label = strtok_r(line," \t\n", &r);
326326
if(label == NULL) // empty line
327327
exit_input_error(i+1);
328328

@@ -332,8 +332,8 @@ void read_problem(const char *filename)
332332

333333
while(1)
334334
{
335-
idx = strtok(NULL,":");
336-
val = strtok(NULL," \t");
335+
idx = strtok_r(NULL,":",&r);
336+
val = strtok_r(NULL," \t",&r);
337337

338338
if(val == NULL)
339339
break;

svm.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2914,14 +2914,14 @@ svm_model *svm_load_model(const char *model_file_name)
29142914

29152915
max_line_len = 1024;
29162916
line = Malloc(char,max_line_len);
2917-
char *p,*endptr,*idx,*val;
2917+
char *p,*endptr,*idx,*val,*r = (char *)NULL;;
29182918

29192919
while(readline(fp)!=NULL)
29202920
{
2921-
p = strtok(line,":");
2921+
p = strtok_r(line,":",&r);
29222922
while(1)
29232923
{
2924-
p = strtok(NULL,":");
2924+
p = strtok_r(NULL,":",&r);
29252925
if(p == NULL)
29262926
break;
29272927
++elements;
@@ -2947,18 +2947,18 @@ svm_model *svm_load_model(const char *model_file_name)
29472947
readline(fp);
29482948
model->SV[i] = &x_space[j];
29492949

2950-
p = strtok(line, " \t");
2950+
p = strtok_r(line, " \t", &r);
29512951
model->sv_coef[0][i] = strtod(p,&endptr);
29522952
for(int k=1;k<m;k++)
29532953
{
2954-
p = strtok(NULL, " \t");
2954+
p = strtok_r(NULL, " \t", &r);
29552955
model->sv_coef[k][i] = strtod(p,&endptr);
29562956
}
29572957

29582958
while(1)
29592959
{
2960-
idx = strtok(NULL, ":");
2961-
val = strtok(NULL, " \t");
2960+
idx = strtok_r(NULL, ":", &r);
2961+
val = strtok_r(NULL, " \t", &r);
29622962

29632963
if(val == NULL)
29642964
break;

0 commit comments

Comments
 (0)