Skip to content

Commit dfb820c

Browse files
committed
Replace sprintf() with snprintf()
1 parent 11b81f6 commit dfb820c

File tree

3 files changed

+43
-43
lines changed

3 files changed

+43
-43
lines changed

src/dir.c

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
#define alarm(x) 1
2121
#endif /* DOS */
2222
#include <errno.h> // for errno, EINTR, ENOENT
23-
#include <stdio.h> // for NULL, sprintf, size_t
23+
#include <stdio.h> // for NULL, snprintf, size_t
2424
#include <stdlib.h> // for calloc, free, strtoul, malloc, qsort
2525
#include <string.h> // for strcpy, strcmp, strlen, strrchr, strcat
2626
#include <sys/stat.h> // for stat, S_ISDIR, st_atime, st_mtime
@@ -441,11 +441,11 @@ static int enum_dsk_prop(char *dir, char *name, char *ver, FINFO **finfo_buf)
441441
nextp->next = prevp;
442442
if (isslash) {
443443
if (drive)
444-
sprintf(namebuf, "%c:\\%s", drive, dirp.name);
444+
snprintf(namebuf, sizeof(namebuf), "%c:\\%s", drive, dirp.name);
445445
else
446-
sprintf(namebuf, "\\%s", dirp.name);
446+
snprintf(namebuf, sizeof(namebuf), "\\%s", dirp.name);
447447
} else
448-
sprintf(namebuf, "%s\\%s", dir, dirp.name);
448+
snprintf(namebuf, sizeof(namebuf), "%s\\%s", dir, dirp.name);
449449

