Fix overflow issue
[platform/upstream/libxkbcommon.git] / src / xkbcomp / compat.c
index ff83645..44a444c 100644 (file)
@@ -47,6 +47,8 @@
  * DEALINGS IN THE SOFTWARE.
  */
 
+#include "config.h"
+
 #include "xkbcomp-priv.h"
 #include "text.h"
 #include "expr.h"
@@ -122,7 +124,8 @@ static inline bool
 ReportSIBadType(CompatInfo *info, SymInterpInfo *si, const char *field,
                 const char *wanted)
 {
-    return ReportBadType(info->ctx, "symbol interpretation", field,
+    return ReportBadType(info->ctx, XKB_ERROR_WRONG_FIELD_TYPE,
+                         "symbol interpretation", field,
                          siText(si, info), wanted);
 }
 
@@ -130,7 +133,8 @@ static inline bool
 ReportLedBadType(CompatInfo *info, LedInfo *ledi, const char *field,
                  const char *wanted)
 {
-    return ReportBadType(info->ctx, "indicator map", field,
+    return ReportBadType(info->ctx, XKB_ERROR_WRONG_FIELD_TYPE,
+                         "indicator map", field,
                          xkb_atom_text(info->ctx, ledi->led.name),
                          wanted);
 }
@@ -205,7 +209,7 @@ AddInterp(CompatInfo *info, SymInterpInfo *new, bool same_file)
 
         if (new->merge == MERGE_REPLACE) {
             if (report)
-                log_warn(info->ctx,
+                log_warn(info->ctx, XKB_LOG_MESSAGE_NO_ID,
                          "Multiple definitions for \"%s\"; "
                          "Earlier interpretation ignored\n",
                          siText(new, info));
@@ -235,7 +239,7 @@ AddInterp(CompatInfo *info, SymInterpInfo *new, bool same_file)
         }
 
         if (collide) {
-            log_warn(info->ctx,
+            log_warn(info->ctx, XKB_LOG_MESSAGE_NO_ID,
                      "Multiple interpretations of \"%s\"; "
                      "Using %s definition for duplicate fields\n",
                      siText(new, info),
@@ -264,8 +268,9 @@ ResolveStateAndPredicate(ExprDef *expr, enum xkb_match_operation *pred_rtrn,
     *pred_rtrn = MATCH_EXACTLY;
     if (expr->expr.op == EXPR_ACTION_DECL) {
         const char *pred_txt = xkb_atom_text(info->ctx, expr->action.name);
-        if (!LookupString(symInterpretMatchMaskNames, pred_txt, pred_rtrn)) {
-            log_err(info->ctx,
+        if (!LookupString(symInterpretMatchMaskNames, pred_txt, pred_rtrn) ||
+            !expr->action.args || expr->action.args->common.next) {
+            log_err(info->ctx, XKB_LOG_MESSAGE_NO_ID,
                     "Illegal modifier predicate \"%s\"; Ignored\n", pred_txt);
             return false;
         }
@@ -328,7 +333,7 @@ AddLedMap(CompatInfo *info, LedInfo *new, bool same_file)
 
         if (new->merge == MERGE_REPLACE) {
             if (report)
-                log_warn(info->ctx,
+                log_warn(info->ctx, XKB_LOG_MESSAGE_NO_ID,
                          "Map for indicator %s redefined; "
                          "Earlier definition ignored\n",
                          xkb_atom_text(info->ctx, old->led.name));
@@ -353,7 +358,7 @@ AddLedMap(CompatInfo *info, LedInfo *new, bool same_file)
         }
 
         if (collide) {
-            log_warn(info->ctx,
+            log_warn(info->ctx, XKB_LOG_MESSAGE_NO_ID,
                      "Map for indicator %s redefined; "
                      "Using %s definition for duplicate fields\n",
                      xkb_atom_text(info->ctx, old->led.name),
@@ -364,7 +369,7 @@ AddLedMap(CompatInfo *info, LedInfo *new, bool same_file)
     }
 
     if (info->num_leds >= XKB_MAX_LEDS) {
-        log_err(info->ctx,
+        log_err(info->ctx, XKB_LOG_MESSAGE_NO_ID,
                 "Too many LEDs defined (maximum %d)\n",
                 XKB_MAX_LEDS);
         return false;
@@ -377,8 +382,6 @@ static void
 MergeIncludedCompatMaps(CompatInfo *into, CompatInfo *from,
                         enum merge_mode merge)
 {
-    SymInterpInfo *si;
-
     if (from->errorCount > 0) {
         into->errorCount += from->errorCount;
         return;
@@ -396,6 +399,7 @@ MergeIncludedCompatMaps(CompatInfo *into, CompatInfo *from,
         darray_init(from->interps);
     }
     else {
+        SymInterpInfo *si;
         darray_foreach(si, from->interps) {
             si->merge = (merge == MERGE_DEFAULT ? si->merge : merge);
             if (!AddInterp(into, si, false))
@@ -426,6 +430,9 @@ HandleIncludeCompatMap(CompatInfo *info, IncludeStmt *include)
 {
     CompatInfo included;
 
+    if (!include)
+        return false;
+
     InitCompatInfo(&included, info->ctx, info->actions, &info->mods);
     included.name = include->stmt;
     include->stmt = NULL;
@@ -502,7 +509,7 @@ SetInterpField(CompatInfo *info, SymInterpInfo *si, const char *field,
         si->defined |= SI_FIELD_AUTO_REPEAT;
     }
     else if (istreq(field, "locking")) {
-        log_dbg(info->ctx,
+        log_dbg(info->ctx, XKB_LOG_MESSAGE_NO_ID,
                 "The \"locking\" field in symbol interpretation is unsupported; "
                 "Ignored\n");
     }
@@ -568,7 +575,7 @@ SetLedMapField(CompatInfo *info, LedInfo *ledi, const char *field,
         ledi->defined |= LED_FIELD_CTRLS;
     }
     else if (istreq(field, "allowexplicit")) {
-        log_dbg(info->ctx,
+        log_dbg(info->ctx, XKB_LOG_MESSAGE_NO_ID,
                 "The \"allowExplicit\" field in indicator statements is unsupported; "
                 "Ignored\n");
     }
@@ -605,18 +612,18 @@ SetLedMapField(CompatInfo *info, LedInfo *ledi, const char *field,
              istreq(field, "leddriveskeyboard") ||
              istreq(field, "indicatordriveskbd") ||
              istreq(field, "indicatordriveskeyboard")) {
-        log_dbg(info->ctx,
+        log_dbg(info->ctx, XKB_LOG_MESSAGE_NO_ID,
                 "The \"%s\" field in indicator statements is unsupported; "
                 "Ignored\n", field);
     }
     else if (istreq(field, "index")) {
         /* Users should see this, it might cause unexpected behavior. */
-        log_err(info->ctx,
+        log_err(info->ctx, XKB_LOG_MESSAGE_NO_ID,
                 "The \"index\" field in indicator statements is unsupported; "
                 "Ignored\n");
     }
     else {
-        log_err(info->ctx,
+        log_err(info->ctx, XKB_LOG_MESSAGE_NO_ID,
                 "Unknown field %s in map for %s indicator; "
                 "Definition ignored\n",
                 field, xkb_atom_text(info->ctx, ledi->led.name));
@@ -656,7 +663,7 @@ HandleInterpBody(CompatInfo *info, VarDef *def, SymInterpInfo *si)
 
     for (; def; def = (VarDef *) def->common.next) {
         if (def->name && def->name->expr.op == EXPR_FIELD_REF) {
-            log_err(info->ctx,
+            log_err(info->ctx, XKB_LOG_MESSAGE_NO_ID,
                     "Cannot set a global default value from within an interpret statement; "
                     "Move statements to the global file scope\n");
             ok = false;
@@ -681,14 +688,14 @@ HandleInterpDef(CompatInfo *info, InterpDef *def, enum merge_mode merge)
     SymInterpInfo si;
 
     if (!ResolveStateAndPredicate(def->match, &pred, &mods, info)) {
-        log_err(info->ctx,
+        log_err(info->ctx, XKB_LOG_MESSAGE_NO_ID,
                 "Couldn't determine matching modifiers; "
                 "Symbol interpretation ignored\n");
         return false;
     }
 
     si = info->default_interp;
-    si.merge = merge = (def->merge == MERGE_DEFAULT ? merge : def->merge);
+    si.merge = (def->merge == MERGE_DEFAULT ? merge : def->merge);
     si.interp.sym = def->sym;
     si.interp.match = pred;
     si.interp.mods = mods;
@@ -730,7 +737,7 @@ HandleLedMapDef(CompatInfo *info, LedMapDef *def, enum merge_mode merge)
         }
 
         if (elem) {
-            log_err(info->ctx,
+            log_err(info->ctx, XKB_LOG_MESSAGE_NO_ID,
                     "Cannot set defaults for \"%s\" element in indicator map; "
                     "Assignment to %s.%s ignored\n", elem, elem, field);
             ok = false;
@@ -765,7 +772,7 @@ HandleCompatMapFile(CompatInfo *info, XkbFile *file, enum merge_mode merge)
             ok = HandleInterpDef(info, (InterpDef *) stmt, merge);
             break;
         case STMT_GROUP_COMPAT:
-            log_dbg(info->ctx,
+            log_dbg(info->ctx, XKB_LOG_MESSAGE_NO_ID,
                     "The \"group\" statement in compat is unsupported; "
                     "Ignored\n");
             ok = true;
@@ -780,7 +787,7 @@ HandleCompatMapFile(CompatInfo *info, XkbFile *file, enum merge_mode merge)
             ok = HandleVModDef(info->ctx, &info->mods, (VModDef *) stmt, merge);
             break;
         default:
-            log_err(info->ctx,
+            log_err(info->ctx, XKB_LOG_MESSAGE_NO_ID,
                     "Compat files may not include other types; "
                     "Ignoring %s\n", stmt_type_to_string(stmt->type));
             ok = false;
@@ -791,8 +798,8 @@ HandleCompatMapFile(CompatInfo *info, XkbFile *file, enum merge_mode merge)
             info->errorCount++;
 
         if (info->errorCount > 10) {
-            log_err(info->ctx,
-                    "Abandoning compatibility map \"%s\"\n", file->topName);
+            log_err(info->ctx, XKB_LOG_MESSAGE_NO_ID,
+                    "Abandoning compatibility map \"%s\"\n", file->name);
             break;
         }
     }
@@ -833,7 +840,7 @@ CopyLedMapDefsToKeymap(struct xkb_keymap *keymap, CompatInfo *info)
 
         /* Not previously declared; create it with next free index. */
         if (i >= keymap->num_leds) {
-            log_dbg(keymap->ctx,
+            log_dbg(keymap->ctx, XKB_LOG_MESSAGE_NO_ID,
                     "Indicator name \"%s\" was not declared in the keycodes section; "
                     "Adding new indicator\n",
                     xkb_atom_text(keymap->ctx, ledi->led.name));
@@ -845,7 +852,7 @@ CopyLedMapDefsToKeymap(struct xkb_keymap *keymap, CompatInfo *info)
             if (i >= keymap->num_leds) {
                 /* Not place to put it; ignore. */
                 if (i >= XKB_MAX_LEDS) {
-                    log_err(keymap->ctx,
+                    log_err(keymap->ctx, XKB_LOG_MESSAGE_NO_ID,
                             "Too many indicators (maximum is %d); "
                             "Indicator name \"%s\" ignored\n",
                             XKB_MAX_LEDS,