Skip to content

Commit a8e6f84

Browse files
committed
all files in data/ pass valgrind
1 parent f951b38 commit a8e6f84

File tree

3 files changed

+71
-43
lines changed

3 files changed

+71
-43
lines changed

c/strutil.c

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,11 @@ int readlines(char *filename, char **textlines) {
3333
as char *text[MAX] */
3434

3535
FILE *finp;
36-
char *thisline ;
37-
char *text, *c;
36+
char *thisline;
37+
char *text;
3838
long file_length, index, i, ilen;
3939
int is_newline;
40+
char *orig_text, *orig_thisline;
4041

4142
finp = fopen(filename, "r");
4243
if (finp == NULL) {
@@ -50,6 +51,9 @@ int readlines(char *filename, char **textlines) {
5051

5152
text = calloc(file_length + 1, sizeof(char));
5253
thisline = calloc(MAX_LINE_LENGTH, sizeof(char));
54+
/* need to save a pointers to the beginning of the original strings so they can be freed at the end of this function */
55+
orig_text = text;
56+
orig_thisline = thisline;
5357

5458
if (text == NULL ) {
5559
printf("\nnot enough memory to read file.\n");
@@ -83,6 +87,11 @@ int readlines(char *filename, char **textlines) {
8387
return -EFBIG;
8488
}
8589
}
90+
/* free strings */
91+
text = orig_text;
92+
thisline = orig_thisline;
93+
free(text);
94+
free(thisline);
8695
return ilen;
8796
}
8897

c/xdi_reader.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ int main(int argc, char **argv) {
8282
}
8383
printf("\n");
8484
}
85+
free(tdat);
8586
XDI_cleanup(xdifile);
8687
return 0;
8788
}

c/xdifile.c

