Simplify the code by using asprintf. Suggested-by: "Darrick J. Wong" Signed-off-by: Bernd Schubert --- lib/mount.c | 28 +++++++++++++--------------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/lib/mount.c b/lib/mount.c index fe353e2cc4579adb47473cac5db7d1bae2defb2c..68f9219d2b8beee51346b198ce826138b9528e73 100644 --- a/lib/mount.c +++ b/lib/mount.c @@ -754,32 +754,30 @@ char *fuse_mnt_build_source(const struct mount_opts *mo) { const char *devname = fuse_mnt_get_devname(); char *source; + int ret; - source = malloc((mo->fsname ? strlen(mo->fsname) : 0) + - (mo->subtype ? strlen(mo->subtype) : 0) + - strlen(devname) + 32); - if (!source) + ret = asprintf(&source, "%s", + mo->fsname ? mo->fsname : + (mo->subtype ? mo->subtype : devname)); + if (ret == -1) return NULL; - strcpy(source, - mo->fsname ? mo->fsname : (mo->subtype ? mo->subtype : devname)); - return source; } char *fuse_mnt_build_type(const struct mount_opts *mo) { char *type; + int ret; - type = malloc((mo->subtype ? strlen(mo->subtype) : 0) + 32); - if (!type) + if (mo->subtype) + ret = asprintf(&type, "%s.%s", mo->blkdev ? "fuseblk" : "fuse", + mo->subtype); + else + ret = asprintf(&type, "%s", mo->blkdev ? "fuseblk" : "fuse"); + + if (ret == -1) return NULL; - strcpy(type, mo->blkdev ? "fuseblk" : "fuse"); - if (mo->subtype) { - strcat(type, "."); - strcat(type, mo->subtype); - } - return type; } -- 2.43.0