From 39a4d38bb7da97189a301b8a26cb83a469d87d3d Mon Sep 17 00:00:00 2001 From: sinanmohd Date: Fri, 12 Apr 2024 08:07:25 +0530 Subject: c: properly handle return value of snprintf --- src/libnpass/libnpass.c | 48 ++++++++++++++++++++++++------------------------ src/libnpass/util.c | 4 ++-- src/npass/npass.c | 4 ++-- 3 files changed, 28 insertions(+), 28 deletions(-) diff --git a/src/libnpass/libnpass.c b/src/libnpass/libnpass.c index b7a0ded..6618385 100644 --- a/src/libnpass/libnpass.c +++ b/src/libnpass/libnpass.c @@ -55,8 +55,8 @@ static int set_pass_dir(void) if (env) { r = snprintf(pass_dir, sizeof(pass_dir), "%s/%s", env, ".password-store"); - if ((size_t)r > sizeof(pass_dir)) - err_ret(PASS_STORE_INV, "path exceeded PATH_MAX"); + if (r < 0 || (size_t)r > sizeof(pass_dir)) + err_ret(PASS_STORE_INV, "failed to build path"); r = stat(pass_dir, &statbuf); if (!r) @@ -67,8 +67,8 @@ static int set_pass_dir(void) if (env) { r = snprintf(pass_dir, sizeof(pass_dir), "%s/%s", env, DEF_PASS_DIR); - if ((size_t)r > sizeof(pass_dir)) - err_ret(PASS_STORE_INV, "path exceeded PATH_MAX"); + if (r < 0 || (size_t)r > sizeof(pass_dir)) + err_ret(PASS_STORE_INV, "failed to build path"); return 0; } @@ -76,8 +76,8 @@ static int set_pass_dir(void) if (env) { r = snprintf(pass_dir, sizeof(pass_dir), "%s/%s/%s", env, ".local/share", DEF_PASS_DIR); - if ((size_t)r > sizeof(pass_dir)) - err_ret(PASS_STORE_INV, "path exceeded PATH_MAX"); + if (r < 0 || (size_t)r > sizeof(pass_dir)) + err_ret(PASS_STORE_INV, "failed to build path"); return 0; } @@ -132,15 +132,15 @@ pass_store_t pass_store_type(const char *spath) r = snprintf(abs_path, sizeof(abs_path), "%s/%s", pass_dir, (spath) ? spath : ""); - if (r >= (int)sizeof(abs_path)) - err_ret(PASS_STORE_INV, "path exceeded PATH_MAX"); + if (r < 0 || (size_t)r >= sizeof(abs_path)) + err_ret(PASS_STORE_INV, "failed to build path"); r = stat(abs_path, &sbuf); if (!r && (sbuf.st_mode & S_IFMT) == S_IFDIR) return PASS_STORE_DIR; r = snprintf(abs_path, sizeof(abs_path), "%s/%s.gpg", pass_dir, spath); - if (r >= (int)sizeof(abs_path)) - err_ret(PASS_STORE_INV, "path exceeded PATH_MAX"); + if (r < 0 || (size_t)r >= sizeof(abs_path)) + err_ret(PASS_STORE_INV, "failed to build path"); r = stat(abs_path, &sbuf); if (r) err_ret(PASS_STORE_INV, "%s", strerror(errno)); @@ -167,8 +167,8 @@ DIR *openstore(const char *spath) if (spath) { r = snprintf(abs_path, sizeof(abs_path), "%s/%s", pass_dir, spath); - if (r >= (int)sizeof(abs_path)) - err_ret(NULL, "path exceeded PATH_MAX"); + if (r < 0 || (size_t)r >= sizeof(abs_path)) + err_ret(NULL, "failed to build path"); path = abs_path; } else { @@ -274,8 +274,8 @@ int pass_init(const char *fpr) r = snprintf(gpg_id_path, sizeof(gpg_id_path), "%s/%s", pass_dir, ".gpg-id"); - if (r > (int)sizeof(gpg_id_path)) - err_ret(1, "path exceeded PATH_MAX"); + if (r < 0 || (size_t)r >= sizeof(gpg_id_path)) + err_ret(1, "failed to build path"); gpg_id = fopen(gpg_id_path, "w"); if (!gpg_id) @@ -304,8 +304,8 @@ int pass_cat(FILE *out, const char *path) err_ret(1, "PASSWORD_STORE_DIR not set"); r = snprintf(pass_path, sizeof(pass_path), "%s/%s.gpg", pass_dir, path); - if (r >= (int)sizeof(pass_path)) - err_ret(1, "path exceeded PATH_MAX"); + if (r < 0 || (size_t)r >= sizeof(pass_path)) + err_ret(1, "failed to build path"); r = gpg_decrypt(out, pass_path); return r; @@ -372,8 +372,8 @@ int pass_add(const char *path, const char *pass, size_t n) r = snprintf(gpg_id_path, sizeof(gpg_id_path), "%s/%s", pass_dir, ".gpg-id"); - if (r > (int)sizeof(gpg_id_path)) - err_ret(1, "path exceeded PATH_MAX"); + if (r < 0 || (size_t)r >= sizeof(gpg_id_path)) + err_ret(1, "failed to build path"); gpg_id = fopen(gpg_id_path, "r"); if (!gpg_id) @@ -391,8 +391,8 @@ int pass_add(const char *path, const char *pass, size_t n) /* TODO: guard against .*\.gpg\.gpg[/$] */ r = snprintf(pass_path, sizeof(pass_path), "%s/%s.gpg", pass_dir, path); - if (r > (int)sizeof(pass_path)) - err_ret(1, "path exceeded PATH_MAX"); + if (r < 0 || (size_t)r >= sizeof(pass_path)) + err_ret(1, "failed to build path"); rc = strdup(pass_path); if (!rc) @@ -424,12 +424,12 @@ int pass_rm(const char *path) err_ret(1, "PASSWORD_STORE_DIR not set"); r = snprintf(gpg_path, sizeof(gpg_path), "%s.gpg", path); - if (r > (int)sizeof(gpg_path)) - err_ret(1, "path exceeded PATH_MAX"); + if (r < 0 || (size_t)r >= sizeof(gpg_path)) + err_ret(1, "failed to build path"); r = snprintf(abs_path, sizeof(gpg_path), "%s/%s", pass_dir, gpg_path); - if (r > (int)sizeof(abs_path)) - err_ret(1, "path exceeded PATH_MAX"); + if (r < 0 || (size_t)r >= sizeof(abs_path)) + err_ret(1, "failed to build path"); /* TODO: guard against .*\.gpg\.gpg[/$] */ r = unlink(abs_path); diff --git a/src/libnpass/util.c b/src/libnpass/util.c index c17c4ac..4e34c26 100644 --- a/src/libnpass/util.c +++ b/src/libnpass/util.c @@ -46,8 +46,8 @@ int r_rmdir(const char *prefix_path, char *rm_path) return 0; r = snprintf(abs_path, sizeof(abs_path), "%s/%s", prefix_path, rm_path); - if ((size_t)r >= sizeof(abs_path)) - err_ret(1, "path exceeded PATH_MAX"); + if (r < 0 || (size_t)r >= sizeof(abs_path)) + err_ret(1, "failed to build path"); r = rmdir(abs_path); if (r && errno != EEXIST && errno != ENOTEMPTY) diff --git a/src/npass/npass.c b/src/npass/npass.c index a38b202..025566e 100644 --- a/src/npass/npass.c +++ b/src/npass/npass.c @@ -100,8 +100,8 @@ static int ls(const char *path, size_t depth) r = snprintf(new_path, sizeof(new_path), "%s/%s", (path) ? path : "", stor[i].name); - if ((size_t)r >= sizeof(new_path)) - err_ret(1, "%s", "path exceeded PATH_MAX"); + if (r < 0 || (size_t)r >= sizeof(new_path)) + err_ret(1, "%s", "failed to build path"); ls(new_path, depth + 1); } else { printf("%s %s\n", prefix, stor[i].name); -- cgit v1.2.3