Fix NULL after deref issue detected by static analysis tool
[platform/upstream/libxkbcommon.git] / src / xkbcomp / types.c
index d5a0380..fa12c23 100644 (file)
@@ -24,6 +24,8 @@
  *
  ********************************************************/
 
+#include "config.h"
+
 #include "xkbcomp-priv.h"
 #include "text.h"
 #include "vmod.h"
@@ -31,9 +33,9 @@
 #include "include.h"
 
 enum type_field {
-    TYPE_FIELD_MASK       = (1 << 0),
-    TYPE_FIELD_MAP        = (1 << 1),
-    TYPE_FIELD_PRESERVE   = (1 << 2),
+    TYPE_FIELD_MASK = (1 << 0),
+    TYPE_FIELD_MAP = (1 << 1),
+    TYPE_FIELD_PRESERVE = (1 << 2),
     TYPE_FIELD_LEVEL_NAME = (1 << 3),
 };
 
@@ -53,8 +55,9 @@ typedef struct {
     int errorCount;
 
     darray(KeyTypeInfo) types;
+    struct xkb_mod_set mods;
+
     struct xkb_context *ctx;
-    struct xkb_keymap *keymap;
 } KeyTypesInfo;
 
 /***====================================================================***/
@@ -62,7 +65,7 @@ typedef struct {
 static inline const char *
 MapEntryTxt(KeyTypesInfo *info, struct xkb_key_type_entry *entry)
 {
-    return ModMaskText(info->keymap, entry->mods.mods);
+    return ModMaskText(info->ctx, &info->mods, entry->mods.mods);
 }
 
 static inline const char *
@@ -74,7 +77,7 @@ TypeTxt(KeyTypesInfo *info, KeyTypeInfo *type)
 static inline const char *
 TypeMaskTxt(KeyTypesInfo *info, KeyTypeInfo *type)
 {
-    return ModMaskText(info->keymap, type->mods);
+    return ModMaskText(info->ctx, &info->mods, type->mods);
 }
 
 static inline bool
@@ -86,21 +89,22 @@ ReportTypeShouldBeArray(KeyTypesInfo *info, KeyTypeInfo *type,
 }
 
 static inline bool
-ReportTypeBadType(KeyTypesInfo *info, KeyTypeInfo *type,
-                  const char *field, const char *wanted)
+ReportTypeBadType(KeyTypesInfo *info, xkb_message_code_t code,
+                  KeyTypeInfo *type, const char *field, const char *wanted)
 {
-    return ReportBadType(info->ctx, "key type", field,
+    return ReportBadType(info->ctx, code, "key type", field,
                          TypeTxt(info, type), wanted);
 }
 
 /***====================================================================***/
 
 static void
-InitKeyTypesInfo(KeyTypesInfo *info, struct xkb_keymap *keymap)
+InitKeyTypesInfo(KeyTypesInfo *info, struct xkb_context *ctx,
+                 const struct xkb_mod_set *mods)
 {
     memset(info, 0, sizeof(*info));
-    info->keymap = keymap;
-    info->ctx = keymap->ctx;
+    info->ctx = ctx;
+    info->mods = *mods;
 }
 
 static void
@@ -140,6 +144,7 @@ AddKeyType(KeyTypesInfo *info, KeyTypeInfo *new, bool same_file)
         if (new->merge == MERGE_REPLACE || new->merge == MERGE_OVERRIDE) {
             if ((same_file && verbosity > 0) || verbosity > 9) {
                 log_warn(info->ctx,
+                         XKB_WARNING_CONFLICTING_KEY_TYPE_DEFINITIONS,
                          "Multiple definitions of the %s key type; "
                          "Earlier definition ignored\n",
                          xkb_atom_text(info->ctx, new->name));
@@ -154,6 +159,7 @@ AddKeyType(KeyTypesInfo *info, KeyTypeInfo *new, bool same_file)
 
         if (same_file)
             log_vrb(info->ctx, 4,
+                    XKB_WARNING_CONFLICTING_KEY_TYPE_DEFINITIONS,
                     "Multiple definitions of the %s key type; "
                     "Later definition ignored\n",
                     xkb_atom_text(info->ctx, new->name));
@@ -172,13 +178,13 @@ static void
 MergeIncludedKeyTypes(KeyTypesInfo *into, KeyTypesInfo *from,
                       enum merge_mode merge)
 {
-    KeyTypeInfo *type;
-
     if (from->errorCount > 0) {
         into->errorCount += from->errorCount;
         return;
     }
 
+    into->mods = from->mods;
+
     if (into->name == NULL) {
         into->name = from->name;
         from->name = NULL;
@@ -189,6 +195,7 @@ MergeIncludedKeyTypes(KeyTypesInfo *into, KeyTypesInfo *from,
         darray_init(from->types);
     }
     else {
+        KeyTypeInfo *type;
         darray_foreach(type, from->types) {
             type->merge = (merge == MERGE_DEFAULT ? type->merge : merge);
             if (!AddKeyType(into, type, false))
@@ -205,7 +212,10 @@ HandleIncludeKeyTypes(KeyTypesInfo *info, IncludeStmt *include)
 {
     KeyTypesInfo included;
 
-    InitKeyTypesInfo(&included, info->keymap);
+    if (!include)
+        return false;
+
+    InitKeyTypesInfo(&included, info->ctx, &info->mods);
     included.name = include->stmt;
     include->stmt = NULL;
 
@@ -220,7 +230,7 @@ HandleIncludeKeyTypes(KeyTypesInfo *info, IncludeStmt *include)
             return false;
         }
 
-        InitKeyTypesInfo(&next_incl, info->keymap);
+        InitKeyTypesInfo(&next_incl, info->ctx, &included.mods);
 
         HandleKeyTypesFile(&next_incl, file, stmt->merge);
 
@@ -245,24 +255,25 @@ SetModifiers(KeyTypesInfo *info, KeyTypeInfo *type, ExprDef *arrayNdx,
     xkb_mod_mask_t mods;
 
     if (arrayNdx)
-        log_warn(info->ctx,
+        log_warn(info->ctx, XKB_LOG_MESSAGE_NO_ID,
                  "The modifiers field of a key type is not an array; "
                  "Illegal array subscript ignored\n");
 
-    if (!ExprResolveModMask(info->keymap, value, MOD_BOTH, &mods)) {
+    if (!ExprResolveModMask(info->ctx, value, MOD_BOTH, &info->mods, &mods)) {
         log_err(info->ctx,
+                XKB_ERROR_UNSUPPORTED_MODIFIER_MASK,
                 "Key type mask field must be a modifier mask; "
                 "Key type definition ignored\n");
         return false;
     }
 
     if (type->defined & TYPE_FIELD_MASK) {
-        log_warn(info->ctx,
+        log_warn(info->ctx, XKB_LOG_MESSAGE_NO_ID,
                  "Multiple modifier mask definitions for key type %s; "
                  "Using %s, ignoring %s\n",
                  xkb_atom_text(info->ctx, type->name),
                  TypeMaskTxt(info, type),
-                 ModMaskText(info->keymap, mods));
+                 ModMaskText(info->ctx, &info->mods, mods));
         return false;
     }
 
@@ -294,6 +305,7 @@ AddMapEntry(KeyTypesInfo *info, KeyTypeInfo *type,
     if (old) {
         if (report && old->level != new->level) {
             log_warn(info->ctx,
+                     XKB_WARNING_CONFLICTING_KEY_TYPE_MAP_ENTRY,
                      "Multiple map entries for %s in %s; "
                      "Using %d, ignoring %d\n",
                      MapEntryTxt(info, new), TypeTxt(info, type),
@@ -302,6 +314,7 @@ AddMapEntry(KeyTypesInfo *info, KeyTypeInfo *type,
         }
         else {
             log_vrb(info->ctx, 10,
+                    XKB_WARNING_CONFLICTING_KEY_TYPE_MAP_ENTRY,
                     "Multiple occurrences of map[%s]= %d in %s; Ignored\n",
                     MapEntryTxt(info, new), new->level + 1,
                     TypeTxt(info, type));
@@ -333,23 +346,27 @@ SetMapEntry(KeyTypesInfo *info, KeyTypeInfo *type, ExprDef *arrayNdx,
     if (arrayNdx == NULL)
         return ReportTypeShouldBeArray(info, type, "map entry");
 
-    if (!ExprResolveModMask(info->keymap, arrayNdx, MOD_BOTH, &entry.mods.mods))
-        return ReportTypeBadType(info, type, "map entry", "modifier mask");
+    if (!ExprResolveModMask(info->ctx, arrayNdx, MOD_BOTH, &info->mods,
+                            &entry.mods.mods))
+        return ReportTypeBadType(info, XKB_ERROR_UNSUPPORTED_MODIFIER_MASK,
+                                 type, "map entry", "modifier mask");
 
     if (entry.mods.mods & (~type->mods)) {
         log_vrb(info->ctx, 1,
-                "Map entry for unused modifiers in %s; "
+                XKB_WARNING_UNDECLARED_MODIFIERS_IN_KEY_TYPE,
+                "Map entry for modifiers not used by type %s; "
                 "Using %s instead of %s\n",
                 TypeTxt(info, type),
-                ModMaskText(info->keymap, entry.mods.mods & type->mods),
+                ModMaskText(info->ctx, &info->mods,
+                            entry.mods.mods & type->mods),
                 MapEntryTxt(info, &entry));
         entry.mods.mods &= type->mods;
     }
 
     if (!ExprResolveLevel(info->ctx, value, &entry.level)) {
-        log_err(info->ctx,
-                "Level specifications in a key type must be integer; "
-                "Ignoring malformed level specification\n");
+        log_err(info->ctx, XKB_ERROR_UNSUPPORTED_SHIFT_LEVEL,
+                          "Level specifications in a key type must be integer; "
+                          "Ignoring malformed level specification\n");
         return false;
     }
 
@@ -380,21 +397,23 @@ AddPreserve(KeyTypesInfo *info, KeyTypeInfo *type,
         /* Map exists with same preserve; do nothing. */
         if (entry->preserve.mods == preserve_mods) {
             log_vrb(info->ctx, 10,
+                    XKB_WARNING_DUPLICATE_ENTRY,
                     "Identical definitions for preserve[%s] in %s; "
                     "Ignored\n",
-                    ModMaskText(info->keymap, mods),
+                    ModMaskText(info->ctx, &info->mods, mods),
                     TypeTxt(info, type));
             return true;
         }
 
         /* Map exists with different preserve; latter wins. */
         log_vrb(info->ctx, 1,
+                XKB_WARNING_CONFLICTING_KEY_TYPE_PRESERVE_ENTRIES,
                 "Multiple definitions for preserve[%s] in %s; "
                 "Using %s, ignoring %s\n",
-                ModMaskText(info->keymap, mods),
+                ModMaskText(info->ctx, &info->mods, mods),
                 TypeTxt(info, type),
-                ModMaskText(info->keymap, preserve_mods),
-                ModMaskText(info->keymap, entry->preserve.mods));
+                ModMaskText(info->ctx, &info->mods, preserve_mods),
+                ModMaskText(info->ctx, &info->mods, entry->preserve.mods));
 
         entry->preserve.mods = preserve_mods;
         return true;
@@ -421,28 +440,31 @@ SetPreserve(KeyTypesInfo *info, KeyTypeInfo *type, ExprDef *arrayNdx,
     if (arrayNdx == NULL)
         return ReportTypeShouldBeArray(info, type, "preserve entry");
 
-    if (!ExprResolveModMask(info->keymap, arrayNdx, MOD_BOTH, &mods))
-        return ReportTypeBadType(info, type, "preserve entry",
-                                 "modifier mask");
+    if (!ExprResolveModMask(info->ctx, arrayNdx, MOD_BOTH, &info->mods, &mods))
+        return ReportTypeBadType(info, XKB_ERROR_UNSUPPORTED_MODIFIER_MASK,
+                                 type, "preserve entry", "modifier mask");
 
     if (mods & ~type->mods) {
         const char *before, *after;
 
-        before = ModMaskText(info->keymap, mods);
+        before = ModMaskText(info->ctx, &info->mods, mods);
         mods &= type->mods;
-        after = ModMaskText(info->keymap, mods);
+        after = ModMaskText(info->ctx, &info->mods, mods);
 
         log_vrb(info->ctx, 1,
-                "Preserve for modifiers not used by the %s type; "
+                XKB_WARNING_UNDECLARED_MODIFIERS_IN_KEY_TYPE,
+                "Preserve entry for modifiers not used by the %s type; "
                 "Index %s converted to %s\n",
                 TypeTxt(info, type), before, after);
     }
 
-    if (!ExprResolveModMask(info->keymap, value, MOD_BOTH, &preserve_mods)) {
+    if (!ExprResolveModMask(info->ctx, value, MOD_BOTH, &info->mods,
+                            &preserve_mods)) {
         log_err(info->ctx,
+                XKB_ERROR_UNSUPPORTED_MODIFIER_MASK,
                 "Preserve value in a key type is not a modifier mask; "
                 "Ignoring preserve[%s] in type %s\n",
-                ModMaskText(info->keymap, mods),
+                ModMaskText(info->ctx, &info->mods, mods),
                 TypeTxt(info, type));
         return false;
     }
@@ -450,14 +472,15 @@ SetPreserve(KeyTypesInfo *info, KeyTypeInfo *type, ExprDef *arrayNdx,
     if (preserve_mods & ~mods) {
         const char *before, *after;
 
-        before = ModMaskText(info->keymap, preserve_mods);
+        before = ModMaskText(info->ctx, &info->mods, preserve_mods);
         preserve_mods &= mods;
-        after = ModMaskText(info->keymap, preserve_mods);
+        after = ModMaskText(info->ctx, &info->mods, preserve_mods);
 
         log_vrb(info->ctx, 1,
+                XKB_WARNING_ILLEGAL_KEY_TYPE_PRESERVE_RESULT,
                 "Illegal value for preserve[%s] in type %s; "
                 "Converted %s to %s\n",
-                ModMaskText(info->keymap, mods),
+                ModMaskText(info->ctx, &info->mods, mods),
                 TypeTxt(info, type), before, after);
     }
 
@@ -479,6 +502,7 @@ AddLevelName(KeyTypesInfo *info, KeyTypeInfo *type,
     /* Same level, same name. */
     if (darray_item(type->level_names, level) == name) {
         log_vrb(info->ctx, 10,
+                XKB_WARNING_DUPLICATE_ENTRY,
                 "Duplicate names for level %d of key type %s; Ignored\n",
                 level + 1, TypeTxt(info, type));
         return true;
@@ -491,6 +515,7 @@ AddLevelName(KeyTypesInfo *info, KeyTypeInfo *type,
                             darray_item(type->level_names, level));
         new = xkb_atom_text(info->ctx, name);
         log_vrb(info->ctx, 1,
+                XKB_WARNING_CONFLICTING_KEY_TYPE_LEVEL_NAMES,
                 "Multiple names for level %d of key type %s; "
                 "Using %s, ignoring %s\n",
                 level + 1, TypeTxt(info, type),
@@ -518,10 +543,12 @@ SetLevelName(KeyTypesInfo *info, KeyTypeInfo *type, ExprDef *arrayNdx,
         return ReportTypeShouldBeArray(info, type, "level name");
 
     if (!ExprResolveLevel(info->ctx, arrayNdx, &level))
-        return ReportTypeBadType(info, type, "level name", "integer");
+        return ReportTypeBadType(info, XKB_ERROR_UNSUPPORTED_SHIFT_LEVEL,
+                                 type, "level name", "integer");
 
     if (!ExprResolveString(info->ctx, value, &level_name)) {
         log_err(info->ctx,
+                XKB_ERROR_WRONG_FIELD_TYPE,
                 "Non-string name for level %d in key type %s; "
                 "Ignoring illegal level name definition\n",
                 level + 1, xkb_atom_text(info->ctx, type->name));
@@ -557,6 +584,7 @@ SetKeyTypeField(KeyTypesInfo *info, KeyTypeInfo *type,
         ok = SetLevelName(info, type, arrayNdx, value);
     } else {
         log_err(info->ctx,
+                XKB_ERROR_UNKNOWN_FIELD,
                 "Unknown field %s in key type %s; Definition ignored\n",
                 field, TypeTxt(info, type));
     }
@@ -580,6 +608,7 @@ HandleKeyTypeBody(KeyTypesInfo *info, VarDef *def, KeyTypeInfo *type)
 
         if (elem && istreq(elem, "type")) {
             log_err(info->ctx,
+                    XKB_ERROR_INVALID_SET_DEFAULT_STATEMENT,
                     "Support for changing the default type has been removed; "
                     "Statement ignored\n");
             continue;
@@ -635,15 +664,17 @@ HandleKeyTypesFile(KeyTypesInfo *info, XkbFile *file, enum merge_mode merge)
             break;
         case STMT_VAR:
             log_err(info->ctx,
+                    XKB_ERROR_WRONG_STATEMENT_TYPE,
                     "Support for changing the default type has been removed; "
                     "Statement ignored\n");
             ok = true;
             break;
         case STMT_VMOD:
-            ok = HandleVModDef(info->keymap, (VModDef *) stmt, merge);
+            ok = HandleVModDef(info->ctx, &info->mods, (VModDef *) stmt, merge);
             break;
         default:
             log_err(info->ctx,
+                    XKB_ERROR_WRONG_STATEMENT_TYPE,
                     "Key type files may not include other declarations; "
                     "Ignoring %s\n", stmt_type_to_string(stmt->type));
             ok = false;
@@ -655,7 +686,8 @@ HandleKeyTypesFile(KeyTypesInfo *info, XkbFile *file, enum merge_mode merge)
 
         if (info->errorCount > 10) {
             log_err(info->ctx,
-                    "Abandoning keytypes file \"%s\"\n", file->topName);
+                    XKB_ERROR_INVALID_SYNTAX,
+                    "Abandoning keytypes file \"%s\"\n", file->name);
             break;
         }
     }
@@ -666,21 +698,20 @@ HandleKeyTypesFile(KeyTypesInfo *info, XkbFile *file, enum merge_mode merge)
 static bool
 CopyKeyTypesToKeymap(struct xkb_keymap *keymap, KeyTypesInfo *info)
 {
-    keymap->types_section_name = strdup_safe(info->name);
-    XkbEscapeMapName(keymap->types_section_name);
+    unsigned num_types;
+    struct xkb_key_type *types;
 
-    keymap->num_types = darray_size(info->types);
-    if (keymap->num_types == 0)
-        keymap->num_types = 1;
-
-    keymap->types = calloc(keymap->num_types, sizeof(*keymap->types));
+    num_types = darray_empty(info->types) ? 1 : darray_size(info->types);
+    types = calloc(num_types, sizeof(*types));
+    if (!types)
+        return false;
 
     /*
      * If no types were specified, a default unnamed one-level type is
      * used for all keys.
      */
     if (darray_empty(info->types)) {
-        struct xkb_key_type *type = &keymap->types[0];
+        struct xkb_key_type *type = &types[0];
 
         type->mods.mods = 0;
         type->num_levels = 1;
@@ -688,24 +719,26 @@ CopyKeyTypesToKeymap(struct xkb_keymap *keymap, KeyTypesInfo *info)
         type->num_entries = 0;
         type->name = xkb_atom_intern_literal(keymap->ctx, "default");
         type->level_names = NULL;
-
-        return true;
+        type->num_level_names = 0;
     }
-
-    for (unsigned i = 0; i < keymap->num_types; i++) {
-        KeyTypeInfo *def = &darray_item(info->types, i);
-        struct xkb_key_type *type = &keymap->types[i];
-
-        type->mods.mods = def->mods;
-        type->num_levels = def->num_levels;
-        type->entries = darray_mem(def->entries, 0);
-        type->num_entries = darray_size(def->entries);
-        darray_init(def->entries);
-        type->name = def->name;
-        type->level_names = darray_mem(def->level_names, 0);
-        darray_init(def->level_names);
+    else {
+        for (unsigned i = 0; i < num_types; i++) {
+            KeyTypeInfo *def = &darray_item(info->types, i);
+            struct xkb_key_type *type = &types[i];
+
+            type->name = def->name;
+            type->mods.mods = def->mods;
+            type->num_levels = def->num_levels;
+            darray_steal(def->level_names, &type->level_names, &type->num_level_names);
+            darray_steal(def->entries, &type->entries, &type->num_entries);
+        }
     }
 
+    keymap->types_section_name = strdup_safe(info->name);
+    XkbEscapeMapName(keymap->types_section_name);
+    keymap->num_types = num_types;
+    keymap->types = types;
+    keymap->mods = info->mods;
     return true;
 }
 
@@ -717,7 +750,7 @@ CompileKeyTypes(XkbFile *file, struct xkb_keymap *keymap,
 {
     KeyTypesInfo info;
 
-    InitKeyTypesInfo(&info, keymap);
+    InitKeyTypesInfo(&info, keymap->ctx, &keymap->mods);
 
     HandleKeyTypesFile(&info, file, merge);
     if (info.errorCount != 0)