add warn_refs for mktemp(), tempnam(), and tmpnam() since most code

use these incorrectly. mkstemp() is ok however. inspired by openbsd
This commit is contained in:
lukem 1997-03-16 05:00:38 +00:00
parent b132875e6e
commit 1960b7137d
3 changed files with 38 additions and 18 deletions

View File

@ -1,4 +1,4 @@
/* $NetBSD: mktemp.c,v 1.5 1995/02/02 02:10:09 jtc Exp $ */
/* $NetBSD: mktemp.c,v 1.6 1997/03/16 05:00:38 lukem Exp $ */
/*
* Copyright (c) 1987, 1993
@ -37,7 +37,7 @@
#if 0
static char sccsid[] = "@(#)mktemp.c 8.1 (Berkeley) 6/4/93";
#endif
static char rcsid[] = "$NetBSD: mktemp.c,v 1.5 1995/02/02 02:10:09 jtc Exp $";
static char rcsid[] = "$NetBSD: mktemp.c,v 1.6 1997/03/16 05:00:38 lukem Exp $";
#endif /* LIBC_SCCS and not lint */
#include <sys/types.h>
@ -59,11 +59,21 @@ mkstemp(path)
return (_gettemp(path, &fd) ? fd : -1);
}
char *
_mktemp(path)
char *path;
{
return (_gettemp(path, (int *)NULL) ? path : (char *)NULL);
}
__warn_references(mktemp,
"warning: mktemp() possibly used unsafely, consider using mkstemp()");
char *
mktemp(path)
char *path;
{
return(_gettemp(path, (int *)NULL) ? path : (char *)NULL);
return (_mktemp(path));
}
static int
@ -93,10 +103,10 @@ _gettemp(path, doopen)
if (*trv == '/') {
*trv = '\0';
if (stat(path, &sbuf))
return(0);
return (0);
if (!S_ISDIR(sbuf.st_mode)) {
errno = ENOTDIR;
return(0);
return (0);
}
*trv = '/';
break;
@ -107,17 +117,17 @@ _gettemp(path, doopen)
if (doopen) {
if ((*doopen =
open(path, O_CREAT|O_EXCL|O_RDWR, 0600)) >= 0)
return(1);
return (1);
if (errno != EEXIST)
return(0);
return (0);
}
else if (stat(path, &sbuf))
return(errno == ENOENT ? 1 : 0);
return (errno == ENOENT ? 1 : 0);
/* tricky little algorithm for backward compatibility */
for (trv = start;;) {
if (!*trv)
return(0);
return (0);
if (*trv == 'z')
*trv++ = 'a';
else {

View File

@ -1,4 +1,4 @@
/* $NetBSD: tempnam.c,v 1.6 1995/02/02 02:10:42 jtc Exp $ */
/* $NetBSD: tempnam.c,v 1.7 1997/03/16 05:00:39 lukem Exp $ */
/*
* Copyright (c) 1988, 1993
@ -37,7 +37,7 @@
#if 0
static char sccsid[] = "@(#)tempnam.c 8.1 (Berkeley) 6/4/93";
#endif
static char rcsid[] = "$NetBSD: tempnam.c,v 1.6 1995/02/02 02:10:42 jtc Exp $";
static char rcsid[] = "$NetBSD: tempnam.c,v 1.7 1997/03/16 05:00:39 lukem Exp $";
#endif /* LIBC_SCCS and not lint */
#include <sys/param.h>
@ -48,6 +48,11 @@ static char rcsid[] = "$NetBSD: tempnam.c,v 1.6 1995/02/02 02:10:42 jtc Exp $";
#include <unistd.h>
#include <paths.h>
extern char *_mktemp __P((char *));
__warn_references(tempnam,
"warning: tempnam() possibly used unsafely, consider using mkstemp()");
char *
tempnam(dir, pfx)
const char *dir, *pfx;
@ -64,25 +69,25 @@ tempnam(dir, pfx)
if (f = getenv("TMPDIR")) {
(void)snprintf(name, MAXPATHLEN, "%s%s%sXXXXXX", f,
*(f + strlen(f) - 1) == '/'? "": "/", pfx);
if (f = mktemp(name))
if (f = _mktemp(name))
return(f);
}
if (f = (char *)dir) {
(void)snprintf(name, MAXPATHLEN, "%s%s%sXXXXXX", f,
*(f + strlen(f) - 1) == '/'? "": "/", pfx);
if (f = mktemp(name))
if (f = _mktemp(name))
return(f);
}
f = P_tmpdir;
(void)snprintf(name, MAXPATHLEN, "%s%sXXXXXX", f, pfx);
if (f = mktemp(name))
if (f = _mktemp(name))
return(f);
f = _PATH_TMP;
(void)snprintf(name, MAXPATHLEN, "%s%sXXXXXX", f, pfx);
if (f = mktemp(name))
if (f = _mktemp(name))
return(f);
sverrno = errno;

View File

@ -1,4 +1,4 @@
/* $NetBSD: tmpnam.c,v 1.6 1995/02/02 02:10:45 jtc Exp $ */
/* $NetBSD: tmpnam.c,v 1.7 1997/03/16 05:00:40 lukem Exp $ */
/*-
* Copyright (c) 1990, 1993, 1994
@ -40,7 +40,7 @@
#if 0
static char sccsid[] = "@(#)tmpnam.c 8.3 (Berkeley) 3/28/94";
#endif
static char rcsid[] = "$NetBSD: tmpnam.c,v 1.6 1995/02/02 02:10:45 jtc Exp $";
static char rcsid[] = "$NetBSD: tmpnam.c,v 1.7 1997/03/16 05:00:40 lukem Exp $";
#endif /* LIBC_SCCS and not lint */
#include <sys/types.h>
@ -48,6 +48,11 @@ static char rcsid[] = "$NetBSD: tmpnam.c,v 1.6 1995/02/02 02:10:45 jtc Exp $";
#include <stdio.h>
#include <unistd.h>
extern char *_mktemp __P((char *));
__warn_references(tmpnam,
"warning: tmpnam() possibly used unsafely, consider using mkstemp()");
char *
tmpnam(s)
char *s;
@ -59,5 +64,5 @@ tmpnam(s)
s = buf;
(void)snprintf(s, L_tmpnam, "%stmp.%lu.XXXXXX", P_tmpdir, tmpcount);
++tmpcount;
return (mktemp(s));
return (_mktemp(s));
}