Fix overflow issue
[platform/upstream/libxkbcommon.git] / src / text.c
index 133094a..e85b0eb 100644 (file)
@@ -24,6 +24,8 @@
  *
  ********************************************************/
 
+#include "config.h"
+
 #include "keymap.h"
 #include "text.h"
 
@@ -204,6 +206,7 @@ const LookupEntry symInterpretMatchMaskNames[] = {
     { "AnyOf", MATCH_ANY },
     { "AllOf", MATCH_ALL },
     { "Exactly", MATCH_EXACTLY },
+    { NULL, 0 },
 };
 
 const char *
@@ -213,10 +216,10 @@ ModIndexText(struct xkb_context *ctx, const struct xkb_mod_set *mods,
     if (ndx == XKB_MOD_INVALID)
         return "none";
 
-    if (ndx >= darray_size(mods->mods))
+    if (ndx >= mods->num_mods)
         return NULL;
 
-    return xkb_atom_text(ctx, darray_item(mods->mods, ndx).name);
+    return xkb_atom_text(ctx, mods->mods[ndx].name);
 }
 
 const char *
@@ -254,7 +257,7 @@ const char *
 ModMaskText(struct xkb_context *ctx, const struct xkb_mod_set *mods,
             xkb_mod_mask_t mask)
 {
-    char buf[1024];
+    char buf[1024] = {0};
     size_t pos = 0;
     xkb_mod_index_t i;
     const struct xkb_mod *mod;
@@ -280,13 +283,13 @@ ModMaskText(struct xkb_context *ctx, const struct xkb_mod_set *mods,
             pos += ret;
     }
 
-    return strcpy(xkb_context_get_buffer(ctx, pos + 1), buf);
+    return strncpy(xkb_context_get_buffer(ctx, pos + 1), buf, pos + 1);
 }
 
 const char *
 LedStateMaskText(struct xkb_context *ctx, enum xkb_state_component mask)
 {
-    char buf[1024];
+    char buf[1024] = {0};
     size_t pos = 0;
 
     if (mask == 0)
@@ -309,13 +312,13 @@ LedStateMaskText(struct xkb_context *ctx, enum xkb_state_component mask)
             pos += ret;
     }
 
-    return strcpy(xkb_context_get_buffer(ctx, pos + 1), buf);
+    return strncpy(xkb_context_get_buffer(ctx, pos + 1), buf, pos + 1);
 }
 
 const char *
 ControlMaskText(struct xkb_context *ctx, enum xkb_action_controls mask)
 {
-    char buf[1024];
+    char buf[1024] = {0};
     size_t pos = 0;
 
     if (mask == 0)
@@ -341,5 +344,5 @@ ControlMaskText(struct xkb_context *ctx, enum xkb_action_controls mask)
             pos += ret;
     }
 
-    return strcpy(xkb_context_get_buffer(ctx, pos + 1), buf);
+    return strncpy(xkb_context_get_buffer(ctx, pos + 1), buf, pos + 1);
 }