From: Karel Zak Date: Fri, 4 May 2007 07:14:04 +0000 (+0200) Subject: mount: use realloc for xstrconcat functions X-Git-Url: https://err.no/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=6573c985a4077fa7d50ccb993bae177526fde8ec;p=util-linux mount: use realloc for xstrconcat functions Everywhere in code is s = xstrconcat3(s, t, u); that's nonsense use malloc(new_s); free(s); return new_s; Signed-off-by: Karel Zak --- diff --git a/mount/mount.c b/mount/mount.c index c0302041..495eaed6 100644 --- a/mount/mount.c +++ b/mount/mount.c @@ -281,7 +281,7 @@ append_opt(char *s, const char *opt, const char *val) if (!val) return xstrdup(opt); /* opt */ - return xstrconcat2(opt, val); /* opt=val */ + return xstrconcat3(NULL, opt, val); /* opt=val */ } if (!val) return xstrconcat3(s, ",", opt); /* s,opt */ @@ -491,8 +491,8 @@ do_mount_syscall (struct mountargs *args) { flags |= MS_MGC_VAL; mnt_debug(1, "mount(2) syscall: source=\"%s\" target=\"%s\" " - "filesystemtype=\"%s\" mountflags=%lu data=%s", - args->spec, args->node, args->type, flags, args->data ? "YES" : "NOT"); + "filesystemtype=\"%s\" mountflags=%lu data=\"%s\"", + args->spec, args->node, args->type, flags, (char *) args->data); ret = mount (args->spec, args->node, args->type, flags, args->data); if (ret == 0) diff --git a/mount/sundries.c b/mount/sundries.c index 1be9099b..e6208e43 100644 --- a/mount/sundries.c +++ b/mount/sundries.c @@ -15,6 +15,7 @@ #include "fstab.h" #include "sundries.h" #include "realpath.h" +#include "xmalloc.h" #include "nfsmount.h" #include "nls.h" @@ -32,50 +33,53 @@ xstrndup (const char *s, int n) { return t; } +/* reallocates its first arg - typical use: s = xstrconcat3(s,t,u); */ char * -xstrconcat2 (const char *s, const char *t) { - char *res; - - if (!s) s = ""; - if (!t) t = ""; - res = xmalloc(strlen(s) + strlen(t) + 1); - strcpy(res, s); - strcat(res, t); - return res; -} +xstrconcat3 (char *s, const char *t, const char *u) { + size_t len = 0; -/* frees its first arg - typical use: s = xstrconcat3(s,t,u); */ -char * -xstrconcat3 (const char *s, const char *t, const char *u) { - char *res; - - if (!s) s = ""; - if (!t) t = ""; - if (!u) u = ""; - res = xmalloc(strlen(s) + strlen(t) + strlen(u) + 1); - strcpy(res, s); - strcat(res, t); - strcat(res, u); - free((void *) s); - return res; + len = (s ? strlen(s) : 0) + (t ? strlen(t) : 0) + (u ? strlen(u) : 0); + + if (!len) + return NULL; + if (!s) { + s = xmalloc(len + 1); + *s = '\0'; + } + else + s = xrealloc(s, len + 1); + if (t) + strcat(s, t); + if (u) + strcat(s, u); + return s; } /* frees its first arg - typical use: s = xstrconcat4(s,t,u,v); */ char * -xstrconcat4 (const char *s, const char *t, const char *u, const char *v) { - char *res; - - if (!s) s = ""; - if (!t) t = ""; - if (!u) u = ""; - if (!v) v = ""; - res = xmalloc(strlen(s) + strlen(t) + strlen(u) + strlen(v) + 1); - strcpy(res, s); - strcat(res, t); - strcat(res, u); - strcat(res, v); - free((void *) s); - return res; +xstrconcat4 (char *s, const char *t, const char *u, const char *v) { + size_t len = 0; + + len = (s ? strlen(s) : 0) + (t ? strlen(t) : 0) + + (u ? strlen(u) : 0) + (v ? strlen(v) : 0); + + if (!len) + return NULL; + if (!s) { + s = xmalloc(len + 1); + *s = '\0'; + } + else + s = xrealloc(s, len + 1); + if (t) + strcat(s, t); + if (u) + strcat(s, u); + if (v) + strcat(s, v); + return s; + + } /* Call this with SIG_BLOCK to block and SIG_UNBLOCK to unblock. */ @@ -96,15 +100,13 @@ block_signals (int how) { void error (const char *fmt, ...) { va_list args; - char *fmt2; if (mount_quiet) return; - fmt2 = xstrconcat2 (fmt, "\n"); va_start (args, fmt); - vfprintf (stderr, fmt2, args); + vfprintf (stderr, fmt, args); va_end (args); - free (fmt2); + fputc('\n', stderr); } /* True if fstypes match. Null *TYPES means match anything, diff --git a/mount/sundries.h b/mount/sundries.h index 00959be7..c3780f17 100644 --- a/mount/sundries.h +++ b/mount/sundries.h @@ -32,9 +32,8 @@ int matching_opts (const char *options, const char *test_opts); void *xmalloc (size_t size); char *xstrdup (const char *s); char *xstrndup (const char *s, int n); -char *xstrconcat2 (const char *, const char *); -char *xstrconcat3 (const char *, const char *, const char *); -char *xstrconcat4 (const char *, const char *, const char *, const char *); +char *xstrconcat3 (char *, const char *, const char *); +char *xstrconcat4 (char *, const char *, const char *, const char *); void die (int errcode, const char *fmt, ...);