diff --git a/include/path.h b/include/path.h index 45da692f8..ae36d7f4c 100644 --- a/include/path.h +++ b/include/path.h @@ -4,8 +4,11 @@ #include #include -extern char *path_strdup(const char *path, ...) +/* Returns a pointer to a static buffer which may be destroyed by any later +path_* function call. NULL means error and errno will be set. */ +extern const char *path_get(const char *path, ...) __attribute__ ((__format__ (__printf__, 1, 2))); + extern FILE *path_fopen(const char *mode, int exit_on_err, const char *path, ...) __attribute__ ((__format__ (__printf__, 3, 4))); extern void path_read_str(char *result, size_t len, const char *path, ...) @@ -27,7 +30,11 @@ extern cpu_set_t *path_read_cpuset(int, const char *path, ...) __attribute__ ((__format__ (__printf__, 2, 3))); extern cpu_set_t *path_read_cpulist(int, const char *path, ...) __attribute__ ((__format__ (__printf__, 2, 3))); -extern void path_set_prefix(const char *); + +/* Returns: 0 on success, sets errno on error. */ +extern int path_set_prefix(const char *) + __attribute__((warn_unused_result)); + #endif /* HAVE_CPU_SET_T */ #endif /* UTIL_LINUX_PATH_H */ diff --git a/lib/mbsedit.c b/lib/mbsedit.c index d464358fc..e028c496d 100644 --- a/lib/mbsedit.c +++ b/lib/mbsedit.c @@ -151,7 +151,7 @@ static size_t mbs_insert(char *str, wint_t c, size_t *ncells) { /* all in bytes! */ size_t n = 1, bytes; - char *in = (char *) &c; + char *in; #ifdef HAVE_WIDECHAR wchar_t wc = (wchar_t) c; @@ -162,6 +162,7 @@ static size_t mbs_insert(char *str, wint_t c, size_t *ncells) in = in_buf; #else *ncells = 1; + in = (char *) &c; #endif bytes = strlen(str); diff --git a/lib/path.c b/lib/path.c index 1a623bc6d..79c1e7a68 100644 --- a/lib/path.c +++ b/lib/path.c @@ -38,16 +38,20 @@ static char pathbuf[PATH_MAX]; static const char * path_vcreate(const char *path, va_list ap) { - if (prefixlen) - vsnprintf(pathbuf + prefixlen, - sizeof(pathbuf) - prefixlen, path, ap); - else - vsnprintf(pathbuf, sizeof(pathbuf), path, ap); + int rc = vsnprintf( + pathbuf + prefixlen, sizeof(pathbuf) - prefixlen, path, ap); + + if (rc < 0) + return NULL; + if ((size_t)rc >= sizeof(pathbuf)) { + errno = ENAMETOOLONG; + return NULL; + } return pathbuf; } -char * -path_strdup(const char *path, ...) +const char * +path_get(const char *path, ...) { const char *p; va_list ap; @@ -56,7 +60,7 @@ path_strdup(const char *path, ...) p = path_vcreate(path, ap); va_end(ap); - return p ? strdup(p) : NULL; + return p; } static FILE * @@ -64,11 +68,18 @@ path_vfopen(const char *mode, int exit_on_error, const char *path, va_list ap) { FILE *f; const char *p = path_vcreate(path, ap); + if (!p) + goto err; f = fopen(p, mode); - if (!f && exit_on_error) - err(EXIT_FAILURE, _("cannot open %s"), p); + if (!f) + goto err; + return f; +err: + if (exit_on_error) + err(EXIT_FAILURE, _("cannot open %s"), p ? p : "path"); + return NULL; } static int @@ -76,11 +87,16 @@ path_vopen(int flags, const char *path, va_list ap) { int fd; const char *p = path_vcreate(path, ap); + if (!p) + goto err; fd = open(p, flags); if (fd == -1) - err(EXIT_FAILURE, _("cannot open %s"), p); + goto err; + return fd; +err: + err(EXIT_FAILURE, _("cannot open %s"), p ? p : "path"); } FILE * @@ -181,7 +197,7 @@ path_exist(const char *path, ...) p = path_vcreate(path, ap); va_end(ap); - return access(p, F_OK) == 0; + return p && access(p, F_OK) == 0; } #ifdef HAVE_CPU_SET_T @@ -244,12 +260,18 @@ path_read_cpulist(int maxcpus, const char *path, ...) return set; } -void +int path_set_prefix(const char *prefix) { - prefixlen = strlen(prefix); - strncpy(pathbuf, prefix, sizeof(pathbuf)); - pathbuf[sizeof(pathbuf) - 1] = '\0'; + size_t len = strlen(prefix); + + if (len >= sizeof(pathbuf) - 1) { + errno = ENAMETOOLONG; + return -1; + } + prefixlen = len; + strcpy(pathbuf, prefix); + return 0; } #endif /* HAVE_CPU_SET_T */ diff --git a/misc-utils/findmnt-verify.c b/misc-utils/findmnt-verify.c index b32901d66..1cc62def9 100644 --- a/misc-utils/findmnt-verify.c +++ b/misc-utils/findmnt-verify.c @@ -126,14 +126,13 @@ done: static int verify_target(struct verify_context *vfy) { const char *tgt = mnt_fs_get_target(vfy->fs); - const char *cn = tgt; struct stat sb; if (!tgt) return verify_err(vfy, _("undefined target (fs_file)")); if (!(flags & FL_NOCACHE)) { - cn = mnt_resolve_target(tgt, cache); + const char *cn = mnt_resolve_target(tgt, cache); if (!cn) return -ENOMEM; if (strcmp(cn, tgt) != 0) diff --git a/sys-utils/hwclock.c b/sys-utils/hwclock.c index 98ee5bef5..a0a48dd13 100644 --- a/sys-utils/hwclock.c +++ b/sys-utils/hwclock.c @@ -1225,10 +1225,11 @@ usage(const struct hwclock_control *ctl) fputs(USAGE_OPTIONS, out); fputs(_(" -u, --utc inform hwclock the RTC timescale is UTC\n"), out); fputs(_(" -l, --localtime inform hwclock the RTC timescale is Local\n"), out); - fprintf(out, _( #ifdef __linux__ - " -f, --rtc use an alternate file to %1$s\n" + printf(_( + " -f, --rtc use an alternate file to %1$s\n"), _PATH_RTC_DEV); #endif + printf(_( " --directisa use the ISA bus instead of %1$s access\n"), _PATH_RTC_DEV); fputs(_(" --date