Fix NULL after deref issue detected by static analysis tool
[platform/upstream/libxkbcommon.git] / src / xkbcomp / symbols.c
index 6d31c7c..58f73e6 100644 (file)
@@ -242,7 +242,7 @@ MergeGroups(SymbolsInfo *info, GroupInfo *into, GroupInfo *from, bool clobber,
             xkb_atom_t ignore = (clobber ? into->type : from->type);
 
             if (report) {
-                log_warn_with_code(info->ctx,
+                log_warn(info->ctx,
                          XKB_WARNING_CONFLICTING_KEY_TYPE_MERGING_GROUPS,
                          "Multiple definitions for group %d type of key %s; "
                          "Using %s, ignoring %s\n",
@@ -288,7 +288,7 @@ MergeGroups(SymbolsInfo *info, GroupInfo *into, GroupInfo *from, bool clobber,
             ignore = (clobber ? &intoLevel->action : &fromLevel->action);
 
             if (report) {
-                log_warn_with_code(info->ctx,
+                log_warn(info->ctx,
                          XKB_WARNING_CONFLICTING_KEY_ACTION,
                          "Multiple actions for level %d/group %u on key %s; "
                          "Using %s, ignoring %s\n",
@@ -313,7 +313,7 @@ MergeGroups(SymbolsInfo *info, GroupInfo *into, GroupInfo *from, bool clobber,
         }
         else if (!XkbLevelsSameSyms(fromLevel, intoLevel)) {
             if (report) {
-                log_warn_with_code(info->ctx,
+                log_warn(info->ctx,
                          XKB_WARNING_CONFLICTING_KEY_SYMBOL,
                          "Multiple symbols for level %d/group %u on key %s; "
                          "Using %s, ignoring %s\n",
@@ -414,7 +414,7 @@ MergeKeys(SymbolsInfo *info, KeyInfo *into, KeyInfo *from, bool same_file)
     }
 
     if (collide) {
-        log_warn_with_code(info->ctx,
+        log_warn(info->ctx,
                  XKB_WARNING_CONFLICTING_KEY_FIELDS,
                  "Symbol map for key %s redefined; "
                  "Using %s definition for conflicting fields\n",
@@ -474,7 +474,7 @@ AddModMapEntry(SymbolsInfo *info, ModMapEntry *new)
         ignore = (clobber ? old->modifier : new->modifier);
 
         if (new->haveSymbol) {
-            log_warn_with_code(info->ctx,
+            log_warn(info->ctx,
                      XKB_WARNING_CONFLICTING_MODMAP,
                      "Symbol \"%s\" added to modifier map for multiple modifiers; "
                      "Using %s, ignoring %s\n",
@@ -482,7 +482,7 @@ AddModMapEntry(SymbolsInfo *info, ModMapEntry *new)
                      ModIndexText(info->ctx, &info->mods, use),
                      ModIndexText(info->ctx, &info->mods, ignore));
         } else {
-            log_warn_with_code(info->ctx,
+            log_warn(info->ctx,
                      XKB_WARNING_CONFLICTING_MODMAP,
                      "Key \"%s\" added to modifier map for multiple modifiers; "
                      "Using %s, ignoring %s\n",
@@ -569,6 +569,9 @@ HandleIncludeSymbols(SymbolsInfo *info, IncludeStmt *include)
 {
     SymbolsInfo included;
 
+    if (!include)
+        return false;
+
     InitSymbolsInfo(&included, info->keymap, info->actions, &info->mods);
     included.name = include->stmt;
     include->stmt = NULL;
@@ -589,7 +592,7 @@ HandleIncludeSymbols(SymbolsInfo *info, IncludeStmt *include)
         if (stmt->modifier) {
             next_incl.explicit_group = atoi(stmt->modifier) - 1;
             if (next_incl.explicit_group >= XKB_MAX_GROUPS) {
-                log_err_with_code(info->ctx,
+                log_err(info->ctx,
                         XKB_ERROR_UNSUPPORTED_GROUP_INDEX,
                         "Cannot set explicit group to %d - must be between 1..%d; "
                         "Ignoring group number\n",
@@ -638,7 +641,7 @@ GetGroupIndex(SymbolsInfo *info, KeyInfo *keyi, ExprDef *arrayNdx,
         }
 
         if (i >= XKB_MAX_GROUPS) {
-            log_err_with_code(info->ctx,
+            log_err(info->ctx,
                     XKB_ERROR_UNSUPPORTED_GROUP_INDEX,
                     "Too many groups of %s for key %s (max %u); "
                     "Ignoring %s defined for extra groups\n",
@@ -652,7 +655,7 @@ GetGroupIndex(SymbolsInfo *info, KeyInfo *keyi, ExprDef *arrayNdx,
     }
 
     if (!ExprResolveGroup(info->ctx, arrayNdx, ndx_rtrn)) {
-        log_err_with_code(info->ctx, XKB_ERROR_UNSUPPORTED_GROUP_INDEX,
+        log_err(info->ctx, XKB_ERROR_UNSUPPORTED_GROUP_INDEX,
                 "Illegal group index for %s of key %s\n"
                 "Definition with non-integer array index ignored\n",
                 name, KeyInfoText(info, keyi));
@@ -685,7 +688,7 @@ AddSymbolsToKey(SymbolsInfo *info, KeyInfo *keyi, ExprDef *arrayNdx,
     }
 
     if (value->expr.op != EXPR_KEYSYM_LIST) {
-        log_err_with_code(info->ctx,
+        log_err(info->ctx,
                 XKB_ERROR_WRONG_FIELD_TYPE,
                 "Expected a list of symbols, found %s; "
                 "Ignoring symbols for group %u of %s\n",
@@ -695,7 +698,7 @@ AddSymbolsToKey(SymbolsInfo *info, KeyInfo *keyi, ExprDef *arrayNdx,
     }
 
     if (groupi->defined & GROUP_FIELD_SYMS) {
-        log_err_with_code(info->ctx,
+        log_err(info->ctx,
                 XKB_ERROR_CONFLICTING_KEY_SYMBOLS_ENTRY,
                 "Symbols for key %s, group %u already defined; "
                 "Ignoring duplicate definition\n",
@@ -715,8 +718,13 @@ AddSymbolsToKey(SymbolsInfo *info, KeyInfo *keyi, ExprDef *arrayNdx,
 
         sym_index = darray_item(value->keysym_list.symsMapIndex, i);
         leveli->num_syms = darray_item(value->keysym_list.symsNumEntries, i);
-        if (leveli->num_syms > 1)
+        if (leveli->num_syms > 1) {
             leveli->u.syms = calloc(leveli->num_syms, sizeof(*leveli->u.syms));
+            if (!leveli->u.syms) {
+                log_err(info->ctx, "Failed to allocate memory for leveli->u.syms\n");
+                continue;
+            }
+        }
 
         for (unsigned j = 0; j < leveli->num_syms; j++) {
             xkb_keysym_t keysym = darray_item(value->keysym_list.syms,
@@ -757,7 +765,7 @@ AddActionsToKey(SymbolsInfo *info, KeyInfo *keyi, ExprDef *arrayNdx,
     }
 
     if (value->expr.op != EXPR_ACTION_LIST) {
-        log_wsgo(info->ctx,
+        log_wsgo(info->ctx, XKB_LOG_MESSAGE_NO_ID,
                  "Bad expression type (%d) for action list value; "
                  "Ignoring actions for group %u of %s\n",
                  value->expr.op, ndx, KeyInfoText(info, keyi));
@@ -765,7 +773,7 @@ AddActionsToKey(SymbolsInfo *info, KeyInfo *keyi, ExprDef *arrayNdx,
     }
 
     if (groupi->defined & GROUP_FIELD_ACTS) {
-        log_wsgo(info->ctx,
+        log_wsgo(info->ctx, XKB_LOG_MESSAGE_NO_ID,
                  "Actions for key %s, group %u already defined\n",
                  KeyInfoText(info, keyi), ndx);
         return false;
@@ -785,7 +793,7 @@ AddActionsToKey(SymbolsInfo *info, KeyInfo *keyi, ExprDef *arrayNdx,
         union xkb_action *toAct = &darray_item(groupi->levels, i).action;
 
         if (!HandleActionDef(info->ctx, info->actions, &info->mods, act, toAct))
-            log_err_with_code(info->ctx,
+            log_err(info->ctx,
                     XKB_ERROR_INVALID_VALUE,
                     "Illegal action definition for %s; "
                     "Action for group %u/level %u ignored\n",
@@ -817,7 +825,7 @@ SetSymbolsField(SymbolsInfo *info, KeyInfo *keyi, const char *field,
         xkb_atom_t val;
 
         if (!ExprResolveString(info->ctx, value, &val)) {
-            log_err_with_code(info->ctx,
+            log_err(info->ctx,
                     XKB_ERROR_WRONG_FIELD_TYPE,
                     "The type field of a key symbol map must be a string; "
                     "Ignoring illegal type definition\n");
@@ -829,7 +837,7 @@ SetSymbolsField(SymbolsInfo *info, KeyInfo *keyi, const char *field,
             keyi->defined |= KEY_FIELD_DEFAULT_TYPE;
         }
         else if (!ExprResolveGroup(info->ctx, arrayNdx, &ndx)) {
-            log_err_with_code(info->ctx, XKB_ERROR_UNSUPPORTED_GROUP_INDEX,
+            log_err(info->ctx, XKB_ERROR_UNSUPPORTED_GROUP_INDEX,
                     "Illegal group index for type of key %s; "
                     "Definition with non-integer array index ignored\n",
                     KeyInfoText(info, keyi));
@@ -856,7 +864,7 @@ SetSymbolsField(SymbolsInfo *info, KeyInfo *keyi, const char *field,
 
         if (!ExprResolveModMask(info->ctx, value, MOD_VIRT, &info->mods,
                                 &mask)) {
-            log_err_with_code(info->ctx,
+            log_err(info->ctx,
                     XKB_ERROR_UNSUPPORTED_MODIFIER_MASK,
                     "Expected a virtual modifier mask, found %s; "
                     "Ignoring virtual modifiers definition for key %s\n",
@@ -900,7 +908,7 @@ SetSymbolsField(SymbolsInfo *info, KeyInfo *keyi, const char *field,
         unsigned int val;
 
         if (!ExprResolveEnum(info->ctx, value, &val, repeatEntries)) {
-            log_err_with_code(info->ctx,
+            log_err(info->ctx,
                     XKB_ERROR_INVALID_VALUE,
                     "Illegal repeat setting for %s; "
                     "Non-boolean repeat setting ignored\n",
@@ -916,7 +924,7 @@ SetSymbolsField(SymbolsInfo *info, KeyInfo *keyi, const char *field,
         bool set;
 
         if (!ExprResolveBoolean(info->ctx, value, &set)) {
-            log_err_with_code(info->ctx,
+            log_err(info->ctx,
                     XKB_ERROR_INVALID_VALUE,
                     "Illegal groupsWrap setting for %s; "
                     "Non-boolean value ignored\n",
@@ -932,7 +940,7 @@ SetSymbolsField(SymbolsInfo *info, KeyInfo *keyi, const char *field,
         bool set;
 
         if (!ExprResolveBoolean(info->ctx, value, &set)) {
-            log_err_with_code(info->ctx,
+            log_err(info->ctx,
                     XKB_ERROR_INVALID_VALUE,
                     "Illegal groupsClamp setting for %s; "
                     "Non-boolean value ignored\n",
@@ -948,7 +956,7 @@ SetSymbolsField(SymbolsInfo *info, KeyInfo *keyi, const char *field,
         xkb_layout_index_t grp;
 
         if (!ExprResolveGroup(info->ctx, value, &grp)) {
-            log_err_with_code(info->ctx, XKB_ERROR_UNSUPPORTED_GROUP_INDEX,
+            log_err(info->ctx, XKB_ERROR_UNSUPPORTED_GROUP_INDEX,
                     "Illegal group index for redirect of key %s; "
                     "Definition with non-integer group ignored\n",
                     KeyInfoText(info, keyi));
@@ -960,7 +968,7 @@ SetSymbolsField(SymbolsInfo *info, KeyInfo *keyi, const char *field,
         keyi->defined |= KEY_FIELD_GROUPINFO;
     }
     else {
-        log_err_with_code(info->ctx,
+        log_err(info->ctx,
                 XKB_ERROR_UNKNOWN_FIELD,
                 "Unknown field %s in a symbol interpretation; "
                 "Definition ignored\n",
@@ -986,14 +994,14 @@ SetGroupName(SymbolsInfo *info, ExprDef *arrayNdx, ExprDef *value)
     }
 
     if (!ExprResolveGroup(info->ctx, arrayNdx, &group)) {
-        log_err_with_code(info->ctx, XKB_ERROR_UNSUPPORTED_GROUP_INDEX,
+        log_err(info->ctx, XKB_ERROR_UNSUPPORTED_GROUP_INDEX,
                 "Illegal index in group name definition; "
                 "Definition with non-integer array index ignored\n");
         return false;
     }
 
     if (!ExprResolveString(info->ctx, value, &name)) {
-        log_err_with_code(info->ctx,
+        log_err(info->ctx,
                 XKB_ERROR_WRONG_FIELD_TYPE,
                 "Group name must be a string; "
                 "Illegal name for group %d ignored\n", group);
@@ -1007,7 +1015,7 @@ SetGroupName(SymbolsInfo *info, ExprDef *arrayNdx, ExprDef *value)
         group_to_use = info->explicit_group;
     }
     else {
-        log_warn_with_code(info->ctx,
+        log_warn(info->ctx,
                  XKB_WARNING_NON_BASE_GROUP_NAME,
                  "An explicit group was specified for the '%s' map, "
                  "but it provides a name for a group other than Group1 (%d); "
@@ -1044,27 +1052,27 @@ HandleGlobalVar(SymbolsInfo *info, VarDef *stmt)
     }
     else if (!elem && (istreq(field, "groupswrap") ||
                        istreq(field, "wrapgroups"))) {
-        log_err_with_code(info->ctx,
+        log_err(info->ctx,
                 XKB_WARNING_UNSUPPORTED_SYMBOLS_FIELD,
                 "Global \"groupswrap\" not supported; Ignored\n");
         ret = true;
     }
     else if (!elem && (istreq(field, "groupsclamp") ||
                        istreq(field, "clampgroups"))) {
-        log_err_with_code(info->ctx,
+        log_err(info->ctx,
                 XKB_WARNING_UNSUPPORTED_SYMBOLS_FIELD,
                 "Global \"groupsclamp\" not supported; Ignored\n");
         ret = true;
     }
     else if (!elem && (istreq(field, "groupsredirect") ||
                        istreq(field, "redirectgroups"))) {
-        log_err_with_code(info->ctx,
+        log_err(info->ctx,
                 XKB_WARNING_UNSUPPORTED_SYMBOLS_FIELD,
                 "Global \"groupsredirect\" not supported; Ignored\n");
         ret = true;
     }
     else if (!elem && istreq(field, "allownone")) {
-        log_err_with_code(info->ctx,
+        log_err(info->ctx,
                 XKB_WARNING_UNSUPPORTED_SYMBOLS_FIELD,
                 "Radio groups not supported; "
                 "Ignoring \"allownone\" specification\n");
@@ -1087,7 +1095,7 @@ HandleSymbolsBody(SymbolsInfo *info, VarDef *def, KeyInfo *keyi)
 
     for (; def; def = (VarDef *) def->common.next) {
         if (def->name && def->name->expr.op == EXPR_FIELD_REF) {
-            log_err_with_code(info->ctx,
+            log_err(info->ctx,
                     XKB_ERROR_WRONG_SCOPE,
                     "Cannot set a global default value from within a key statement; "
                     "Move statements to the global file scope\n");
@@ -1132,7 +1140,7 @@ SetExplicitGroup(SymbolsInfo *info, KeyInfo *keyi)
     }
 
     if (warn) {
-        log_warn_with_code(info->ctx,
+        log_warn(info->ctx,
                  XKB_WARNING_MULTIPLE_GROUPS_AT_ONCE,
                  "For the map %s an explicit group specified, "
                  "but key %s has more than one group defined; "
@@ -1198,7 +1206,7 @@ HandleModMapDef(SymbolsInfo *info, ModMapDef *def)
         // Handle normal entry
         ndx = XkbModNameToIndex(&info->mods, def->modifier, MOD_REAL);
         if (ndx == XKB_MOD_INVALID) {
-            log_err_with_code(info->ctx,
+            log_err(info->ctx,
                     XKB_ERROR_INVALID_REAL_MODIFIER,
                     "Illegal modifier map definition; "
                     "Ignoring map for non-modifier \"%s\"\n",
@@ -1224,7 +1232,7 @@ HandleModMapDef(SymbolsInfo *info, ModMapDef *def)
             tmp.u.keySym = sym;
         }
         else {
-            log_err_with_code(info->ctx,
+            log_err(info->ctx,
                     XKB_ERROR_INVALID_MODMAP_ENTRY,
                     "Modmap entries may contain only key names or keysyms; "
                     "Illegal definition for %s modifier ignored\n",
@@ -1263,7 +1271,7 @@ HandleSymbolsFile(SymbolsInfo *info, XkbFile *file, enum merge_mode merge)
             ok = HandleModMapDef(info, (ModMapDef *) stmt);
             break;
         default:
-            log_err_with_code(info->ctx,
+            log_err(info->ctx,
                     XKB_ERROR_WRONG_STATEMENT_TYPE,
                     "Symbols files may not include other types; "
                     "Ignoring %s\n", stmt_type_to_string(stmt->type));
@@ -1275,7 +1283,7 @@ HandleSymbolsFile(SymbolsInfo *info, XkbFile *file, enum merge_mode merge)
             info->errorCount++;
 
         if (info->errorCount > 10) {
-            log_err_with_code(info->ctx,
+            log_err(info->ctx,
                     XKB_ERROR_INVALID_SYNTAX,
                     "Abandoning symbols file \"%s\"\n",
                     file->name);
@@ -1411,7 +1419,7 @@ FindTypeForGroup(struct xkb_keymap *keymap, KeyInfo *keyi,
     }
 
     if (type_name == XKB_ATOM_NONE) {
-        log_warn_with_code(keymap->ctx,
+        log_warn(keymap->ctx,
                  XKB_WARNING_CANNOT_INFER_KEY_TYPE,
                  "Couldn't find an automatic type for key '%s' group %d with %lu levels; "
                  "Using the default type\n",
@@ -1425,7 +1433,7 @@ FindTypeForGroup(struct xkb_keymap *keymap, KeyInfo *keyi,
             break;
 
     if (i >= keymap->num_types) {
-        log_warn_with_code(keymap->ctx,
+        log_warn(keymap->ctx,
                  XKB_WARNING_UNDEFINED_KEY_TYPE,
                  "The type \"%s\" for key '%s' group %d was not previously defined; "
                  "Using the default type\n",
@@ -1491,6 +1499,10 @@ CopySymbolsDefToKeymap(struct xkb_keymap *keymap, SymbolsInfo *info,
     }
 
     key->groups = calloc(key->num_groups, sizeof(*key->groups));
+    if (!key->groups) {
+        log_err(info->ctx, "Failed to allocate memory for key->groups\n");
+        return false;
+    }
 
     /* Find and assign the groups' types in the keymap. */
     darray_enumerate(i, groupi, keyi->groups) {
@@ -1613,7 +1625,7 @@ CopySymbolsToKeymap(struct xkb_keymap *keymap, SymbolsInfo *info)
                 continue;
 
             if (key->num_groups < 1)
-                log_info(info->ctx,
+                log_info(info->ctx, XKB_LOG_MESSAGE_NO_ID,
                          "No symbols defined for %s\n",
                          KeyNameText(info->ctx, key->name));
         }