nbd: Avoid off-by-one in long export name truncation
authorEric Blake <eblake@redhat.com>
Mon, 22 Jun 2020 21:03:55 +0000 (16:03 -0500)
committerEric Blake <eblake@redhat.com>
Mon, 13 Jul 2020 14:01:01 +0000 (09:01 -0500)
When snprintf returns the same value as the buffer size, the final
byte was truncated to ensure a NUL terminator.  Fortunately, such long
export names are unusual enough, with no real impact other than what
is displayed to the user.

Fixes: 5c86bdf12089
Reported-by: Max Reitz <mreitz@redhat.com>
Signed-off-by: Eric Blake <eblake@redhat.com>
Message-Id: <20200622210355.414941-1-eblake@redhat.com>
Reviewed-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
block/nbd.c

index c297336ffc5f04465b67f0cbc20d3daee2d559d1..65a4f56924ec173688885e26472854e4b3917e9b 100644 (file)
@@ -2002,7 +2002,7 @@ static void nbd_refresh_filename(BlockDriverState *bs)
         len = snprintf(bs->exact_filename, sizeof(bs->exact_filename),
                        "nbd://%s:%s", host, port);
     }
-    if (len > sizeof(bs->exact_filename)) {
+    if (len >= sizeof(bs->exact_filename)) {
         /* Name is too long to represent exactly, so leave it empty. */
         bs->exact_filename[0] = '\0';
     }