From 2e48312004acdd4a1f699fa242eef17659064dc1 Mon Sep 17 00:00:00 2001 From: itojun Date: Sun, 3 Aug 2003 04:35:50 +0000 Subject: [PATCH] be more pedantic to strlcpy/strcat errors --- lib/libc/gen/getcwd.c | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/lib/libc/gen/getcwd.c b/lib/libc/gen/getcwd.c index 0b3fc1d8339a..70355083906e 100644 --- a/lib/libc/gen/getcwd.c +++ b/lib/libc/gen/getcwd.c @@ -1,4 +1,4 @@ -/* $NetBSD: getcwd.c,v 1.30 2003/08/03 04:14:00 itojun Exp $ */ +/* $NetBSD: getcwd.c,v 1.31 2003/08/03 04:35:50 itojun Exp $ */ /* * Copyright (c) 1989, 1991, 1993, 1995 @@ -41,7 +41,7 @@ #if 0 static char sccsid[] = "@(#)getcwd.c 8.5 (Berkeley) 2/7/95"; #else -__RCSID("$NetBSD: getcwd.c,v 1.30 2003/08/03 04:14:00 itojun Exp $"); +__RCSID("$NetBSD: getcwd.c,v 1.31 2003/08/03 04:35:50 itojun Exp $"); #endif #endif /* LIBC_SCCS and not lint */ @@ -107,7 +107,10 @@ realpath(path, resolved) * if it is a directory, then change to that directory. * get the current directory name and append the basename. */ - (void)strlcpy(resolved, path, MAXPATHLEN); + if (strlcpy(resolved, path, MAXPATHLEN) >= MAXPATHLEN) { + errno = ENAMETOOLONG; + goto err1; + } loop: q = strrchr(resolved, '/'); if (q != NULL) { @@ -150,7 +153,10 @@ loop: * Save the last component name and get the full pathname of * the current directory. */ - (void)strlcpy(wbuf, p, sizeof(wbuf)); + if (strlcpy(wbuf, p, sizeof(wbuf)) >= sizeof(wbuf)) { + errno = ENAMETOOLONG; + goto err1; + } /* * Call the inernal internal version of getcwd which @@ -175,8 +181,14 @@ loop: goto err1; } if (rootd == 0) - (void)strlcat(resolved, "/", MAXPATHLEN); - (void)strlcat(resolved, wbuf, MAXPATHLEN); + if (strlcat(resolved, "/", MAXPATHLEN) >= MAXPATHLEN) { + errno = ENAMETOOLONG; + goto err1; + } + if (strlcat(resolved, wbuf, MAXPATHLEN) >= MAXPATHLEN) { + errno = ENAMETOOLONG; + goto err1; + } } /* Go back to where we came from. */