]> git.armaanb.net Git - stagit.git/commitdiff
pedantic snprintf() improvement
authorHiltjo Posthuma <hiltjo@codemadness.org>
Sat, 9 Mar 2019 11:39:10 +0000 (12:39 +0100)
committerHiltjo Posthuma <hiltjo@codemadness.org>
Sat, 9 Mar 2019 11:39:10 +0000 (12:39 +0100)
POSIX says:
"If an output error was encountered, these functions shall return a negative
value and set errno to indicate the error."

stagit-index.c
stagit.c

index accb1a534cb027dc08dc5f87e11a230a9f328523..cc70e4de0adf31bc4705fa58204cbafbb14d8b5f 100644 (file)
@@ -28,7 +28,7 @@ joinpath(char *buf, size_t bufsiz, const char *path, const char *path2)
 
        r = snprintf(buf, bufsiz, "%s%s%s",
                path, path[0] && path[strlen(path) - 1] != '/' ? "/" : "", path2);
-       if (r == -1 || (size_t)r >= bufsiz)
+       if (r < 0 || (size_t)r >= bufsiz)
                errx(1, "path truncated: '%s%s%s'",
                        path, path[0] && path[strlen(path) - 1] != '/' ? "/" : "", path2);
 }
index b8abea32fa447225f26b9ef793e0ae7c4723b771..1fc6c1e592458935fe348ef2a480b0b8018d7fa1 100644 (file)
--- a/stagit.c
+++ b/stagit.c
@@ -76,7 +76,7 @@ joinpath(char *buf, size_t bufsiz, const char *path, const char *path2)
 
        r = snprintf(buf, bufsiz, "%s%s%s",
                path, path[0] && path[strlen(path) - 1] != '/' ? "/" : "", path2);
-       if (r == -1 || (size_t)r >= bufsiz)
+       if (r < 0 || (size_t)r >= bufsiz)
                errx(1, "path truncated: '%s%s%s'",
                        path, path[0] && path[strlen(path) - 1] != '/' ? "/" : "", path2);
 }
@@ -616,7 +616,7 @@ writelog(FILE *fp, const git_oid *oid)
 
                git_oid_tostr(oidstr, sizeof(oidstr), &id);
                r = snprintf(path, sizeof(path), "commit/%s.html", oidstr);
-               if (r == -1 || (size_t)r >= sizeof(path))
+               if (r < 0 || (size_t)r >= sizeof(path))
                        errx(1, "path truncated: 'commit/%s.html'", oidstr);
                r = access(path, F_OK);
 
@@ -856,7 +856,7 @@ writefilestree(FILE *fp, git_tree *tree, const char *path)
 
                r = snprintf(filepath, sizeof(filepath), "file/%s.html",
                         entrypath);
-               if (r == -1 || (size_t)r >= sizeof(filepath))
+               if (r < 0 || (size_t)r >= sizeof(filepath))
                        errx(1, "path truncated: 'file/%s.html'", entrypath);
 
                if (!git_tree_entry_to_object(&obj, repo, entry)) {