From 2875419215061f767fe784f7a6f065e015daa325 Mon Sep 17 00:00:00 2001 From: "Andrey A. Chernov" Date: Wed, 2 Apr 1997 03:38:29 +0000 Subject: [PATCH] Remove unused USE_PERROR define and syslog.h include Use snprintf instead of sprintf to avoid buffer overflows Use snprintf in uu_lockerr instead of lots of hardcoded constants and not null-terminated strncpy Return "" for OK and "device in use" for INUSE, it allows simple strcpy(buf, uu_lockerr(retcode)) without testing for special OK case (NULL was there) and obtaining meaningful result for INUSE ("" was there) without special testing for it too. --- lib/libutil/uucplock.3 | 9 +++------ lib/libutil/uucplock.c | 33 ++++++++++++++------------------- 2 files changed, 17 insertions(+), 25 deletions(-) diff --git a/lib/libutil/uucplock.3 b/lib/libutil/uucplock.3 index 1841df3be5cf..896979ed756c 100644 --- a/lib/libutil/uucplock.3 +++ b/lib/libutil/uucplock.3 @@ -23,7 +23,7 @@ .\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF .\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. .\" -.\" $Id: uucplock.3,v 1.2 1997/03/31 22:47:53 brian Exp $ +.\" $Id: uucplock.3,v 1.3 1997/04/01 17:44:31 mpp Exp $ .\" " .Dd March 30, 1997 .Os @@ -113,11 +113,8 @@ If a value of .Dv UU_LOCK_OK is passed to .Fn uu_lockerr , -a -.Dv NULL -pointer is returned. If a value of -.Dv UU_LOCK_INUSE -is passed, an empty string is returned. Otherwise, a string specifying +an empty string returned. +Otherwise, a string specifying the reason for failure is returned. .Fn uu_lockerr uses the current value of diff --git a/lib/libutil/uucplock.c b/lib/libutil/uucplock.c index 0f08646c65d3..f6af4d02b2ca 100644 --- a/lib/libutil/uucplock.c +++ b/lib/libutil/uucplock.c @@ -39,9 +39,6 @@ static const char sccsid[] = "@(#)uucplock.c 8.1 (Berkeley) 6/6/93"; #include #include #include -#ifndef USE_PERROR -#include -#endif #include #include #include @@ -69,7 +66,7 @@ int uu_lock (char *ttyname) char tbuf[sizeof(_PATH_UUCPLOCK) + MAXNAMLEN]; int err; - (void)sprintf(tbuf, _PATH_UUCPLOCK LOCKFMT, ttyname); + (void)snprintf(tbuf, sizeof(tbuf), _PATH_UUCPLOCK LOCKFMT, ttyname); fd = open(tbuf, O_RDWR|O_CREAT|O_EXCL, 0660); if (fd < 0) { /* @@ -118,43 +115,41 @@ int uu_unlock (char *ttyname) { char tbuf[sizeof(_PATH_UUCPLOCK) + MAXNAMLEN]; - (void)sprintf(tbuf, _PATH_UUCPLOCK LOCKFMT, ttyname); + (void)snprintf(tbuf, sizeof(tbuf), _PATH_UUCPLOCK LOCKFMT, ttyname); return unlink(tbuf); } char *uu_lockerr (int uu_lockresult) { static char errbuf[512]; - int len; switch (uu_lockresult) { case UU_LOCK_INUSE: - return ""; + return "device in use"; case UU_LOCK_OK: - return 0; + return ""; case UU_LOCK_OPEN_ERR: - strcpy(errbuf,"open error: "); - len = 12; + (void)snprintf(errbuf, sizeof(errbuf), + "open error: %s", strerror(errno)); break; case UU_LOCK_READ_ERR: - strcpy(errbuf,"read error: "); - len = 12; + (void)snprintf(errbuf, sizeof(errbuf), + "read error: %s", strerror(errno)); break; case UU_LOCK_SEEK_ERR: - strcpy(errbuf,"seek error: "); - len = 12; + (void)snprintf(errbuf, sizeof(errbuf), + "seek error: %s", strerror(errno)); break; case UU_LOCK_WRITE_ERR: - strcpy(errbuf,"write error: "); - len = 13; + (void)snprintf(errbuf, sizeof(errbuf), + "write error: %s", strerror(errno)); break; default: - strcpy(errbuf,"Undefined error: "); - len = 17; + (void)snprintf(errbuf, sizeof(errbuf), + "undefined error: %s", strerror(errno)); break; } - strncpy(errbuf+len,strerror(errno),sizeof(errbuf)-len-1); return errbuf; }