Skip to content

Commit 78e99b9

Browse files
committed
now handling allocation/freeing of col_labels correctly
1 parent f713ce0 commit 78e99b9

File tree

3 files changed

+61
-14
lines changed

3 files changed

+61
-14
lines changed

baddata/BadFiles.txt

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ bad_07.xdi file read(2) Y No Column labels -
1414
bad_08.xdi file read(2) Y Too few Column labels
1515
bad_09.xdi file read(2) Y Too many Column labels
1616
bad_10.xdi file read(2) Y Column indices not continuous
17-
bad_11.xdi error msg Y Unsupported comment character
17+
bad_11.xdi file read(3) Y Unsupported comment character
1818
bad_12.xdi file read(1) Y angle given without mono.d_spacing
1919
bad_13.xdi error msg Y inconsistent number of columns in data table -- too few
2020
bad_14.xdi error msg Y inconsistent number of columns in data table -- too many
@@ -26,17 +26,18 @@ bad_19.xdi error msg Y Header 'Family.Key: Value' -- no ':'
2626
bad_20.xdi error msg Y Header 'Family.Key: Value' -- two ':'
2727
bad_21.xdi error msg Y Header 'Family.Key: Value' -- no '.'
2828
bad_22.xdi error msg Y Header 'Family.Key: Value' -- two '.'
29-
bad_23.xdi file read(3) Y Header 'Family.Key: Value' -- key starts with number
29+
bad_23.xdi file read(4) Y Header 'Family.Key: Value' -- key starts with number
3030
bad_24.xdi error msg Y Header 'Family.Key: Value' -- Family starts with number
31-
bad_25.xdi file read(4) Y No extra version given
32-
bad_26.xdi file read(5) Y No user comment
33-
bad_27.xdi file read(5) Y Line of '////', but no user comment
31+
bad_25.xdi file read(5) Y No extra version given
32+
bad_26.xdi file read(6) Y No user comment
33+
bad_27.xdi file read(6) Y Line of '////', but no user comment
3434
bad_28.xdi error msg Y incorrectly format date-time
3535
bad_29.xdi error msg Y date-time has invalid range (month > 12)
3636

3737
Notes:
3838
1. files read, return value > 0, indicating Warning
3939
2. extra column labels ignored, unspecified column labels set to 'colN' (N=1...)
40-
3. keys are allowed to start with a number...
41-
4. extra version is left as empty string ''
42-
5. user comment is left as empty string ''
40+
3. lines with unrecognized comment characters ignored
41+
4. keys are allowed to start with a number...
42+
5. extra version is left as empty string ''
43+
6. user comment is left as empty string ''

c/test_valgrind.pl

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
use strict;
44
use warnings;
5-
use Test::More tests => 45;
5+
use Test::More tests => 90;
66

77
## good data
88
foreach my $file (qw(co_metal_rt.xdi
@@ -21,17 +21,27 @@
2121
se_znse_rt.xdi
2222
zn_znse_rt.xdi
2323
)) {
24-
my $command = "valgrind --track-origins=yes --leak-check=full --show-leak-kinds=all ./xdi_reader ../baddata/$file 2>&1";
24+
my $command = "valgrind --track-origins=yes --leak-check=full --show-leak-kinds=all ./xdi_reader ../data/$file 2>&1";
2525
my $x = `$command`;
2626
ok(($x =~ m{All heap blocks were freed}), $file);
27+
ok((not $?), "$file return value is 0");
2728
};
2829

30+
31+
## see baddata/BadFile.txt for explanations of return values
32+
my %return = ('00' => 0, '01' => 1, '02' => 0, '03' => 0, '04' => 0, '05' => 0,
33+
'06' => 0, '07' => 0, '08' => 0, '09' => 0, '10' => 0, '11' => 0,
34+
'12' => 0, '13' => 1, '14' => 1, '15' => 1, '16' => 1, '17' => 1,
35+
'18' => 1, '19' => 1, '20' => 1, '21' => 1, '22' => 1, '23' => 0,
36+
'24' => 1, '25' => 0, '26' => 0, '27' => 0, '28' => 1, '29' => 1, );
37+
2938
## bad data
3039
foreach my $i (0 .. 29) {
3140
my $n = sprintf("%2.2d", $i);
3241
my $command = "valgrind --track-origins=yes --leak-check=full --show-leak-kinds=all ./xdi_reader ../baddata/bad_$n.xdi 2>&1";
3342
my $x = `$command`;
3443
ok(($x =~ m{All heap blocks were freed}), "bad_$n.xdi");
44+
ok((not ($? xor $return{$n})), "bad_$n.xdi return value is $?");
3545
};
3646

3747

