diff options
author | Sunil Nimmagadda <sunil@cvs.openbsd.org> | 2016-01-19 11:15:40 +0000 |
---|---|---|
committer | Sunil Nimmagadda <sunil@cvs.openbsd.org> | 2016-01-19 11:15:40 +0000 |
commit | b4b52c20c19408ac1c92f87d50bb466ff2fbac55 (patch) | |
tree | 57a01d1f7807a303223afb261b67999b380a4bcc | |
parent | 87dc01c6aad217927cbcf4a725a10fbc816b3828 (diff) |
Check snprintf(3) return value for overflow instead of manual length
check. From max at max-fillinger.net.
Ok lum@ jasper@
-rw-r--r-- | usr.bin/mg/cscope.c | 9 |
1 files changed, 4 insertions, 5 deletions
diff --git a/usr.bin/mg/cscope.c b/usr.bin/mg/cscope.c index 1fa0efa4b5e..353b7458b4d 100644 --- a/usr.bin/mg/cscope.c +++ b/usr.bin/mg/cscope.c @@ -1,4 +1,4 @@ -/* $OpenBSD: cscope.c,v 1.12 2016/01/19 11:13:02 sunil Exp $ */ +/* $OpenBSD: cscope.c,v 1.13 2016/01/19 11:15:39 sunil Exp $ */ /* * This file is in the public domain. @@ -593,7 +593,7 @@ int csexists(const char *cmd) { char fname[NFILEN], *dir, *path, *pathc, *tmp; - int cmdlen, dlen; + int len, dlen; /* Special case if prog contains '/' */ if (strchr(cmd, '/')) { @@ -609,7 +609,6 @@ csexists(const char *cmd) ewprintf("out of memory"); return (FALSE); } - cmdlen = strlen(cmd); while ((dir = strsep(&path, ":")) != NULL) { if (*dir == '\0') continue; @@ -618,12 +617,12 @@ csexists(const char *cmd) while (dir[dlen-1] == '/') dir[--dlen] = '\0'; /* strip trailing '/' */ - if (dlen + 1 + cmdlen >= sizeof(fname)) { + len = snprintf(fname, sizeof(fname), "%s/%s", dir, cmd); + if (len == -1 || len >= sizeof(fname)) { dobeep(); ewprintf("path too long"); goto cleanup; } - snprintf(fname, sizeof(fname), "%s/%s", dir, cmd); if(access(fname, F_OK) == 0) { free(pathc); return (TRUE); |