From 58734d2538854ef79e6605ba69c57723737d12c2 Mon Sep 17 00:00:00 2001 From: Zdenek Kabelac Date: Fri, 10 Feb 2012 18:34:12 +0100 Subject: [PATCH] dmsetup: check for strncpy Test whether device name fits into a given buffer. --- tools/dmsetup.c | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/tools/dmsetup.c b/tools/dmsetup.c index ce46d6c71..8f6b34027 100644 --- a/tools/dmsetup.c +++ b/tools/dmsetup.c @@ -3244,20 +3244,19 @@ static char *parse_loop_device_name(const char *dev, const char *dev_dir) device[strlen(dev_dir)] != '/') goto error; - strncpy(buf, strrchr(device, '/') + 1, PATH_MAX - 1); - buf[PATH_MAX - 1] = '\0'; + if (!dm_strncpy(buf, strrchr(device, '/') + 1, PATH_MAX)) + goto error; dm_free(device); - } else { /* check for device number */ - if (!strncmp(dev, "loop", strlen("loop"))) - strncpy(buf, dev, (size_t) PATH_MAX); - else + if (strncmp(dev, "loop", sizeof("loop") - 1)) + goto error; + + if (!dm_strncpy(buf, dev, PATH_MAX)) goto error; } return buf; - error: dm_free(device); dm_free(buf);