atom: really work with non-NUL-terminated strings Signed-off-by: Ran Benita <ran234@gmail.com>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
diff --git a/src/atom.c b/src/atom.c
index cbc7a14..a7033c4 100644
--- a/src/atom.c
+++ b/src/atom.c
@@ -210,7 +210,7 @@ atom_intern(struct atom_table *table, const char *string, size_t len,
struct atom_node *node;
unsigned int fingerprint;
- if (!string)
+ if (!string || len == 0)
return XKB_ATOM_NONE;
if (find_node_pointer(table, string, len, &nodep, &fingerprint)) {
@@ -227,7 +227,7 @@ atom_intern(struct atom_table *table, const char *string, size_t len,
node->string = UNCONSTIFY(string);
}
else {
- node->string = strdup(string);
+ node->string = strndup(string, len);
if (!node->string) {
free(node);
return XKB_ATOM_NONE;
diff --git a/test/context.c b/test/context.c
index 63813f1..dc60888 100644
--- a/test/context.c
+++ b/test/context.c
@@ -30,6 +30,7 @@ int
main(void)
{
struct xkb_context *context = test_get_context(0);
+ xkb_atom_t atom;
assert(context);
@@ -37,6 +38,14 @@ main(void)
assert(!xkb_context_include_path_append(context, "¡NONSENSE!"));
assert(xkb_context_num_include_paths(context) == 1);
+ atom = xkb_atom_intern(context, "HELLOjunkjunkjunk", 5);
+ assert(atom != XKB_ATOM_NONE);
+ assert(streq(xkb_atom_text(context, atom), "HELLO"));
+
+ atom = xkb_atom_intern_literal(context, "HELLOjunkjunkjunk");
+ assert(atom != XKB_ATOM_NONE);
+ assert(streq(xkb_atom_text(context, atom), "HELLOjunkjunkjunk"));
+
xkb_context_unref(context);
return 0;