stagit

pedantic snprintf() improvement

POSIX says:
"If an output error was encountered, these functions shall return a negative
value and set errno to indicate the error."

Hiltjo Posthuma contact@arjunchoudhary.com

commit: de57c68 parent: b659476
2 files changed, 4 insertions(+), 4 deletions(-)
Mstagit-index.c+1-1
Mstagit.c+3-3
M · stagit-index.c +1, -1
1@@ -28,7 +28,7 @@ joinpath(char *buf, size_t bufsiz, const char *path, const char *path2)
2 
3 	r = snprintf(buf, bufsiz, "%s%s%s",
4 		path, path[0] && path[strlen(path) - 1] != '/' ? "/" : "", path2);
5-	if (r == -1 || (size_t)r >= bufsiz)
6+	if (r < 0 || (size_t)r >= bufsiz)
7 		errx(1, "path truncated: '%s%s%s'",
8 			path, path[0] && path[strlen(path) - 1] != '/' ? "/" : "", path2);
9 }
M · stagit.c +3, -3
 1@@ -76,7 +76,7 @@ joinpath(char *buf, size_t bufsiz, const char *path, const char *path2)
 2 
 3 	r = snprintf(buf, bufsiz, "%s%s%s",
 4 		path, path[0] && path[strlen(path) - 1] != '/' ? "/" : "", path2);
 5-	if (r == -1 || (size_t)r >= bufsiz)
 6+	if (r < 0 || (size_t)r >= bufsiz)
 7 		errx(1, "path truncated: '%s%s%s'",
 8 			path, path[0] && path[strlen(path) - 1] != '/' ? "/" : "", path2);
 9 }
10@@ -616,7 +616,7 @@ writelog(FILE *fp, const git_oid *oid)
11 
12 		git_oid_tostr(oidstr, sizeof(oidstr), &id);
13 		r = snprintf(path, sizeof(path), "commit/%s.html", oidstr);
14-		if (r == -1 || (size_t)r >= sizeof(path))
15+		if (r < 0 || (size_t)r >= sizeof(path))
16 			errx(1, "path truncated: 'commit/%s.html'", oidstr);
17 		r = access(path, F_OK);
18 
19@@ -856,7 +856,7 @@ writefilestree(FILE *fp, git_tree *tree, const char *path)
20 
21 		r = snprintf(filepath, sizeof(filepath), "file/%s.html",
22 		         entrypath);
23-		if (r == -1 || (size_t)r >= sizeof(filepath))
24+		if (r < 0 || (size_t)r >= sizeof(filepath))
25 			errx(1, "path truncated: 'file/%s.html'", entrypath);
26 
27 		if (!git_tree_entry_to_object(&obj, repo, entry)) {