aboutsummaryrefslogtreecommitdiff
path: root/gcc
diff options
context:
space:
mode:
authorDavid Malcolm <dmalcolm@redhat.com>2025-07-11 14:58:20 -0400
committerDavid Malcolm <dmalcolm@redhat.com>2025-07-11 14:58:20 -0400
commit1ea72a15031cd8aaea0f03449595bfeb4c45d167 (patch)
tree97d3dbf7a07291b0aa5a110690d1f6175802b51e /gcc
parent457464edf19f17e2d30fe04c997144aa2cc5b99c (diff)
downloadgcc-1ea72a15031cd8aaea0f03449595bfeb4c45d167.zip
gcc-1ea72a15031cd8aaea0f03449595bfeb4c45d167.tar.gz
gcc-1ea72a15031cd8aaea0f03449595bfeb4c45d167.tar.bz2
json: fix null-termination of json::string
gcc/ChangeLog: * json.cc (string::string): When constructing from pointer and length, ensure the new buffer is null-terminated. (selftest::test_strcmp): New. (selftest::json_cc_tests): Likewise. Signed-off-by: David Malcolm <dmalcolm@redhat.com>
Diffstat (limited to 'gcc')
-rw-r--r--gcc/json.cc13
1 files changed, 12 insertions, 1 deletions
diff --git a/gcc/json.cc b/gcc/json.cc
index f3f3645..df0702f 100644
--- a/gcc/json.cc
+++ b/gcc/json.cc
@@ -501,9 +501,10 @@ string::string (const char *utf8)
string::string (const char *utf8, size_t len)
{
gcc_assert (utf8);
- m_utf8 = XNEWVEC (char, len);
+ m_utf8 = XNEWVEC (char, len + 1);
m_len = len;
memcpy (m_utf8, utf8, len);
+ m_utf8[len] = '\0';
}
/* Implementation of json::value::print for json::string. */
@@ -914,6 +915,15 @@ test_comparisons ()
ASSERT_JSON_NE (arr_1, arr_2);
}
+/* Ensure that json::string's get_string is usable as a C-style string. */
+
+static void
+test_strcmp ()
+{
+ string str ("foobar", 3);
+ ASSERT_EQ (strcmp (str.get_string (), "foo"), 0);
+}
+
/* Run all of the selftests within this file. */
void
@@ -928,6 +938,7 @@ json_cc_tests ()
test_writing_literals ();
test_formatting ();
test_comparisons ();
+ test_strcmp ();
}
} // namespace selftest