changeset 35213:984b47eb6389

Use snprintf instead of sprintf. No good reason beyond paranoia and Coverity complaining about it. In a very theoretical, construed case the adds might overflow or the environment might change in-between the getenv calls.
author reimar
date Wed, 31 Oct 2012 19:01:55 +0000
parents fe44546ee423
children 126ce1e7f87b
files stream/cookies.c
diffstat 1 files changed, 7 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/stream/cookies.c	Wed Oct 31 18:55:33 2012 +0000
+++ b/stream/cookies.c	Wed Oct 31 19:01:55 2012 +0000
@@ -205,11 +205,13 @@
     if (dir) {
 	while ((ent = readdir(dir)) != NULL) {
 	    if ((ent->d_name)[0] != '.') {
-		buf = malloc(strlen(getenv("HOME")) +
-                             sizeof("/.mozilla/default/") +
-                             strlen(ent->d_name) + sizeof("cookies.txt") + 1);
-		sprintf(buf, "%s/.mozilla/default/%s/cookies.txt",
-			 getenv("HOME"), ent->d_name);
+		const char *home = getenv("HOME");
+		unsigned len = strlen(home) +
+		               sizeof("/.mozilla/default/") +
+		               strlen(ent->d_name) + sizeof("cookies.txt") + 1;
+		buf = malloc(len);
+		snprintf(buf, len, "%s/.mozilla/default/%s/cookies.txt",
+			 home, ent->d_name);
 		list = load_cookies_from(buf, list);
 		free(buf);
 	    }