450450
TIMEOUT(rval = stat(namebuf, &sbuf));
451451
if (rval == -1 && errno != ENOENT) {
@@ -511,11 +511,11 @@ static int enum_dsk_prop(char *dir, char *name, char *ver, FINFO **finfo_buf)
511511

512512
if (isslash) {
513513
if (drive)
514-
sprintf(namebuf, "%c:\\%s", drive, old);
514+
snprintf(namebuf, sizeof(namebuf), "%c:\\%s", drive, old);
515515
else
516-
sprintf(namebuf, "\\%s", old);
516+
snprintf(namebuf, sizeof(namebuf), "\\%s", old);
517517
} else
518-
sprintf(namebuf, "%s\\%s", dir, old);
518+
snprintf(namebuf, sizeof(namebuf), "%s\\%s", dir, old);
519519
TIMEOUT(rval = stat(namebuf, &sbuf));
520520

521521
if (rval == -1) continue;
@@ -524,7 +524,7 @@ static int enum_dsk_prop(char *dir, char *name, char *ver, FINFO **finfo_buf)
524524
newp->next = prevp;
525525
/* All other types than directory. */
526526
newp->dirp = 0;
527-
sprintf(namebuf, "%s.~00~", nextp->no_ver_name);
527+
snprintf(namebuf, sizeof(namebuf), "%s.~00~", nextp->no_ver_name);
528528
quote_fname(namebuf);
529529
len = strlen(namebuf);
530530
strcpy(newp->lname, namebuf);
@@ -582,7 +582,7 @@ static int enum_dsk_prop(char *dir, char *name, char *ver, FINFO **finfo_buf)
582582
return (-1);
583583
}
584584
nextp->next = prevp;
585-
sprintf(namebuf, "%s/%s", dir, dp->d_name);
585+
snprintf(namebuf, sizeof(namebuf), "%s/%s", dir, dp->d_name);
586586
TIMEOUT(rval = stat(namebuf, &sbuf));
587587
if (rval == -1 && errno != ENOENT) {
588588
/*
@@ -720,11 +720,11 @@ static int enum_dsk(char *dir, char *name, char *ver, FINFO **finfo_buf)
720720
nextp->next = prevp;
721721
if (isslash) {
722722
if (drive)
723-
sprintf(namebuf, "%c:\\%s", drive, dirp.name);
723+
snprintf(namebuf, sizeof(namebuf), "%c:\\%s", drive, dirp.name);
724724
else
725-
sprintf(namebuf, "\\%s", dirp.name);
725+
snprintf(namebuf, sizeof(namebuf), "\\%s", dirp.name);
726726
} else
727-
sprintf(namebuf, "%s\\%s", dir, dirp.name);
727+
snprintf(namebuf, sizeof(namebuf), "%s\\%s", dir, dirp.name);
728728
TIMEOUT(rval = stat(namebuf, &sbuf));
729729
if (rval == -1 && errno != ENOENT) {
730730
/*
@@ -776,11 +776,11 @@ static int enum_dsk(char *dir, char *name, char *ver, FINFO **finfo_buf)
776776

777777
if (isslash) {
778778
if (drive)
779-
sprintf(namebuf, "%c:\\%s", drive, old);
779+
snprintf(namebuf, sizeof(namebuf), "%c:\\%s", drive, old);
780780
else
781-
sprintf(namebuf, "\\%s", old);
781+
snprintf(namebuf, sizeof(namebuf), "\\%s", old);
782782
} else
783-
sprintf(namebuf, "%s\\%s", dir, old);
783+
snprintf(namebuf, sizeof(namebuf), "%s\\%s", dir, old);
784784
TIMEOUT(rval = stat(namebuf, &sbuf));
785785

786786
if (rval == -1) continue;
@@ -789,7 +789,7 @@ static int enum_dsk(char *dir, char *name, char *ver, FINFO **finfo_buf)
789789
newp->next = prevp;
790790
/* All other types than directory. */
791791
newp->dirp = 0;
792-
sprintf(namebuf, "%s.~00~", nextp->no_ver_name);
792+
snprintf(namebuf, sizeof(namebuf), "%s.~00~", nextp->no_ver_name);
793793
quote_fname(namebuf);
794794
len = strlen(namebuf);
795795
strcpy(newp->lname, namebuf);
@@ -845,7 +845,7 @@ static int enum_dsk(char *dir, char *name, char *ver, FINFO **finfo_buf)
845845
return (-1);
846846
}
847847
nextp->next = prevp;
848-
sprintf(namebuf, "%s/%s", dir, dp->d_name);
848+
snprintf(namebuf, sizeof(namebuf), "%s/%s", dir, dp->d_name);
849849
TIMEOUT(rval = stat(namebuf, &sbuf));
850850
if (rval == -1 && errno != ENOENT) {
851851
/*
@@ -949,7 +949,7 @@ static int enum_ufs_prop(char *dir, char *name, char *ver, FINFO **finfo_buf)
949949
return (-1);
950950
}
951951
nextp->next = prevp;
952-
sprintf(namebuf, "%s\\%s", dir, dirp.name);
952+
snprintf(namebuf, sizeof(namebuf), "%s\\%s", dir, dirp.name);
953953
TIMEOUT(rval = stat(namebuf, &sbuf));
954954
if (rval == -1 && errno != ENOENT) {
955955
/*
@@ -1040,7 +1040,7 @@ static int enum_ufs_prop(char *dir, char *name, char *ver, FINFO **finfo_buf)
10401040
return (-1);
10411041
}
10421042
nextp->next = prevp;
1043-
sprintf(namebuf, "%s/%s", dir, dp->d_name);
1043+
snprintf(namebuf, sizeof(namebuf), "%s/%s", dir, dp->d_name);
10441044
TIMEOUT(rval = stat(namebuf, &sbuf));
10451045
if (rval == -1 && errno != ENOENT) {
10461046
/*
@@ -1148,7 +1148,7 @@ static int enum_ufs(char *dir, char *name, char *ver, FINFO **finfo_buf)
11481148
return (-1);
11491149
}
11501150
nextp->next = prevp;
1151-
sprintf(namebuf, "%s\\%s", dir, dirp.name);
1151+
snprintf(namebuf, sizeof(namebuf), "%s\\%s", dir, dirp.name);
11521152
TIMEOUT(rval = stat(namebuf, &sbuf));
11531153
if (rval == -1 && errno != ENOENT) {
11541154
/*
@@ -1223,7 +1223,7 @@ static int enum_ufs(char *dir, char *name, char *ver, FINFO **finfo_buf)
12231223
return (-1);
12241224
}
12251225
nextp->next = prevp;
1226-
sprintf(namebuf, "%s/%s", dir, dp->d_name);
1226+
snprintf(namebuf, sizeof(namebuf), "%s/%s", dir, dp->d_name);
12271227
TIMEOUT(rval = stat(namebuf, &sbuf));
12281228
if (rval == -1 && errno != ENOENT) {
12291229
/*
@@ -1336,7 +1336,7 @@ static int trim_finfo(FINFO **fp)
13361336
* Versionless is not linked to any versioned
13371337
* file.
13381338
*/
1339-
sprintf(ver, ";%u", mp->version + 1);
1339+
snprintf(ver, sizeof(ver), ";%u", mp->version + 1);
13401340
strcat(sp->lname, ver);
13411341
sp->lname_len = strlen(sp->lname);
13421342
pnum = ++num;
@@ -1464,7 +1464,7 @@ static int trim_finfo_highest(FINFO **fp, int highestp)
14641464
* Versionless is not linked to any versioned
14651465
* file.
14661466
*/
1467-
sprintf(ver, ";%u", mp->version + 1);
1467+
snprintf(ver, sizeof(ver), ";%u", mp->version + 1);
14681468
strcat(sp->lname, ver);
14691469
sp->lname_len = strlen(sp->lname);
14701470
/*
@@ -1646,7 +1646,7 @@ static int trim_finfo_version(FINFO **fp, unsigned rver)
16461646
* file.
16471647
*/
16481648
if (mp->version + 1 == rver) {
1649-
sprintf(ver, ";%u", rver);
1649+
snprintf(ver, sizeof(ver), ";%u", rver);
16501650
strcat(sp->lname, ver);
16511651
sp->lname_len = strlen(sp->lname);
16521652
/*

src/dsk.c

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
#include <errno.h> // for errno, EINTR, ENOENT, ENFILE, EPERM
1313
#include <fcntl.h> // for O_RDWR, O_CREAT, open, O_RDONLY, O_TRUNC
14-
#include <stdio.h> // for NULL, sprintf, size_t, rename, SEEK_SET
14+
#include <stdio.h> // for NULL, snprintf, size_t, rename, SEEK_SET
1515
#include <stddef.h> // for ptrdiff_t
1616
#include <stdlib.h> // for strtoul, qsort
1717
#include <string.h> // for strlcpy, strcmp, strlen, strncpy, strchr
@@ -662,7 +662,7 @@ LispPTR COM_closefile(LispPTR *args)
662662
}
663663

664664
for (; rval == 0; S_TOUT(rval = _dos_findnext(&dirp))) {
665-
sprintf(file, "%s\\%s", dir, dirp.name);
665+
snprintf(file, sizeof(file), "%s\\%s", dir, dirp.name);
666666
}
667667
}
668668
#ifndef DOS /* effectively NEVER, since we're in an ifdef DOS */
@@ -2358,7 +2358,7 @@ void separate_version(char *name, char *ver, int checkp)
23582358
* Use strtoul() to eliminate leading 0s.
23592359
*/
23602360
ver_no = strtoul(start + 1, (char **)NULL, 10);
2361-
sprintf(ver_buf, "%u", ver_no);
2361+
snprintf(ver_buf, sizeof(ver_buf), "%u", ver_no);
23622362
strlcpy(ver, ver_buf, sizeof(ver));
23632363
return;
23642364
} else {
@@ -2638,7 +2638,7 @@ static int locate_file(char *dir, char *name)
26382638
struct direct *dp;
26392639

26402640
/* First of all, recognize as if. */
2641-
sprintf(path, "%s\\%s", dir, name);
2641+
snprintf(path, sizeof(path), "%s\\%s", dir, name);
26422642
DIR_OR_FILE_P(path, type);
26432643
if (type != 0) {
26442644
strlcpy(dir, path, sizeof(dir));
@@ -2657,7 +2657,7 @@ static int locate_file(char *dir, char *name)
26572657
struct dirent *dp;
26582658

26592659
/* First of all, recognize as if. */
2660-
sprintf(path, "%s/%s", dir, name);
2660+
snprintf(path, sizeof(path), "%s/%s", dir, name);
26612661
DIR_OR_FILE_P(path, type);
26622662
if (type != 0) {
26632663
strlcpy(dir, path, sizeof(dir));
@@ -2667,7 +2667,7 @@ static int locate_file(char *dir, char *name)
26672667
/* Next try with all lower case name. */
26682668
strlcpy(nb1, name, sizeof(nb1));
26692669
DOWNCASE(nb1);
2670-
sprintf(path, "%s/%s", dir, nb1);
2670+
snprintf(path, sizeof(path), "%s/%s", dir, nb1);
26712671
DIR_OR_FILE_P(path, type);
26722672
if (type != 0) {
26732673
strlcpy(dir, path, sizeof(dir));
@@ -2676,7 +2676,7 @@ static int locate_file(char *dir, char *name)
26762676

26772677
/* Next try with all upper case name. */
26782678
UPCASE(nb1);
2679-
sprintf(path, "%s/%s", dir, nb1);
2679+
snprintf(path, sizeof(path), "%s/%s", dir, nb1);
26802680
DIR_OR_FILE_P(path, type);
26812681
if (type != 0) {
26822682
strlcpy(dir, path, sizeof(dir));
@@ -2698,7 +2698,7 @@ static int locate_file(char *dir, char *name)
26982698
strlcpy(nb2, dp->d_name, sizeof(nb2));
26992699
UPCASE(nb2);
27002700
if (strcmp(nb1, nb2) == 0) {
2701-
sprintf(path, "%s/%s", dir, dp->d_name);
2701+
snprintf(path, sizeof(path), "%s/%s", dir, dp->d_name);
27022702
DIR_OR_FILE_P(path, type);
27032703
if (type != 0) {
27042704
strlcpy(dir, path, sizeof(dir));
@@ -3329,7 +3329,7 @@ static int maintain_version(char *file, int forcep)
33293329
* is versioned one higher than the existing highest version.
33303330
*/
33313331
FindHighestVersion(VA.files, entry, max_no);
3332-
sprintf(ver, "%u", max_no + 1);
3332+
snprintf(ver, sizeof(ver), "%u", max_no + 1);
33333333
/*
33343334
* The old file should have the same case name as the versionless
33353335
* file.
@@ -3653,7 +3653,7 @@ static int get_old(char *dir, FileName *varray, char *afile, char *vfile)
36533653
* link missing versionless file.
36543654
*/
36553655
FindHighestVersion(varray, entry, max_no);
3656-
sprintf(vbuf, "%u", max_no + 1);
3656+
snprintf(vbuf, sizeof(vbuf), "%u", max_no + 1);
36573657
conc_name_and_version(vless, vbuf, vfile, sizeof(vfile));
36583658
strlcpy(afile, vless, sizeof(afile));
36593659
return (1);
@@ -3668,7 +3668,7 @@ static int get_old(char *dir, FileName *varray, char *afile, char *vfile)
36683668
* is dealt with as a version of the link
36693669
* missing versionless file.
36703670
*/
3671-
sprintf(vbuf, "%u", ver_no);
3671+
snprintf(vbuf, sizeof(vbuf), "%u", ver_no);
36723672
conc_name_and_version(vless, vbuf, vfile, sizeof(vfile));
36733673
strlcpy(afile, vless, sizeof(afile));
36743674
return (1);
@@ -3864,7 +3864,7 @@ static int get_oldest(char *dir, FileName *varray, char *afile, char *vfile)
38643864
* is dealt with as a version of the link
38653865
* missing versionless file.
38663866
*/
3867-
sprintf(vbuf, "%u", ver_no);
3867+
snprintf(vbuf, sizeof(vbuf), "%u", ver_no);
38683868
conc_name_and_version(vless, vbuf, vfile, sizeof(vfile));
38693869
strlcpy(afile, vless, sizeof(afile));
38703870
return (1);
@@ -4009,7 +4009,7 @@ static int get_new(char *dir, FileName *varray, char *afile, char *vfile)
40094009
* the existing highest version.
40104010
*/
40114011
FindHighestVersion(varray, entry, max_no);
4012-
sprintf(vbuf, "%u", max_no + 1);
4012+
snprintf(vbuf, sizeof(vbuf), "%u", max_no + 1);
40134013
/*
40144014
* We will use the file name of the existing highest
40154015
* versioned file as the name of the new file, so that
@@ -4101,7 +4101,7 @@ static int get_new(char *dir, FileName *varray, char *afile, char *vfile)
41014101
* missing versionless file.
41024102
*/
41034103
FindHighestVersion(varray, entry, max_no);
4104-
sprintf(vbuf, "%u", max_no + 2);
4104+
snprintf(vbuf, sizeof(vbuf), "%u", max_no + 2);
41054105
conc_name_and_version(vless, vbuf, vfile, sizeof(vfile));
41064106
strlcpy(afile, vfile, sizeof(afile));
41074107
return (1);
@@ -4116,7 +4116,7 @@ static int get_new(char *dir, FileName *varray, char *afile, char *vfile)
41164116
* is dealt with as a version of the link
41174117
* missing versionless file.
41184118
*/
4119-
sprintf(vbuf, "%u", ver_no);
4119+
snprintf(vbuf, sizeof(vbuf), "%u", ver_no);
41204120
conc_name_and_version(vless, vbuf, vfile, sizeof(vfile));
41214121
strlcpy(afile, vless, sizeof(afile));
41224122
return (1);
@@ -4164,7 +4164,7 @@ static int get_new(char *dir, FileName *varray, char *afile, char *vfile)
41644164
* new file.
41654165
*/
41664166
FindHighestVersion(varray, entry, max_no);
4167-
sprintf(vbuf, "%u", max_no + 1);
4167+
snprintf(vbuf, sizeof(vbuf), "%u", max_no + 1);
41684168
/*
41694169
* We will use the name of the highest versioned file
41704170
* as the name of the new file.
@@ -4380,7 +4380,7 @@ static int get_old_new(char *dir, FileName *varray, char *afile, char *vfile)
43804380
* link missing versionless file.
43814381
*/
43824382
FindHighestVersion(varray, entry, max_no);
4383-
sprintf(vbuf, "%u", max_no + 1);
4383+
snprintf(vbuf, sizeof(vbuf), "%u", max_no + 1);
43844384
conc_name_and_version(vless, vbuf, vfile, sizeof(vfile));
43854385
strlcpy(afile, vless, sizeof(afile));
43864386
return (1);
@@ -4395,7 +4395,7 @@ static int get_old_new(char *dir, FileName *varray, char *afile, char *vfile)
43954395
* is dealt with as a version of the link
43964396
* missing versionless file.
43974397
*/
4398-
sprintf(vbuf, "%u", ver_no);
4398+
snprintf(vbuf, sizeof(vbuf), "%u", ver_no);
43994399
conc_name_and_version(vless, vbuf, vfile, sizeof(vfile));
44004400
strlcpy(afile, vless, sizeof(afile));
44014401
return (1);

src/vmemsave.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ LispPTR vmem_save(char *sysout_file_name)
351351
/* Bloddy 8 char filenames in dos ... /jarl */
352352
make_old_version(tempname, sysout_file_name);
353353
#else /* DOS */
354-
sprintf(tempname, "%s-temp", sysout_file_name);
354+
snprintf(tempname, sizeof(tempname), "%s-temp", sysout_file_name);
355355
#endif /* DOS */
356356

357357
/* Confirm protection of specified file by open/close */

0 commit comments

Comments
 (0)