Browse Source

Make sure we check snprintf return value for failure or truncation before

using it.  Make uu_lock_txfr work by properly checking return value from
put_pid().  Fix an FD leak on the lockfile in an error path (from FreeBSD
with modifications).  OK millert@, input from otto@ and Bruno Rohee.
OPENBSD_3_7
cloder 20 years ago
parent
commit
d27b833e7e
1 changed files with 10 additions and 9 deletions
  1. +10
    -9
      src/lib/libutil/uucplock.c

+ 10
- 9
src/lib/libutil/uucplock.c View File

@ -1,4 +1,4 @@
/* $OpenBSD: uucplock.c,v 1.12 2004/05/28 07:03:48 deraadt Exp $ */
/* $OpenBSD: uucplock.c,v 1.13 2005/03/03 00:14:17 cloder Exp $ */
/* /*
* Copyright (c) 1988, 1993 * Copyright (c) 1988, 1993
* The Regents of the University of California. All rights reserved. * The Regents of the University of California. All rights reserved.
@ -126,20 +126,21 @@ int
uu_lock_txfr(const char *ttyname, pid_t pid) uu_lock_txfr(const char *ttyname, pid_t pid)
{ {
char lckname[sizeof(_PATH_UUCPLOCK) + MAXNAMLEN]; char lckname[sizeof(_PATH_UUCPLOCK) + MAXNAMLEN];
int fd, err;
int fd, err, ret;
snprintf(lckname, sizeof(lckname), _PATH_UUCPLOCK LOCKFMT, ttyname); snprintf(lckname, sizeof(lckname), _PATH_UUCPLOCK LOCKFMT, ttyname);
if ((fd = open(lckname, O_RDWR)) < 0) if ((fd = open(lckname, O_RDWR)) < 0)
return UU_LOCK_OWNER_ERR; return UU_LOCK_OWNER_ERR;
if (get_pid(fd, &err) != getpid()) if (get_pid(fd, &err) != getpid())
return UU_LOCK_OWNER_ERR;
lseek(fd, 0, SEEK_SET);
if (put_pid(fd, pid))
return UU_LOCK_WRITE_ERR;
close(fd);
ret = UU_LOCK_OWNER_ERR;
else {
lseek(fd, 0, SEEK_SET);
ret = put_pid(fd, pid) ? UU_LOCK_OK : UU_LOCK_WRITE_ERR;
}
return UU_LOCK_OK;
close(fd);
return ret;
} }
int int
@ -200,7 +201,7 @@ put_pid(int fd, pid_t pid)
len = snprintf(buf, sizeof buf, "%10ld\n", (long)pid); len = snprintf(buf, sizeof buf, "%10ld\n", (long)pid);
if (write (fd, buf, len) == len) {
if (len < sizeof buf && len != -1 && write (fd, buf, len) == len) {
/* We don't mind too much if ftruncate() fails - see get_pid */ /* We don't mind too much if ftruncate() fails - see get_pid */
ftruncate(fd, len); ftruncate(fd, len);
return 1; return 1;


Loading…
Cancel
Save