Skip to content

Commit 9557ae8

Browse files
committed
Updates unixpathname() signature to include output string length, replace strcpy with strlcpy
1 parent d1f7361 commit 9557ae8

File tree

5 files changed

+48
-49
lines changed

5 files changed

+48
-49
lines changed

inc/ufsdefs.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ LispPTR UFS_deletefile(LispPTR *args);
66
LispPTR UFS_renamefile(LispPTR *args);
77
LispPTR UFS_directorynamep(LispPTR *args);
88
#ifdef DOS
9-
int unixpathname(char *src, char *dst, int versionp, int genp, char *drive, int *extlenptr, char *rawname);
9+
int unixpathname(char *src, char *dst, int dstlen, int versionp, int genp, char *drive, int *extlenptr, char *rawname);
1010
#else
11-
int unixpathname(char *src, char *dst, int versionp, int genp);
11+
int unixpathname(char *src, char *dst, size_t dstlen, int versionp, int genp);
1212
#endif
1313
int lisppathname(char *fullname, char *lispname, int dirp, int versionp);
1414
int quote_fname(char *file);

src/dir.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2034,9 +2034,9 @@ LispPTR COM_gen_files(LispPTR *args)
20342034
*/
20352035

20362036
#ifdef DOS
2037-
if (!unixpathname(fbuf, pattern, 1, 1, drive, 0, 0)) {
2037+
if (!unixpathname(fbuf, pattern, sizeof(pattern), 1, 1, drive, 0, 0)) {
20382038
#else
2039-
if (!unixpathname(fbuf, pattern, 1, 1)) {
2039+
if (!unixpathname(fbuf, pattern, sizeof(pattern), 1, 1)) {
20402040
#endif /* DOS */
20412041
/* Yes, always dskp is on */
20422042
return (SMALLP_MINUSONE);

src/dsk.c

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
#include <stdio.h> // for NULL, sprintf, size_t, rename, SEEK_SET
1515
#include <stddef.h> // for ptrdiff_t
1616
#include <stdlib.h> // for strtoul, qsort
17-
#include <string.h> // for strcpy, strcmp, strlen, strncpy, strchr
17+
#include <string.h> // for strlcpy, strcmp, strlen, strncpy, strchr
1818
#include <sys/stat.h> // for stat, fstat, mkdir, S_ISREG, st_atime, chmod
1919
#include <sys/types.h> // for ino_t, time_t, off_t
2020
#include <unistd.h> // for unlink, close, link, lseek, access, chdir
@@ -264,9 +264,9 @@ LispPTR COM_openfile(LispPTR *args)
264264
* convert a version field.
265265
*/
266266
#ifdef DOS
267-
unixpathname(lfname, file, dskp, 0, drive, &extlen, rawname);
267+
unixpathname(lfname, file, sizeof(file), dskp, 0, drive, &extlen, rawname);
268268
#else
269-
unixpathname(lfname, file, dskp, 0);
269+
unixpathname(lfname, file, sizeof(file), dskp, 0);
270270
#endif
271271

272272
/*
@@ -598,8 +598,7 @@ LispPTR COM_closefile(LispPTR *args)
598598
* Convert a Lisp file name to UNIX one. If host is DSK, we also have to
599599
* convert a version field.
600600
*/
601-
dskp ? unixpathname(lfname, file, 1, 0, drive, &extlen, rawname)
602-
: unixpathname(lfname, file, 0, 0, drive, &extlen, rawname);
601+
unixpathname(lfname, file, sizeof(file), dskp, drive, &extlen, rawname);
603602
fd = LispNumToCInt(args[1]);
604603
cdate = (time_t)LispNumToCInt(args[2]);
605604
if (!dskp) {
@@ -721,8 +720,7 @@ LispPTR COM_closefile(LispPTR *args)
721720
* Convert a Lisp file name to UNIX one. If host is DSK, we also have to
722721
* convert a version field.
723722
*/
724-
dskp ? unixpathname(lfname, file, 1, 0) : unixpathname(lfname, file, 0, 0);
725-
723+
unixpathname(lfname, file, sizeof(file), dskp, 0);
726724
fd = LispNumToCInt(args[1]);
727725
cdate = (time_t)LispNumToCInt(args[2]);
728726

@@ -849,9 +847,9 @@ LispPTR DSK_getfilename(LispPTR *args)
849847
* unixpathname specifies it.
850848
*/
851849
#ifdef DOS
852-
if (unixpathname(lfname, file, 1, 0, drive, &extlen, rawname) == 0) return (NIL);
850+
if (unixpathname(lfname, file, sizeof(file), 1, 0, drive, &extlen, rawname) == 0) return (NIL);
853851
#else
854-
if (unixpathname(lfname, file, 1, 0) == 0) return (NIL);
852+
if (unixpathname(lfname, file, sizeof(file), 1, 0) == 0) return (NIL);
855853
#endif
856854

857855
if (unpack_filename(file, dir, name, ver, 1) == 0) return (NIL);
@@ -1120,9 +1118,9 @@ LispPTR DSK_deletefile(LispPTR *args)
11201118
LispStringToCString(args[0], fbuf, MAXPATHLEN);
11211119
#ifdef DOS
11221120
separate_drive(fbuf, drive);
1123-
unixpathname(fbuf, file, 1, 0, drive, &extlen, rawname);
1121+
unixpathname(fbuf, file, sizeof(file), 1, 0, drive, &extlen, rawname);
11241122
#else
1125-
unixpathname(fbuf, file, 1, 0);
1123+
unixpathname(fbuf, file, sizeof(file), 1, 0);
11261124
#endif
11271125

11281126
if (unpack_filename(file, dir, fbuf, ver, 1) == 0) return (NIL);
@@ -1271,17 +1269,17 @@ LispPTR DSK_renamefile(LispPTR *args)
12711269
LispStringToCString(args[0], fbuf, MAXPATHLEN);
12721270
#ifdef DOS
12731271
separate_drive(fbuf, drive1);
1274-
unixpathname(fbuf, src, 1, 0, drive1, &extlen1, rawname1);
1272+
unixpathname(fbuf, src, sizeof(src), 1, 0, drive1, &extlen1, rawname1);
12751273
#else /* DOS */
1276-
unixpathname(fbuf, src, 1, 0);
1274+
unixpathname(fbuf, src, sizeof(src), 1, 0);
12771275
#endif /* DOS */
12781276

12791277
LispStringToCString(args[1], fbuf, MAXPATHLEN);
12801278
#ifdef DOS
12811279
separate_drive(fbuf, drive2);
1282-
unixpathname(fbuf, dst, 1, 0, drive2, &extlen2, rawname2);
1280+
unixpathname(fbuf, dst, sizeof(dst), 1, 0, drive2, &extlen2, rawname2);
12831281
#else /* DOS */
1284-
unixpathname(fbuf, dst, 1, 0);
1282+
unixpathname(fbuf, dst, sizeof(dst), 1, 0);
12851283
#endif /* DOS */
12861284

12871285
if (unpack_filename(dst, dir, fbuf, ver, 1) == 0) return (NIL);
@@ -1492,9 +1490,9 @@ LispPTR DSK_directorynamep(LispPTR *args)
14921490
/* Convert Xerox Lisp file naming convention to Unix one. */
14931491
#ifdef DOS
14941492
separate_drive(dirname, drive);
1495-
if (unixpathname(dirname, fullname, 1, 0, drive, 0, 0) == 0) return (NIL);
1493+
if (unixpathname(dirname, fullname, sizeof(fullname), 1, 0, drive, 0, 0) == 0) return (NIL);
14961494
#else /* DOS*/
1497-
if (unixpathname(dirname, fullname, 1, 0) == 0) return (NIL);
1495+
if (unixpathname(dirname, fullname, sizeof(fullname), 1, 0) == 0) return (NIL);
14981496
#endif /* DOS */
14991497

15001498
if (true_name(fullname) != -1) return (NIL);
@@ -1599,9 +1597,9 @@ LispPTR COM_getfileinfo(LispPTR *args)
15991597
* convert a version field.
16001598
*/
16011599
#ifdef DOS
1602-
unixpathname(lfname, file, dskp, 0, drive, &extlen, rawname);
1600+
unixpathname(lfname, file, sizeof(file), dskp, 0, drive, &extlen, rawname);
16031601
#else /* DOS */
1604-
unixpathname(lfname, file, dskp, 0);
1602+
unixpathname(lfname, file, sizeof(file), dskp, 0);
16051603
#endif /* DOS */
16061604

16071605
/*
@@ -1794,9 +1792,9 @@ LispPTR COM_setfileinfo(LispPTR *args)
17941792
* convert a version field.
17951793
*/
17961794
#ifdef DOS
1797-
unixpathname(lfname, file, dskp, 0, drive, &extlen, rawname);
1795+
unixpathname(lfname, file, sizeof(file), dskp, 0, drive, &extlen, rawname);
17981796
#else /* DOS */
1799-
unixpathname(lfname, file, dskp, 0);
1797+
unixpathname(lfname, file, sizeof(file), dskp, 0);
18001798
#endif /* DOS */
18011799

18021800
/*
@@ -2141,9 +2139,9 @@ LispPTR COM_changedir(LispPTR *args)
21412139
return (NIL);
21422140

21432141
#ifdef DOS
2144-
if (!unixpathname(lfname, dir, 0, 0, drive, 0, 0)) return (NIL);
2142+
if (!unixpathname(lfname, dir, sizeof(dir), 0, 0, drive, 0, 0)) return (NIL);
21452143
#else /* DOS */
2146-
if (!unixpathname(lfname, dir, 0, 0)) return (NIL);
2144+
if (!unixpathname(lfname, dir, sizeof(dir), 0, 0)) return (NIL);
21472145
#endif /* DOS */
21482146

21492147
if (dskp) {
@@ -2241,9 +2239,9 @@ LispPTR COM_getfreeblock(LispPTR *args)
22412239
return (NIL);
22422240

22432241
#ifdef DOS
2244-
if (!unixpathname(lfname, file, 0, 0, drive, 0, 0)) return (NIL);
2242+
if (!unixpathname(lfname, file, sizeof(file), 0, 0, drive, 0, 0)) return (NIL);
22452243
#else /* DOS */
2246-
if (!unixpathname(lfname, file, 0, 0)) return (NIL);
2244+
if (!unixpathname(lfname, file, sizeof(file), 0, 0)) return (NIL);
22472245
#endif /* DOS */
22482246

22492247
if (!unpack_filename(file, dir, name, ver, 0)) return (NIL);

src/ufs.c

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -176,9 +176,9 @@ LispPTR UFS_getfilename(LispPTR *args)
176176
* unixpathname specifies it.
177177
*/
178178
#ifdef DOS
179-
if (unixpathname(lfname, file, 0, 0, 0, 0, 0) == 0) return (NIL);
179+
if (unixpathname(lfname, file, sizeof(file), 0, 0, 0, 0, 0) == 0) return (NIL);
180180
#else
181-
if (unixpathname(lfname, file, 0, 0) == 0) return (NIL);
181+
if (unixpathname(lfname, file, sizeof(file), 0, 0) == 0) return (NIL);
182182
#endif /* DOS */
183183

184184
switch (args[1]) {
@@ -259,9 +259,9 @@ LispPTR UFS_deletefile(LispPTR *args)
259259
LispStringToCString(args[0], fbuf, MAXPATHLEN);
260260

261261
#ifdef DOS
262-
if (unixpathname(fbuf, file, 0, 0, 0, 0, 0) == 0) return (NIL);
262+
if (unixpathname(fbuf, file, sizeof(file), 0, 0, 0, 0, 0) == 0) return (NIL);
263263
#else
264-
if (unixpathname(fbuf, file, 0, 0) == 0) return (NIL);
264+
if (unixpathname(fbuf, file, sizeof(file), 0, 0) == 0) return (NIL);
265265
#endif /* DOS */
266266
/* check if we're operating on directory or file */
267267
TIMEOUT(rval = stat(file, &sbuf));
@@ -327,15 +327,15 @@ LispPTR UFS_renamefile(LispPTR *args)
327327

328328
LispStringToCString(args[0], fbuf, MAXPATHLEN);
329329
#ifdef DOS
330-
if (unixpathname(fbuf, src, 0, 0, 0, 0, 0) == 0) return (NIL);
330+
if (unixpathname(fbuf, src, sizeof(src), 0, 0, 0, 0, 0) == 0) return (NIL);
331331
#else
332-
if (unixpathname(fbuf, src, 0, 0) == 0) return (NIL);
332+
if (unixpathname(fbuf, src, sizeof(src), 0, 0) == 0) return (NIL);
333333
#endif /* DOS */
334334
LispStringToCString(args[1], fbuf, MAXPATHLEN);
335335
#ifdef DOS
336-
if (unixpathname(fbuf, dst, 0, 0, 0, 0, 0) == 0) return (NIL);
336+
if (unixpathname(fbuf, dst, sizeof(dst), 0, 0, 0, 0, 0) == 0) return (NIL);
337337
#else
338-
if (unixpathname(fbuf, dst, 0, 0) == 0) return (NIL);
338+
if (unixpathname(fbuf, dst, sizeof(dst), 0, 0) == 0) return (NIL);
339339
#endif /* DOS */
340340

341341
TIMEOUT(rval = rename(src, dst));
@@ -400,9 +400,9 @@ LispPTR UFS_directorynamep(LispPTR *args)
400400

401401
/* Convert Xerox Lisp file naming convention to Unix one. */
402402
#ifdef DOS
403-
if (unixpathname(dirname, fullname, 0, 0, 0, 0, 0) == 0) return (NIL);
403+
if (unixpathname(dirname, fullname, sizeof(fullname), 0, 0, 0, 0, 0) == 0) return (NIL);
404404
#else
405-
if (unixpathname(dirname, fullname, 0, 0) == 0) return (NIL);
405+
if (unixpathname(dirname, fullname, sizeof(fullname), 0, 0) == 0) return (NIL);
406406
#endif /* DOS */
407407

408408
TIMEOUT(rval = stat(fullname, &sbuf));
@@ -437,6 +437,7 @@ LispPTR UFS_directorynamep(LispPTR *args)
437437
* if the pathname is passed as a directory, the
438438
* tail delimiter may be included.
439439
* char *dst The buffer to which the converted pathname is stored.
440+
* int dstlen The size of the dst buffer
440441
* int versionp
441442
* If 1, version field in src is converted to UNIX
442443
* version form. {DSK} device invokes unixpathname
@@ -463,9 +464,9 @@ LispPTR UFS_directorynamep(LispPTR *args)
463464
*
464465
*/
465466
#ifdef DOS
466-
int unixpathname(char *src, char *dst, int versionp, int genp, char *drive, int *extlenptr, char *rawname)
467+
int unixpathname(char *src, char *dst, int dstlen, int versionp, int genp, char *drive, int *extlenptr, char *rawname)
467468
#else
468-
int unixpathname(char *src, char *dst, int versionp, int genp)
469+
int unixpathname(char *src, char *dst, size_t dstlen, int versionp, int genp)
469470
#endif /* DOS */
470471
{
471472
char *cp, *dp, *np;
@@ -495,12 +496,12 @@ int unixpathname(char *src, char *dst, int versionp, int genp)
495496
* file system code.
496497
*/
497498
if (strcmp(src, "<") == 0) {
498-
strcpy(dst, DIRSEPSTR);
499+
strlcpy(dst, DIRSEPSTR, dstlen);
499500
return (1);
500501
}
501502

502503
/* Copy src to protect it from destructive modification. */
503-
strcpy(lfname, src);
504+
strlcpy(lfname, src, sizeof(lfname));
504505

505506
/*
506507
* If versionp is specified, we have to deal with the version field first,
@@ -582,7 +583,7 @@ int unixpathname(char *src, char *dst, int versionp, int genp)
582583
TIMEOUT0(pwd = getpwuid(getuid()));
583584
if (pwd == NULL) return (0);
584585

585-
strcpy(dst, pwd->pw_dir);
586+
strlcpy(dst, pwd->pw_dir, dstlen);
586587
while (*dp != '\0') dp++;
587588
if (*(dp - 1) != DIRSEP) {
588589
/*
@@ -606,7 +607,7 @@ int unixpathname(char *src, char *dst, int versionp, int genp)
606607
TIMEOUT0(pwd = getpwnam(name));
607608
if (pwd == NULL) return (0);
608609

609-
strcpy(dst, pwd->pw_dir);
610+
strlcpy(dst, pwd->pw_dir, dstlen);
610611
while (*dp != '\0') dp++;
611612
if (*(dp - 1) != DIRSEP) {
612613
/*
@@ -807,8 +808,8 @@ int unixpathname(char *src, char *dst, int versionp, int genp)
807808
* for the convenience of the pattern matching routines, we don't
808809
* care about the last period character.
809810
*/
810-
strcpy(fbuf1, lfname);
811-
strcpy(fbuf2, dst);
811+
strlcpy(fbuf1, lfname, sizeof(fbuf1));
812+
strlcpy(fbuf2, dst, sizeof(fbuf2));
812813
separate_version(fbuf1, ver1, 1);
813814
separate_version(fbuf2, ver2, 1);
814815
for (cp = fbuf1; *cp; cp++) {}

src/vmemsave.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,9 +156,9 @@ LispPTR vmem_save0(LispPTR *args)
156156
LispStringToCString(args[0], pathname, MAXPATHLEN);
157157
separate_host(pathname, host);
158158
#ifdef DOS
159-
if (!unixpathname(pathname, sysout, 0, 0, drive, 0, 0)) return (BADFILENAME);
159+
if (!unixpathname(pathname, sysout, sizeof(sysout), 0, 0, drive, 0, 0)) return (BADFILENAME);
160160
#else
161-
if (!unixpathname(pathname, sysout, 0, 0)) return (BADFILENAME);
161+
if (!unixpathname(pathname, sysout, sizeof(sysout), 0, 0)) return (BADFILENAME);
162162
#endif /* DOS */
163163
return (vmem_save(sysout));
164164
} else {

0 commit comments

Comments
 (0)