Skip to content

Commit acb9333

Browse files
committed
improve and expand on pr 187
We improve on the good checks of PR #187. Improve error messages and checks for `getscanfield()`. Improve error messages and checks for `fscanfile()`. Prevent NULL from being passed to `makestring(str)`.
1 parent 81e3cb1 commit acb9333

File tree

3 files changed

+32
-17
lines changed

3 files changed

+32
-17
lines changed

CHANGES

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ The following are the changes from calc version 3.0.0.2 to date:
44

55
Cygwin disable a gcc warning as per PR #186.
66

7+
Improve error messages and checks for `getscanfield()`.
8+
Improve error messages and checks for `fscanfile()`.
9+
Prevent NULL from being passed to `makestring(str)`.
10+
711

812
The following are the changes from calc version 3.0.0.1 to 3.0.0.1:
913

file.c

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2067,18 +2067,15 @@ showfiles(void)
20672067
static void
20682068
getscanfield(FILE *fp, bool skip, unsigned int width, int scannum, char *scanptr, char **strptr)
20692069
{
2070-
char *str; /* current string */
2071-
unsigned long len; /* current length of string */
2072-
unsigned long totlen; /* total length of string */
2073-
char buf[READSIZE]; /* temporary buffer */
2070+
char *str = NULL; /* current string */
2071+
unsigned long len; /* current length of string */
2072+
unsigned long totlen = 0; /* total length of string */
2073+
char buf[READSIZE]; /* temporary buffer */
20742074
int c;
20752075
char *b;
20762076
bool comp; /* Use complement of scanset */
20772077
unsigned int chnum;
20782078

2079-
totlen = 0;
2080-
str = NULL;
2081-
20822079
comp = (scannum < 0);
20832080
if (comp) {
20842081
scannum = -scannum;
@@ -2111,12 +2108,18 @@ getscanfield(FILE *fp, bool skip, unsigned int width, int scannum, char *scanptr
21112108
}
21122109
if (!skip) {
21132110
if (totlen) {
2114-
str = (char *)realloc(str, totlen + len + 1);
2111+
if (str == NULL) {
2112+
/* paranoia */
2113+
math_error("getscanfield: str was NULL while totlen != 0");
2114+
not_reached();
2115+
} else {
2116+
str = (char *)realloc(str, totlen + len + 1);
2117+
}
21152118
} else {
21162119
str = (char *)calloc(len + 1, 1);
21172120
}
21182121
if (str == NULL) {
2119-
math_error("Out of memory for scanning");
2122+
math_error("getscanfield: Out of memory for scanning");
21202123
not_reached();
21212124
}
21222125
if (len) {
@@ -2233,8 +2236,8 @@ fscanfile(FILE *fp, char *fmt, int count, VALUE **vals)
22332236
int scannum; /* Number of characters in scanlist */
22342237
char *scanptr; /* Start of scanlist */
22352238
char *str = NULL;
2236-
bool comp; /* True scanset is complementary */
2237-
bool skip; /* True if string to be skipped rather than read */
2239+
bool comp = false; /* True scanset is complementary */
2240+
bool skip = false; /* True if string to be skipped rather than read */
22382241
int width;
22392242
VALUE *var; /* lvalue to be assigned to */
22402243
unsigned short subtype; /* for var->v_subtype */
@@ -2333,7 +2336,8 @@ fscanfile(FILE *fp, char *fmt, int count, VALUE **vals)
23332336
assnum++;
23342337
var = *vals++;
23352338
if (var->v_type != V_ADDR) {
2336-
math_error("This should not happen!!");
2339+
math_error("fscanfile: i case and var->v_type != V_ADDR");
2340+
not_reached();
23372341
}
23382342
var = var->v_addr;
23392343
subtype = var->v_subtype;
@@ -2347,7 +2351,8 @@ fscanfile(FILE *fp, char *fmt, int count, VALUE **vals)
23472351
var = *vals++;
23482352
count--;
23492353
if (var->v_type != V_ADDR) {
2350-
math_error("This should not happen!!");
2354+
math_error("fscanfile: n case and var->v_type != V_ADDR");
2355+
not_reached();
23512356
}
23522357
var = var->v_addr;
23532358
subtype = var->v_subtype;
@@ -2367,12 +2372,18 @@ fscanfile(FILE *fp, char *fmt, int count, VALUE **vals)
23672372
var = *vals++;
23682373
count--;
23692374
if (var->v_type != V_ADDR) {
2370-
math_error("Assigning to non-variable");
2375+
math_error("fscanfile: assigning to non-variable");
2376+
not_reached();
23712377
}
23722378
var = var->v_addr;
23732379
subtype = var->v_subtype;
23742380
freevalue(var);
23752381
var->v_type = V_STR;
2382+
if (str == NULL) {
2383+
/* paranoia */
2384+
math_error("fscanfile: getscanfield not called and/or str is NULL");
2385+
not_reached();
2386+
}
23762387
var->v_str = makestring(str);
23772388
}
23782389
}

str.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1246,10 +1246,10 @@ makestring(char *str)
12461246
STRING *s;
12471247
size_t len;
12481248

1249-
/* Ensure that str is allocated. */
12501249
if (str == NULL) {
1251-
/* Empty string.*/
1252-
str = calloc(1, sizeof (*str));
1250+
/* paranoia */
1251+
math_error("makestring called with NULL str");
1252+
not_reached();
12531253
}
12541254
len = strlen(str);
12551255
s = stralloc();

0 commit comments

Comments
 (0)