]> xenbits.xensource.com Git - qemu-xen.git/commitdiff
qstring: Move qstring_from_substr()'s @end one to the right
authorMarkus Armbruster <armbru@redhat.com>
Fri, 27 Jul 2018 06:22:04 +0000 (08:22 +0200)
committerMarkus Armbruster <armbru@redhat.com>
Sat, 28 Jul 2018 07:09:58 +0000 (09:09 +0200)
qstring_from_substr() takes the index of the substring's first and
last character.  qstring_from_substr(s, 0, SIZE_MAX) denotes an empty
substring.  Awkward.

Shift the end index one to the right.  This simplifies both
qstring_from_substr() and its callers.

Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Message-Id: <20180727062204.10401-3-armbru@redhat.com>

block/blkdebug.c
block/blkverify.c
block/nbd.c
qobject/qstring.c
tests/check-qobject.c
tests/check-qstring.c

index 0457bf5b66d185a73f9df751e442662536a2bb7d..0759452925b892d2f12206f21ef825bc5bf87713 100644 (file)
@@ -305,7 +305,7 @@ static void blkdebug_parse_filename(const char *filename, QDict *options,
 
     if (c != filename) {
         QString *config_path;
-        config_path = qstring_from_substr(filename, 0, c - filename - 1);
+        config_path = qstring_from_substr(filename, 0, c - filename);
         qdict_put(options, "config", config_path);
     }
 
index da97ee592767b165c17d49c3addb05645bb3a8b7..89bf4386e32eee170a42999beaf5d20d8c9118ee 100644 (file)
@@ -80,7 +80,7 @@ static void blkverify_parse_filename(const char *filename, QDict *options,
     }
 
     /* TODO Implement option pass-through and set raw.filename here */
-    raw_path = qstring_from_substr(filename, 0, c - filename - 1);
+    raw_path = qstring_from_substr(filename, 0, c - filename);
     qdict_put(options, "x-raw", raw_path);
 
     /* TODO Allow multi-level nesting and set file.filename here */
index b198ad775fb903868e60a24a8535b3f610a4dbb0..e87699fb73b13646e0bebcc4ee0ea5431d5c215d 100644 (file)
@@ -109,7 +109,7 @@ static int nbd_parse_uri(const char *filename, QDict *options)
         /* strip braces from literal IPv6 address */
         if (uri->server[0] == '[') {
             host = qstring_from_substr(uri->server, 1,
-                                       strlen(uri->server) - 2);
+                                       strlen(uri->server) - 1);
         } else {
             host = qstring_from_str(uri->server);
         }
index 1bb7784a88e191f617944899cb151fa76c7f504a..0f1510e792fec3861ef61108aa859b18255ebcc6 100644 (file)
@@ -41,12 +41,12 @@ QString *qstring_from_substr(const char *str, size_t start, size_t end)
 {
     QString *qstring;
 
-    assert(start <= end + 1);
+    assert(start <= end);
 
     qstring = g_malloc(sizeof(*qstring));
     qobject_init(QOBJECT(qstring), QTYPE_QSTRING);
 
-    qstring->length = end - start + 1;
+    qstring->length = end - start;
     qstring->capacity = qstring->length;
 
     assert(qstring->capacity < SIZE_MAX);
@@ -64,7 +64,7 @@ QString *qstring_from_substr(const char *str, size_t start, size_t end)
  */
 QString *qstring_from_str(const char *str)
 {
-    return qstring_from_substr(str, 0, strlen(str) - 1);
+    return qstring_from_substr(str, 0, strlen(str));
 }
 
 static void capacity_increase(QString *qstring, size_t len)
index 16ccbde82cc32ba682a13855e86d65561ea48f64..593c3a0618c4cff5355b27f36b7e41314718344a 100644 (file)
@@ -154,7 +154,7 @@ static void qobject_is_equal_string_test(void)
     str_case = qstring_from_str("Foo");
 
     /* Should yield "foo" */
-    str_built = qstring_from_substr("form", 0, 1);
+    str_built = qstring_from_substr("form", 0, 2);
     qstring_append_chr(str_built, 'o');
 
     check_unequal(str_base, str_whitespace_0, str_whitespace_1,
index f11a7a860572a25681783c1fa7781f42d73451ff..2d079921e3e04f8319e9e9786a60da9ea8da1cf2 100644 (file)
@@ -66,7 +66,7 @@ static void qstring_from_substr_test(void)
 {
     QString *qs;
 
-    qs = qstring_from_substr("virtualization", 3, 9);
+    qs = qstring_from_substr("virtualization", 3, 10);
     g_assert(qs != NULL);
     g_assert(strcmp(qstring_get_str(qs), "tualiza") == 0);