Commit c00ea5ff8e43cb9a411cc2325b6534c698df47ad

Ran Benita 2013-07-22T10:51:22

atom: really work with non-NUL-terminated strings Signed-off-by: Ran Benita <ran234@gmail.com>

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;