summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorKarel Zak2019-05-15 15:47:29 +0200
committerKarel Zak2019-05-15 15:47:29 +0200
commit2c417af196f5e532c32e73de0ec98d0aa9d28b26 (patch)
tree05a312ecfcc040304d7694595e7e1ba8b3a59091 /lib
parentlib/path: fix possible NULL pointer dereferencing [coverity scan] (diff)
downloadkernel-qcow2-util-linux-2c417af196f5e532c32e73de0ec98d0aa9d28b26.tar.gz
kernel-qcow2-util-linux-2c417af196f5e532c32e73de0ec98d0aa9d28b26.tar.xz
kernel-qcow2-util-linux-2c417af196f5e532c32e73de0ec98d0aa9d28b26.zip
lib/path: consolidate ul_path_mkpath() usage
* always use errno from ul_path_mkpath() * keep code simple and short as possible Signed-off-by: Karel Zak <kzak@redhat.com>
Diffstat (limited to 'lib')
-rw-r--r--lib/path.c102
1 files changed, 28 insertions, 74 deletions
diff --git a/lib/path.c b/lib/path.c
index b3d66a45d..968ce9114 100644
--- a/lib/path.c
+++ b/lib/path.c
@@ -230,10 +230,16 @@ int ul_path_isopen_dirfd(struct path_cxt *pc)
static const char *ul_path_mkpath(struct path_cxt *pc, const char *path, va_list ap)
{
- int rc = vsnprintf(pc->path_buffer, sizeof(pc->path_buffer), path, ap);
+ int rc;
- if (rc < 0)
+ errno = 0;
+
+ rc = vsnprintf(pc->path_buffer, sizeof(pc->path_buffer), path, ap);
+ if (rc < 0) {
+ if (!errno)
+ errno = EINVAL;
return NULL;
+ }
if ((size_t)rc >= sizeof(pc->path_buffer)) {
errno = ENAMETOOLONG;
@@ -308,10 +314,7 @@ int ul_path_accessf(struct path_cxt *pc, int mode, const char *path, ...)
p = ul_path_mkpath(pc, path, ap);
va_end(ap);
- if (!p)
- return -errno;
-
- return ul_path_access(pc, mode, p);
+ return !p ? -errno : ul_path_access(pc, mode, p);
}
int ul_path_open(struct path_cxt *pc, int flags, const char *path)
@@ -341,13 +344,9 @@ int ul_path_open(struct path_cxt *pc, int flags, const char *path)
int ul_path_vopenf(struct path_cxt *pc, int flags, const char *path, va_list ap)
{
- const char *p;
+ const char *p = ul_path_mkpath(pc, path, ap);
- p = ul_path_mkpath(pc, path, ap);
- if (!p)
- return -errno;
-
- return ul_path_open(pc, flags, p);
+ return !p ? -errno : ul_path_open(pc, flags, p);
}
int ul_path_openf(struct path_cxt *pc, int flags, const char *path, ...)
@@ -408,13 +407,9 @@ FILE *ul_path_fopen(struct path_cxt *pc, const char *mode, const char *path)
FILE *ul_path_vfopenf(struct path_cxt *pc, const char *mode, const char *path, va_list ap)
{
- const char *p;
+ const char *p = ul_path_mkpath(pc, path, ap);
- p = ul_path_mkpath(pc, path, ap);
- if (!p)
- return NULL;
-
- return ul_path_fopen(pc, mode, p);
+ return !p ? NULL : ul_path_fopen(pc, mode, p);
}
FILE *ul_path_fopenf(struct path_cxt *pc, const char *mode, const char *path, ...)
@@ -465,13 +460,9 @@ DIR *ul_path_opendir(struct path_cxt *pc, const char *path)
*/
DIR *ul_path_vopendirf(struct path_cxt *pc, const char *path, va_list ap)
{
- const char *p;
+ const char *p = ul_path_mkpath(pc, path, ap);
- p = ul_path_mkpath(pc, path, ap);
- if (!p)
- return NULL;
-
- return ul_path_opendir(pc, p);
+ return !p ? NULL : ul_path_opendir(pc, p);
}
/*
@@ -523,10 +514,7 @@ ssize_t ul_path_readlinkf(struct path_cxt *pc, char *buf, size_t bufsiz, const c
p = ul_path_mkpath(pc, path, ap);
va_end(ap);
- if (!p)
- return -errno;
-
- return ul_path_readlink(pc, buf, bufsiz, p);
+ return !p ? -errno : ul_path_readlink(pc, buf, bufsiz, p);
}
int ul_path_read(struct path_cxt *pc, char *buf, size_t len, const char *path)
@@ -549,13 +537,9 @@ int ul_path_read(struct path_cxt *pc, char *buf, size_t len, const char *path)
int ul_path_vreadf(struct path_cxt *pc, char *buf, size_t len, const char *path, va_list ap)
{
- const char *p;
-
- p = ul_path_mkpath(pc, path, ap);
- if (!p)
- return -EINVAL;
+ const char *p = ul_path_mkpath(pc, path, ap);
- return ul_path_read(pc, buf, len, p);
+ return !p ? -errno : ul_path_read(pc, buf, len, p);
}
int ul_path_readf(struct path_cxt *pc, char *buf, size_t len, const char *path, ...)
@@ -609,10 +593,7 @@ int ul_path_readf_string(struct path_cxt *pc, char **str, const char *path, ...)
p = ul_path_mkpath(pc, path, ap);
va_end(ap);
- if (!p)
- return -EINVAL;
-
- return ul_path_read_string(pc, str, p);
+ return !p ? -errno : ul_path_read_string(pc, str, p);
}
int ul_path_read_buffer(struct path_cxt *pc, char *buf, size_t bufsz, const char *path)
@@ -638,10 +619,7 @@ int ul_path_readf_buffer(struct path_cxt *pc, char *buf, size_t bufsz, const cha
p = ul_path_mkpath(pc, path, ap);
va_end(ap);
- if (!p)
- return -EINVAL;
-
- return ul_path_read_buffer(pc, buf, bufsz, p);
+ return !p ? -errno : ul_path_read_buffer(pc, buf, bufsz, p);
}
int ul_path_scanf(struct path_cxt *pc, const char *path, const char *fmt, ...)
@@ -705,10 +683,7 @@ int ul_path_readf_s64(struct path_cxt *pc, int64_t *res, const char *path, ...)
p = ul_path_mkpath(pc, path, ap);
va_end(ap);
- if (!p)
- return -EINVAL;
-
- return ul_path_read_s64(pc, res, p);
+ return !p ? -errno : ul_path_read_s64(pc, res, p);
}
int ul_path_read_u64(struct path_cxt *pc, uint64_t *res, const char *path)
@@ -733,10 +708,7 @@ int ul_path_readf_u64(struct path_cxt *pc, uint64_t *res, const char *path, ...)
p = ul_path_mkpath(pc, path, ap);
va_end(ap);
- if (!p)
- return -EINVAL;
-
- return ul_path_read_u64(pc, res, p);
+ return !p ? -errno : ul_path_read_u64(pc, res, p);
}
int ul_path_read_s32(struct path_cxt *pc, int *res, const char *path)
@@ -760,10 +732,7 @@ int ul_path_readf_s32(struct path_cxt *pc, int *res, const char *path, ...)
p = ul_path_mkpath(pc, path, ap);
va_end(ap);
- if (!p)
- return -EINVAL;
-
- return ul_path_read_s32(pc, res, p);
+ return !p ? -errno : ul_path_read_s32(pc, res, p);
}
int ul_path_read_u32(struct path_cxt *pc, unsigned int *res, const char *path)
@@ -788,10 +757,7 @@ int ul_path_readf_u32(struct path_cxt *pc, unsigned int *res, const char *path,
p = ul_path_mkpath(pc, path, ap);
va_end(ap);
- if (!p)
- return -EINVAL;
-
- return ul_path_read_u32(pc, res, p);
+ return !p ? -errno : ul_path_read_u32(pc, res, p);
}
int ul_path_read_majmin(struct path_cxt *pc, dev_t *res, const char *path)
@@ -815,10 +781,7 @@ int ul_path_readf_majmin(struct path_cxt *pc, dev_t *res, const char *path, ...)
p = ul_path_mkpath(pc, path, ap);
va_end(ap);
- if (!p)
- return -EINVAL;
-
- return ul_path_read_majmin(pc, res, p);
+ return !p ? -errno : ul_path_read_majmin(pc, res, p);
}
int ul_path_write_string(struct path_cxt *pc, const char *str, const char *path)
@@ -847,10 +810,7 @@ int ul_path_writef_string(struct path_cxt *pc, const char *str, const char *path
p = ul_path_mkpath(pc, path, ap);
va_end(ap);
- if (!p)
- return -EINVAL;
-
- return ul_path_write_string(pc, str, p);
+ return !p ? -errno : ul_path_write_string(pc, str, p);
}
int ul_path_write_s64(struct path_cxt *pc, int64_t num, const char *path)
@@ -906,10 +866,7 @@ int ul_path_writef_u64(struct path_cxt *pc, uint64_t num, const char *path, ...)
p = ul_path_mkpath(pc, path, ap);
va_end(ap);
- if (!p)
- return -EINVAL;
-
- return ul_path_write_u64(pc, num, p);
+ return !p ? -errno : ul_path_write_u64(pc, num, p);
}
@@ -937,10 +894,7 @@ int ul_path_countf_dirents(struct path_cxt *pc, const char *path, ...)
p = ul_path_mkpath(pc, path, ap);
va_end(ap);
- if (!p)
- return 0;
-
- return ul_path_count_dirents(pc, p);
+ return !p ? -errno : ul_path_count_dirents(pc, p);
}
/*