Skip to content

Commit

Permalink
Replace uses of atoi() with strtol() or stroul() as appropriate (#394)
Browse files Browse the repository at this point in the history
As a side-effect of this change, we also resolve a a problem with
signed file version numbers, so that instead of version 2147483647
wrapping to -2147483648 we can go as far as 4294967296 before we
have issues.  Various sprintf() formats get changed from %d to %u.
The DOS version code is left behind as int versions.
  • Loading branch information
nbriggs authored Sep 8, 2021
1 parent 85c4ebf commit 4f79f55
Show file tree
Hide file tree
Showing 10 changed files with 125 additions and 76 deletions.
2 changes: 1 addition & 1 deletion inc/lispver1.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
NumericStringP(vp, YES, NO); \
NO: *vp = 0; \
YES: \
if ((*vp)) ver = atoi(vp); \
if ((*vp)) ver = strtol(vp, (char **)NULL, 10); \
else ver = -1; \
} \
else ver = -1; \
Expand Down
10 changes: 5 additions & 5 deletions inc/lispver2.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
\
register char *lv_cp; \
register char *lv_vp; \
register int lv_ver; \
register unsigned lv_ver; \
char lv_ver_buf[VERSIONLEN]; \
\
lv_cp = pathname; \
Expand Down Expand Up @@ -48,20 +48,20 @@
/* \
* Convert the remaining field to digit. \
*/ \
lv_ver = atoi(lv_vp + 1); \
if (lv_ver == 0) { \
lv_ver = strtoul(lv_vp + 1, (char **)NULL, 10); \
if (lv_ver == 0) { \
/* versionless */ \
*lv_vp = 0; \
} else { \
sprintf(lv_ver_buf, ".~%d~", lv_ver); \
sprintf(lv_ver_buf, ".~%u~", lv_ver); \
*lv_vp = 0; \
strcat(pathname, lv_ver_buf); \
} \
goto CONT; \
\
NO: \
strcpy(lv_ver_buf, lv_vp + 1); \
strcat(lv_ver_buf, "~"); \
strcat(lv_ver_buf, "~"); \
*lv_vp++ = '.'; \
*lv_vp++ = '~'; \
*lv_vp = 0; \
Expand Down
8 changes: 4 additions & 4 deletions inc/locfile.h
Original file line number Diff line number Diff line change
Expand Up @@ -385,9 +385,9 @@ extern DLword *Lisp_world; /* To access LispSysout area */
*(start - 1) = ';'; \
*start = '\0'; \
*end = '\0'; \
/* call ato i to eliminate leading 0s. */ \
ver_no = atoi(start + 1); \
sprintf(ver_buf, "%d", ver_no); \
/* call strtoul() to eliminate leading 0s. */ \
ver_no = strtoul(start + 1, (char **)NULL, 10); \
sprintf(ver_buf, "%u", ver_no); \
strcat(pathname, ver_buf); \
goto CONT; \
\
Expand Down Expand Up @@ -494,7 +494,7 @@ extern DLword *Lisp_world; /* To access LispSysout area */

#define MAXVERSION 999999999

#define LASTVERSIONARRAY (-1)
#define LASTVERSIONARRAY ((unsigned) -1)
#define VERSIONARRAYLENGTH 200

#define NoFileP(varray) \
Expand Down
14 changes: 7 additions & 7 deletions src/dir.c
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ void print_finfo(FINFO *fp)
if (fp != (FINFO *)NULL) {
do {
printf("%s -> ", fp->lname);
printf("%d\n", fp->version);
printf("%u\n", fp->version);
fp = fp->next;
} while (fp != (FINFO *)NULL && fp != sp);

Expand Down Expand Up @@ -685,7 +685,7 @@ static int enum_dsk_prop(char *dir, char *name, char *ver, FINFO **finfo_buf)
if (*fver == '\0')
nextp->version = 0;
else
nextp->version = atoi(fver);
nextp->version = strtoul(fver, (char **)NULL, 10);
nextp->ino = sbuf.st_ino;
nextp->prop->length = (unsigned)sbuf.st_size;
nextp->prop->wdate = (unsigned)ToLispTime(sbuf.st_mtime);
Expand Down Expand Up @@ -948,7 +948,7 @@ static int enum_dsk(char *dir, char *name, char *ver, FINFO **finfo_buf)
if (*fver == '\0')
nextp->version = 0;
else
nextp->version = atoi(fver);
nextp->version = strtoul(fver, (char **)NULL, 10);
nextp->ino = sbuf.st_ino;
n++;
}
Expand Down Expand Up @@ -1399,7 +1399,7 @@ static int trim_finfo(FINFO **fp)
* Versionless is not linked to any versioned
* file.
*/
sprintf(ver, ";%d", mp->version + 1);
sprintf(ver, ";%u", mp->version + 1);
strcat(sp->lname, ver);
sp->lname_len = strlen(sp->lname);
pnum = ++num;
Expand Down Expand Up @@ -1527,7 +1527,7 @@ static int trim_finfo_highest(FINFO **fp, int highestp)
* Versionless is not linked to any versioned
* file.
*/
sprintf(ver, ";%d", mp->version + 1);
sprintf(ver, ";%u", mp->version + 1);
strcat(sp->lname, ver);
sp->lname_len = strlen(sp->lname);
/*
Expand Down Expand Up @@ -1709,7 +1709,7 @@ static int trim_finfo_version(FINFO **fp, int rver)
* file.
*/
if (mp->version + 1 == rver) {
sprintf(ver, ";%d", rver);
sprintf(ver, ";%u", rver);
strcat(sp->lname, ver);
sp->lname_len = strlen(sp->lname);
/*
Expand Down Expand Up @@ -2111,7 +2111,7 @@ LispPTR COM_gen_files(register LispPTR *args)

if (*ver != '\0') {
highestp = 0;
version = atoi(ver);
version = strtoul(ver, (char **)NULL, 10);
if (version > 0) strcpy(ver, "*");
} else {
version = 0;
Expand Down
Loading

0 comments on commit 4f79f55

Please sign in to comment.