c/xdifile.c

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,6 @@ XDI_readfile(char *filename, XDIFile *xdifile) {
148148
xdifile->extra_version = calloc(MAX_LINE_LENGTH+1, sizeof(char));
149149
strncpy(xdifile->extra_version, version_extra, MAX_LINE_LENGTH);
150150

151-
152151
strcpy(errline, " ");
153152
xdifile->error_line = calloc(MAX_LINE_LENGTH+1, sizeof(char));
154153
strncpy(xdifile->error_line, errline, MAX_LINE_LENGTH);
@@ -171,9 +170,9 @@ XDI_readfile(char *filename, XDIFile *xdifile) {
171170

172171
for (i = 0; i < MAX_COLUMNS; i++) {
173172
sprintf(tlabel, "col%ld", i+1);
174-
/* COPY_STRING(col_labels[i], tlabel); */
173+
COPY_STRING(col_labels[i], tlabel);
175174
/* COPY_STRING(col_units[i], ""); */
176-
col_labels[i] = tlabel;
175+
/* col_labels[i] = tlabel; */
177176
col_units[i] = "";
178177
}
179178

@@ -196,12 +195,18 @@ XDI_readfile(char *filename, XDIFile *xdifile) {
196195
for (j=0; j<=ilen; j++) {
197196
free(textlines[j]);
198197
}
198+
for (i = 0; i < MAX_COLUMNS; i++) {
199+
free(col_labels[i]);
200+
}
199201
return ERR_NOTXDI;
200202
}
201203
if (strncasecmp(cwords[0], TOK_VERSION, strlen(TOK_VERSION)) != 0) {
202204
for (j=0; j<=ilen; j++) {
203205
free(textlines[j]);
204206
}
207+
for (i = 0; i < MAX_COLUMNS; i++) {
208+
free(col_labels[i]);
209+
}
205210
return ERR_NOTXDI;
206211
} else {
207212
firstline = firstline+5;
@@ -261,6 +266,9 @@ XDI_readfile(char *filename, XDIFile *xdifile) {
261266
for (k=0; k<=ilen; k++) {
262267
free(textlines[k]);
263268
}
269+
for (i = 0; i < MAX_COLUMNS; i++) {
270+
free(col_labels[i]);
271+
}
264272
return ERR_META_FAMNAME;
265273
}
266274

@@ -271,6 +279,9 @@ XDI_readfile(char *filename, XDIFile *xdifile) {
271279
for (k=0; k<=ilen; k++) {
272280
free(textlines[k]);
273281
}
282+
for (i = 0; i < MAX_COLUMNS; i++) {
283+
free(col_labels[i]);
284+
}
274285
return ERR_META_KEYNAME;
275286
}
276287

@@ -282,6 +293,9 @@ XDI_readfile(char *filename, XDIFile *xdifile) {
282293
for (k=0; k<=ilen; k++) {
283294
free(textlines[k]);
284295
}
296+
for (i = 0; i < MAX_COLUMNS; i++) {
297+
free(col_labels[i]);
298+
}
285299
return ERR_META_FORMAT;
286300
}
287301
/* printf(" metadata: %ld | %s | %s\n", ndict, mkey, mval); */
@@ -290,7 +304,8 @@ XDI_readfile(char *filename, XDIFile *xdifile) {
290304
j = atoi(mkey+7)-1;
291305
if ((j > -1) && (j < MAX_COLUMNS)) {
292306
ncols = make_words(mval, cwords, 2);
293-
col_labels[j] = cwords[0];
307+
free(col_labels[j]);
308+
COPY_STRING(col_labels[j], cwords[0]);
294309
if (ncols == 2) {
295310
col_units[j] = cwords[1];
296311
}
@@ -320,6 +335,9 @@ XDI_readfile(char *filename, XDIFile *xdifile) {
320335
for (k=0; k<=ilen; k++) {
321336
free(textlines[k]);
322337
}
338+
for (i = 0; i < MAX_COLUMNS; i++) {
339+
free(col_labels[i]);
340+
}
323341
return ERR_NONNUMERIC;
324342
}
325343
xdifile->dspacing = dval;
@@ -338,6 +356,9 @@ XDI_readfile(char *filename, XDIFile *xdifile) {
338356
for (k=0; k<=ilen; k++) {
339357
free(textlines[k]);
340358
}
359+
for (i = 0; i < MAX_COLUMNS; i++) {
360+
free(col_labels[i]);
361+
}
341362
return j;
342363
}
343364
}
@@ -361,6 +382,9 @@ XDI_readfile(char *filename, XDIFile *xdifile) {
361382
for (k=0; k<=ilen; k++) {
362383
free(textlines[k]);
363384
}
385+
for (i = 0; i < MAX_COLUMNS; i++) {
386+
free(col_labels[i]);
387+
}
364388
return ERR_META_FORMAT;
365389
}
366390
free(mkey);
@@ -441,6 +465,9 @@ XDI_readfile(char *filename, XDIFile *xdifile) {
441465
for (j=0; j<=ilen; j++) {
442466
free(textlines[j]);
443467
}
468+
for (i = 0; i < MAX_COLUMNS; i++) {
469+
free(col_labels[i]);
470+
}
444471
return ERR_NONNUMERIC;
445472
}
446473
xdifile->array[j][0] = dval;
@@ -484,6 +511,9 @@ XDI_readfile(char *filename, XDIFile *xdifile) {
484511
for (j=0; j<=ilen; j++) {
485512
free(textlines[j]);
486513
}
514+
for (i = 0; i < MAX_COLUMNS; i++) {
515+
free(col_labels[i]);
516+
}
487517
return ERR_NCOLS_CHANGE;
488518
}
489519
icol = min(ncols, icol);
@@ -497,6 +527,9 @@ XDI_readfile(char *filename, XDIFile *xdifile) {
497527
for (j=0; j<=ilen; j++) {
498528
free(textlines[j]);
499529
}
530+
for (i = 0; i < MAX_COLUMNS; i++) {
531+
free(col_labels[i]);
532+
}
500533
return ERR_NONNUMERIC;
501534
}
502535
xdifile->array[j][ipt] = dval;
@@ -527,6 +560,9 @@ XDI_readfile(char *filename, XDIFile *xdifile) {
527560
for (j=0; j<=ilen; j++) {
528561
free(textlines[j]);
529562
}
563+
for (i = 0; i < MAX_COLUMNS; i++) {
564+
free(col_labels[i]);
565+
}
530566
return iret;
531567

532568
}

0 commit comments

Comments
 (0)