Skip to content

Commit f951b38

Browse files
committed
clean up leaks associated with some character constants
1 parent 143e090 commit f951b38

File tree

2 files changed

+52
-13
lines changed

2 files changed

+52
-13
lines changed

c/strutil.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#define CR "\n"
22
#define CRLF "\n\r"
33

4+
#define MAX_WORD_LENGTH 8192 /* Max length of an interpretable word */
45
#define MAX_LINE_LENGTH 8192 /* Max chars in a line */
56
#define MAX_LINES 16384 /* Max number of lines */
67
#define MAX_WORDS 128

c/xdifile.c

Lines changed: 51 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,16 @@ 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, *version_xdi, *version_extra;
98+
char *c, *line, *fullline, *mkey, *mval;
9999
char *reword;
100100
char tlabel[32] = {'\0'};
101101
char comments[1025] = {'\0'};
102102
char elem[3] = {'\0'};
103+
char edge[3] = {'\0'};
104+
char version_xdi[8] = {'\0'};
105+
char version_extra[MAX_LINE_LENGTH] = {'\0'};
106+
char errline[MAX_LINE_LENGTH] = {'\0'};
107+
char outerlabel[MAX_WORD_LENGTH] = {'\0'};
103108
double dval ;
104109
double *outer_arr, outer_arr0;
105110
long *outer_pts;
@@ -116,6 +121,9 @@ XDI_readfile(char *filename, XDIFile *xdifile) {
116121

117122
iret = 0;
118123

124+
125+
/* initialize string attributes of thr XDIFile struct */
126+
119127
strcpy(comments, " ");
120128
xdifile->comments = calloc(1025, sizeof(char));
121129
strcpy(xdifile->comments, comments);
@@ -124,14 +132,43 @@ XDI_readfile(char *filename, XDIFile *xdifile) {
124132
xdifile->element = calloc(3, sizeof(char));
125133
strncpy(xdifile->element, elem, 2);
126134

127-
COPY_STRING(xdifile->xdi_libversion, XDI_VERSION);
128-
COPY_STRING(xdifile->xdi_version, "");
129-
COPY_STRING(xdifile->extra_version, "");
135+
strcpy(edge, " ");
136+
xdifile->edge = calloc(3, sizeof(char));
137+
strncpy(xdifile->edge, edge, 2);
138+
139+
xdifile->xdi_libversion = calloc(8, sizeof(char));
140+
strncpy(xdifile->xdi_libversion, XDI_VERSION, 7);
141+
142+
strcpy(version_xdi, " ");
143+
xdifile->xdi_version = calloc(8, sizeof(char));
144+
strncpy(xdifile->xdi_version, version_xdi, 7);
145+
146+
strcpy(version_extra, " ");
147+
xdifile->extra_version = calloc(MAX_LINE_LENGTH+1, sizeof(char));
148+
strncpy(xdifile->extra_version, version_extra, MAX_LINE_LENGTH);
149+
150+
151+
strcpy(errline, " ");
152+
xdifile->error_line = calloc(MAX_LINE_LENGTH+1, sizeof(char));
153+
strncpy(xdifile->error_line, errline, MAX_LINE_LENGTH);
154+
155+
strcpy(outerlabel, " ");
156+
xdifile->outer_label = calloc(MAX_LINE_LENGTH+1, sizeof(char));
157+
strncpy(xdifile->outer_label, outerlabel, MAX_LINE_LENGTH);
158+
159+
160+
/* COPY_STRING(xdifile->xdi_libversion, XDI_VERSION); */
161+
/* COPY_STRING(xdifile->xdi_version, ""); */
162+
/* COPY_STRING(xdifile->extra_version, ""); */
130163
/* COPY_STRING(xdifile->element, "__"); */
131-
COPY_STRING(xdifile->edge, "_");
164+
/* COPY_STRING(xdifile->edge, "_"); */
132165
/* COPY_STRING(xdifile->comments, comments); */
133-
COPY_STRING(xdifile->error_line, "");
134-
COPY_STRING(xdifile->outer_label, "");
166+
/* COPY_STRING(xdifile->error_line, ""); */
167+
/* COPY_STRING(xdifile->outer_label, ""); */
168+
169+
170+
/* initialize numeric attributes of thr XDIFile struct */
171+
135172
xdifile->nouter = 1;
136173
xdifile->error_lineno = -1;
137174
xdifile->dspacing = -1.0;
@@ -158,7 +195,7 @@ XDI_readfile(char *filename, XDIFile *xdifile) {
158195
return ilen;
159196
}
160197

161-
/* check fist line for XDI header, get version info */
198+
/* check first line for XDI header, get version info */
162199
if (strncmp(textlines[0], TOK_COMM, 1) == 0) {
163200
line = textlines[0]; line++;
164201
line[strcspn(line, CRLF)] = '\0';
@@ -168,10 +205,10 @@ XDI_readfile(char *filename, XDIFile *xdifile) {
168205
return ERR_NOTXDI;
169206
} else {
170207
line = line+5;
171-
COPY_STRING(xdifile->xdi_version, line)
208+
strcpy(xdifile->xdi_version, line);
172209
}
173210
if (nwords > 1) { /* extra version tags */
174-
COPY_STRING(xdifile->extra_version, cwords[1]);
211+
strcpy(xdifile->extra_version, cwords[1]);
175212
}
176213
}
177214

@@ -245,7 +282,7 @@ XDI_readfile(char *filename, XDIFile *xdifile) {
245282
} else if (strcasecmp(mkey, TOK_EDGE) == 0) {
246283
for (j = 0; j < n_edges; j++) {
247284
if (strcasecmp(ValidEdges[j], mval) == 0) {
248-
COPY_STRING(xdifile->edge, mval);
285+
strcpy(xdifile->edge, mval);
249286
break;
250287
}
251288
}
@@ -396,7 +433,7 @@ XDI_readfile(char *filename, XDIFile *xdifile) {
396433
}
397434
/* success */
398435
xdifile->error_lineno = 0;
399-
COPY_STRING(xdifile->error_line, "");
436+
strcpy(xdifile->error_line, "");
400437

401438
xdifile->npts = ipt;
402439
xdifile->nouter = iouter;
@@ -437,7 +474,8 @@ _EXPORT(int) XDI_get_array_name(XDIFile *xdifile, char *name, double *out) {
437474
return ERR_NOARR_NAME;
438475
}
439476

440-
_EXPORT(void) XDI_cleanup(XDIFile *xdifile) {
477+
_EXPORT(void)
478+
XDI_cleanup(XDIFile *xdifile) {
441479
/* one needs to explicitly free each part of the struct */
442480
long j;
443481
for (j = 0; j < xdifile->narrays; j++) {

0 commit comments

Comments
 (0)