Lines changed: 59 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ XDI_readfile(char *filename, XDIFile *xdifile) {
9595
char *header[MAX_LINES];
9696
char *words[MAX_WORDS], *cwords[2];
9797
char *col_labels[MAX_COLUMNS], *col_units[MAX_COLUMNS];
98-
char *c, *line, *fullline, *mkey, *mval;
98+
char *c, *line, *firstline, *interline, *fullline, *mkey, *mval;
9999
char *reword;
100100
char tlabel[32] = {'\0'};
101101
char comments[1025] = {'\0'};
@@ -156,17 +156,6 @@ XDI_readfile(char *filename, XDIFile *xdifile) {
156156
xdifile->outer_label = calloc(MAX_LINE_LENGTH+1, sizeof(char));
157157
strncpy(xdifile->outer_label, outerlabel, MAX_LINE_LENGTH);
158158

159-
160-
/* COPY_STRING(xdifile->xdi_libversion, XDI_VERSION); */
161-
/* COPY_STRING(xdifile->xdi_version, ""); */
162-
/* COPY_STRING(xdifile->extra_version, ""); */
163-
/* COPY_STRING(xdifile->element, "__"); */
164-
/* COPY_STRING(xdifile->edge, "_"); */
165-
/* COPY_STRING(xdifile->comments, comments); */
166-
/* COPY_STRING(xdifile->error_line, ""); */
167-
/* COPY_STRING(xdifile->outer_label, ""); */
168-
169-
170159
/* initialize numeric attributes of thr XDIFile struct */
171160

172161
xdifile->nouter = 1;
@@ -181,8 +170,10 @@ XDI_readfile(char *filename, XDIFile *xdifile) {
181170

182171
for (i = 0; i < MAX_COLUMNS; i++) {
183172
sprintf(tlabel, "col%ld", i+1);
184-
COPY_STRING(col_labels[i], tlabel);
185-
COPY_STRING(col_units[i], "");
173+
/* COPY_STRING(col_labels[i], tlabel); */
174+
/* COPY_STRING(col_units[i], ""); */
175+
col_labels[i] = tlabel;
176+
col_units[i] = "";
186177
}
187178

188179
/* read file to text lines: an array of trimmed strings */
@@ -197,15 +188,15 @@ XDI_readfile(char *filename, XDIFile *xdifile) {
197188

198189
/* check first line for XDI header, get version info */
199190
if (strncmp(textlines[0], TOK_COMM, 1) == 0) {
200-
line = textlines[0]; line++;
201-
line[strcspn(line, CRLF)] = '\0';
202-
nwords = make_words(line, cwords, 2);
191+
firstline = textlines[0]; firstline++;
192+
firstline[strcspn(firstline, CRLF)] = '\0';
193+
nwords = make_words(firstline, cwords, 2);
203194
if (nwords < 1) { return ERR_NOTXDI; }
204195
if (strncasecmp(cwords[0], TOK_VERSION, strlen(TOK_VERSION)) != 0) {
205196
return ERR_NOTXDI;
206197
} else {
207-
line = line+5;
208-
strcpy(xdifile->xdi_version, line);
198+
firstline = firstline+5;
199+
strcpy(xdifile->xdi_version, firstline);
209200
}
210201
if (nwords > 1) { /* extra version tags */
211202
strcpy(xdifile->extra_version, cwords[1]);
@@ -227,23 +218,25 @@ XDI_readfile(char *filename, XDIFile *xdifile) {
227218
xdifile->meta_keywords = calloc(nheader, sizeof(char *));
228219
xdifile->meta_values = calloc(nheader, sizeof(char *));
229220

230-
231221
mode = 0; /* metadata (Family.Member: Value) mode */
232222
for (i = 1; i < nheader; i++) {
233223
xdifile->error_lineno = i;
234-
COPY_STRING(xdifile->error_line, textlines[i]);
224+
strcpy(xdifile->error_line, textlines[i]);
235225

236226
if (strncmp(textlines[i], TOK_COMM, 1) == 0) {
237-
COPY_STRING(line, textlines[i]);
238-
COPY_STRING(fullline, textlines[i]);
227+
/* COPY_STRING(line, textlines[i]); */
228+
/* COPY_STRING(fullline, textlines[i]); */
229+
line = textlines[i];
230+
fullline = textlines[i];
239231
line++;
240232
fullline++;
241233
nwords = split_on(line, TOK_DELIM, words);
242234
if (nwords < 1) { continue; }
243235
COPY_STRING(mkey, words[0]);
244236

245237
if ((mode==0) && (nwords == 2)) { /* metadata */
246-
COPY_STRING(mval, words[1]);
238+
/* COPY_STRING(mval, words[1]); */
239+
mval = words[1];
247240
nwords = split_on(words[0], TOK_DOT, words);
248241
if (nwords > 1) {
249242
ndict++;
@@ -254,20 +247,23 @@ XDI_readfile(char *filename, XDIFile *xdifile) {
254247
*/
255248
regex_status = slre_match(1, FAMILYNAME, words[0], strlen(words[0]));
256249
if (regex_status != NULL) {
250+
free(mval);
257251
return ERR_META_FAMNAME;
258252
}
259253

260254
regex_status = slre_match(1, KEYNAME, words[1], strlen(words[1]));
261255
if (regex_status != NULL) {
256+
free(mval);
262257
return ERR_META_KEYNAME;
263258
}
264259

265260
COPY_STRING(xdifile->meta_families[ndict], words[0]);
266261
COPY_STRING(xdifile->meta_keywords[ndict], words[1]);
267262
} else {
263+
free(mval);
268264
return ERR_META_FORMAT;
269265
}
270-
/* printf(" metadata: %d %s %s\n", ndict, mkey, mval); */
266+
/* printf(" metadata: %ld | %s | %s\n", ndict, mkey, mval); */
271267
/* ndict, words[0], words[1], xdifile->meta_values[ndict]);*/
272268
if (strncasecmp(mkey, TOK_COLUMN, strlen(TOK_COLUMN)) == 0) {
273269
j = atoi(mkey+7)-1;
@@ -297,19 +293,20 @@ XDI_readfile(char *filename, XDIFile *xdifile) {
297293
}
298294
/* MONO D-SPACING */
299295
} else if (strcasecmp(mkey, TOK_DSPACE) == 0) {
300-
if (0 != xdi_strtod(mval, &dval)) { return ERR_NONNUMERIC;}
296+
if (0 != xdi_strtod(mval, &dval)) { free(mval); return ERR_NONNUMERIC;}
301297
xdifile->dspacing = dval;
302298
/* OUTER ARRAY NAME */
303299
} else if (strcasecmp(mkey, TOK_OUTER_NAME) == 0) {
304-
COPY_STRING(xdifile->outer_label, mval);
300+
strcpy(xdifile->outer_label, mval);
305301
/* OUTER ARRAY VALUE */
306302
} else if (strcasecmp(mkey, TOK_OUTER_VAL) == 0) {
307-
if (0 != xdi_strtod(mval, &dval)) { return ERR_NONNUMERIC;}
303+
if (0 != xdi_strtod(mval, &dval)) { free(mval); return ERR_NONNUMERIC;}
308304
outer_arr0 = dval ;
309305
} else if (strcasecmp(mkey, TOK_TIMESTAMP) == 0) {
310306
j = xdi_is_datestring(mval);
311-
if (0 != j) return j;
307+
if (0 != j) { free(mval); return j;}
312308
}
309+
/* free(mval); */
313310
} else if (strncasecmp(mkey, TOK_USERCOM_0, strlen(TOK_USERCOM_0)) == 0) {
314311
mode = 1;
315312
} else if (strncasecmp(mkey, TOK_USERCOM_1, strlen(TOK_USERCOM_1)) == 0) {
@@ -326,6 +323,7 @@ XDI_readfile(char *filename, XDIFile *xdifile) {
326323
} else if (mode == 0) {
327324
return ERR_META_FORMAT;
328325
}
326+
free(mkey);
329327
} else {
330328
if ((ignored_headerline < 0) && (has_minusline == 0)) {
331329
ignored_headerline = i;
@@ -384,6 +382,7 @@ XDI_readfile(char *filename, XDIFile *xdifile) {
384382
}
385383
}
386384

385+
387386
/* check for mono d-spacing if angle is given but not energy*/
388387
if ((has_angle == 1) && (has_energy == 0) && (xdifile->dspacing < 0)) {
389388
iret = ERR_NODSPACE;
@@ -392,7 +391,7 @@ XDI_readfile(char *filename, XDIFile *xdifile) {
392391
/* set size of data arrays */
393392
xdifile->array = calloc(ncols, sizeof(double *));
394393
for (j = 0; j < ncols; j++) {
395-
xdifile->array[j] = calloc(npts_, sizeof(double));
394+
xdifile->array[j] = calloc(npts_+1, sizeof(double));
396395
if (0 != xdi_strtod(words[j], &dval)) { return ERR_NONNUMERIC;}
397396
xdifile->array[j][0] = dval;
398397
}
@@ -402,35 +401,48 @@ XDI_readfile(char *filename, XDIFile *xdifile) {
402401
iouter = 1;
403402
for (i = nheader-2; i <= ilen; i++) {
404403
/* may find a header line interspersed in array data */
405-
COPY_STRING(line, textlines[i]);
404+
/* COPY_STRING(line, textlines[i]); */
405+
interline = textlines[i];
406406
xdifile->error_lineno = i;
407-
COPY_STRING(xdifile->error_line, line);
407+
strcpy(xdifile->error_line, interline);
408408

409409
if (strncmp(textlines[i], TOK_COMM, 1) == 0) {
410-
line++;
411-
nwords = split_on(line, TOK_DELIM, words);
410+
interline++;
411+
nwords = split_on(interline, TOK_DELIM, words);
412412
if (nwords < 2) { continue; }
413413
COPY_STRING(mkey, words[0]);
414414
if (strcasecmp(mkey, TOK_OUTER_VAL) == 0) {
415-
if (0 != xdi_strtod(words[1], &dval)) { return ERR_NONNUMERIC;}
415+
if (0 != xdi_strtod(words[1], &dval)) {
416+
free(outer_arr);
417+
free(outer_pts);
418+
return ERR_NONNUMERIC;
419+
}
416420
outer_arr[iouter] = dval;
417421
outer_pts[iouter] = ipt;
418422
++iouter;
419423
}
424+
free(mkey);
420425
} else {
421-
COPY_STRING(line, textlines[i]);
422-
icol = make_words(line, words, MAX_WORDS);
426+
/* COPY_STRING(line, textlines[i]); */
427+
icol = make_words(textlines[i], words, MAX_WORDS);
423428
if (icol != ncols) {
429+
free(outer_arr);
430+
free(outer_pts);
424431
return ERR_NCOLS_CHANGE;
425432
}
426433
icol = min(ncols, icol);
427434
for (j = 0; j < icol; j++) {
428-
if (0 != xdi_strtod(words[j], &dval)) { return ERR_NONNUMERIC;}
429-
xdifile->array[j][ipt] = dval ;
435+
if (0 != xdi_strtod(words[j], &dval)) {
436+
free(outer_arr);
437+
free(outer_pts);
438+
return ERR_NONNUMERIC;
439+
}
440+
xdifile->array[j][ipt] = dval;
430441
}
431442
++ipt;
432443
}
433444
}
445+
434446
/* success */
435447
xdifile->error_lineno = 0;
436448
strcpy(xdifile->error_line, "");
@@ -446,7 +458,13 @@ XDI_readfile(char *filename, XDIFile *xdifile) {
446458
xdifile->outer_array[j] = outer_arr[j];
447459
xdifile->outer_breakpts[j] = outer_pts[j];
448460
}
449-
461+
free(line);
462+
free(outer_arr);
463+
free(outer_pts);
464+
/* this was the space used to hold the lines of the file, allocated in readlines */
465+
for (j=0; j<=ilen; j++) {
466+
free(textlines[j]);
467+
}
450468
return iret;
451469

452470
}
@@ -456,7 +474,7 @@ _EXPORT(int) XDI_get_array_index(XDIFile *xdifile, long n, double *out) {
456474
long j;
457475
if (n < xdifile->narrays) {
458476
for (j = 0; j < xdifile->npts; j++) {
459-
out[j] = xdifile->array[n][j] ;
477+
out[j] = xdifile->array[n][j];
460478
}
461479
return 0;
462480
}

0 commit comments

Comments
 (0)