From 7646230de231ee6a8ba3322ac5ca88fdc6ca4697 Mon Sep 17 00:00:00 2001 From: nekral-guest Date: Fri, 24 Apr 2009 22:46:06 +0000 Subject: [PATCH] * libmisc/setupenv.c: Prefer snprintf to sprintf, even if a small context indicates no issues. * libmisc/setupenv.c: Avoid implicit conversion of pointers to booleans. --- ChangeLog | 7 +++++++ libmisc/loginprompt.c | 5 ++++- libmisc/mail.c | 5 ++++- libmisc/setupenv.c | 15 +++++++++++---- 4 files changed, 26 insertions(+), 6 deletions(-) diff --git a/ChangeLog b/ChangeLog index dc7c4307..0f98f82c 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +2009-04-25 Nicolas François + + * libmisc/setupenv.c: Prefer snprintf to sprintf, even if a small + context indicates no issues. + * libmisc/setupenv.c: Avoid implicit conversion of pointers to + booleans. + 2009-04-25 Nicolas François * libmisc/loginprompt.c: Prefer snprintf to sprintf, even if a diff --git a/libmisc/loginprompt.c b/libmisc/loginprompt.c index bcc42b36..3ca89388 100644 --- a/libmisc/loginprompt.c +++ b/libmisc/loginprompt.c @@ -34,6 +34,7 @@ #ident "$Id$" +#include #include #include #include @@ -157,8 +158,10 @@ void login_prompt (const char *prompt, char *name, int namesize) envp[envc] = nvar; } else { size_t len = strlen (nvar) + 32; + int wlen; envp[envc] = xmalloc (len); - snprintf (envp[envc], len, "L%d=%s", count++, nvar); + wlen = snprintf (envp[envc], len, "L%d=%s", count++, nvar); + assert (wlen == (int) len -1); } } set_env (envc, envp); diff --git a/libmisc/mail.c b/libmisc/mail.c index 5b70c73c..cf4f13ee 100644 --- a/libmisc/mail.c +++ b/libmisc/mail.c @@ -58,9 +58,12 @@ void mailcheck (void) if (NULL != mailbox) { char *newmail; size_t len = strlen (mailbox) + 5; + int wlen; newmail = xmalloc (len); - snprintf (newmail, len, "%s/new", mailbox); + wlen = snprintf (newmail, len, "%s/new", mailbox); + assert (wlen == (int) len - 1); + if (stat (newmail, &statbuf) != -1 && statbuf.st_size != 0) { if (statbuf.st_mtime > statbuf.st_atime) { free (newmail); diff --git a/libmisc/setupenv.c b/libmisc/setupenv.c index 7a665136..69388136 100644 --- a/libmisc/setupenv.c +++ b/libmisc/setupenv.c @@ -38,6 +38,7 @@ #ident "$Id$" +#include #include #include #include @@ -52,9 +53,13 @@ static void addenv_path (const char *varname, const char *dirname, const char *filename) { char *buf; + size_t len = strlen (dirname) + strlen (filename) + 2; + int wlen; + + buf = xmalloc (len); + wlen = snprintf (buf, len, "%s/%s", dirname, filename); + assert (wlen == (int) len - 1); - buf = xmalloc (strlen (dirname) + strlen (filename) + 2); - sprintf (buf, "%s/%s", dirname, filename); addenv (varname, buf); free (buf); } @@ -66,12 +71,14 @@ static void read_env_file (const char *filename) char *cp, *name, *val; fp = fopen (filename, "r"); - if (!fp) + if (NULL == fp) { return; + } while (fgets (buf, sizeof buf, fp) == buf) { cp = strrchr (buf, '\n'); - if (!cp) + if (NULL == cp) { break; + } *cp = '\0'; cp = buf; -- 2.40.0