mirror of
https://gitlab.gnome.org/GNOME/glib.git
synced 2024-12-26 15:36:14 +01:00
Fix g_string_chunk_insert_len to accept nuls
Contrary to what the documentation says, g_string_chunk_insert_len was stopping at the first nul. Also add a test. Fixes bug 585088.
This commit is contained in:
parent
9e43937d03
commit
6224d3d2ec
@ -307,7 +307,7 @@ g_string_chunk_insert_const (GStringChunk *chunk,
|
||||
* Return value: a pointer to the copy of @string within the #GStringChunk
|
||||
*
|
||||
* Since: 2.4
|
||||
**/
|
||||
*/
|
||||
gchar*
|
||||
g_string_chunk_insert_len (GStringChunk *chunk,
|
||||
const gchar *string,
|
||||
@ -338,9 +338,7 @@ g_string_chunk_insert_len (GStringChunk *chunk,
|
||||
|
||||
*(pos + size) = '\0';
|
||||
|
||||
strncpy (pos, string, size);
|
||||
if (len > 0)
|
||||
size = strlen (pos);
|
||||
memcpy (pos, string, size);
|
||||
|
||||
chunk->storage_next += size + 1;
|
||||
|
||||
|
@ -49,6 +49,28 @@ test_string_chunks (void)
|
||||
g_string_chunk_free (string_chunk);
|
||||
}
|
||||
|
||||
static void
|
||||
test_string_chunk_insert (void)
|
||||
{
|
||||
const gchar s0[] = "Testing GStringChunk";
|
||||
const gchar s1[] = "a\0b\0c\0d\0";
|
||||
const gchar s2[] = "Hello, world";
|
||||
GStringChunk *chunk;
|
||||
gchar *str[3];
|
||||
|
||||
chunk = g_string_chunk_new (512);
|
||||
|
||||
str[0] = g_string_chunk_insert (chunk, s0);
|
||||
str[1] = g_string_chunk_insert_len (chunk, s1, 8);
|
||||
str[2] = g_string_chunk_insert (chunk, s2);
|
||||
|
||||
g_assert (memcmp (s0, str[0], sizeof s0) == 0);
|
||||
g_assert (memcmp (s1, str[1], sizeof s1) == 0);
|
||||
g_assert (memcmp (s2, str[2], sizeof s2) == 0);
|
||||
|
||||
g_string_chunk_free (chunk);
|
||||
}
|
||||
|
||||
static void
|
||||
test_string_new (void)
|
||||
{
|
||||
@ -376,6 +398,7 @@ main (int argc,
|
||||
g_test_init (&argc, &argv, NULL);
|
||||
|
||||
g_test_add_func ("/string/test-string-chunks", test_string_chunks);
|
||||
g_test_add_func ("/string/test-string-chunk-insert", test_string_chunk_insert);
|
||||
g_test_add_func ("/string/test-string-new", test_string_new);
|
||||
g_test_add_func ("/string/test-string-printf", test_string_printf);
|
||||
g_test_add_func ("/string/test-string-assign", test_string_assign);
|
||||
|
Loading…
Reference in New Issue
Block a user