Limit multipass variables to values from 0 to 255 A rule could set a variable to any number and increment it without bound, so in a UCS4 build `#1=2147483647#1+` overflowed a signed int. The compiler now rejects a value above 255, and an increment stops at 255, the way a decrement already stops at 0. No table uses a value above 6, and louis-rs has the same limit. Fixes #2219
diff --git a/doc/liblouis.texi b/doc/liblouis.texi index ea53f5f..2410e3d 100644 --- a/doc/liblouis.texi +++ b/doc/liblouis.texi
@@ -1998,11 +1998,13 @@ Variables are set in the action part. To set a variable, use an expression like @code{#1=1}. All of the variables are initialized to 0 -at the start of each pass. +at the start of each pass. A variable holds a value from 0 to 255; a +larger value in a rule is a compile error. Variables can also be incremented and decremented by one in the action part with expressions like @code{#1+} and @code{#3-} respectively. -An attempt to decrement a variable below 0 is silently ignored. +An attempt to decrement a variable below 0 or increment it above 255 +is silently ignored. Variables are tested in the test part with conditional expressions like: @code{#1=2}, @code{#3<4}, @code{#5>6}, @code{#7<=8}, @code{#9>=10}.
diff --git a/liblouis/commonTranslationFunctions.c b/liblouis/commonTranslationFunctions.c index 748317a..4f1f531 100644 --- a/liblouis/commonTranslationFunctions.c +++ b/liblouis/commonTranslationFunctions.c
@@ -85,7 +85,8 @@ return 1; case pass_plus: - passVariables[instructions[*IC + 1]] += 1; + if (passVariables[instructions[*IC + 1]] < MAXVARVALUE) + passVariables[instructions[*IC + 1]] += 1; *IC += 2; return 1;
diff --git a/liblouis/compileTranslationTable.c b/liblouis/compileTranslationTable.c index 0e62894..d482512 100644 --- a/liblouis/compileTranslationTable.c +++ b/liblouis/compileTranslationTable.c
@@ -1818,6 +1818,23 @@ } static int +passGetVariableValue( + const FileInfo *file, CharsString *passLine, int *passLinepos, widechar *number) { + /* Read the value a variable is compared with or set to */ + int value = 0; + while ((*passLinepos < passLine->length) && (passLine->chars[*passLinepos] >= '0') && + (passLine->chars[*passLinepos] <= '9')) { + value = 10 * value + (passLine->chars[(*passLinepos)++] - '0'); + if (value > MAXVARVALUE) { + compileError(file, "variable value out of range"); + return 0; + } + } + *number = value; + return 1; +} + +static int passGetVariableNumber( const FileInfo *file, CharsString *passLine, int *passLinepos, widechar *number) { if (!passGetNumber(passLine, passLinepos, number)) { @@ -2051,7 +2068,8 @@ file, passInstructions, &passIC, passHoldNumber)) return 0; passLinepos++; - passGetNumber(&passLine, &passLinepos, &passHoldNumber); + if (!passGetVariableValue(file, &passLine, &passLinepos, &passHoldNumber)) + return 0; if (!appendInstructionChar( file, passInstructions, &passIC, passHoldNumber)) return 0; @@ -2242,7 +2260,8 @@ file, passInstructions, &passIC, passHoldNumber)) return 0; passLinepos++; - passGetNumber(&passLine, &passLinepos, &passHoldNumber); + if (!passGetVariableValue(file, &passLine, &passLinepos, &passHoldNumber)) + return 0; if (!appendInstructionChar( file, passInstructions, &passIC, passHoldNumber)) return 0;
diff --git a/liblouis/internal.h b/liblouis/internal.h index 0d84a86..3130cf2 100644 --- a/liblouis/internal.h +++ b/liblouis/internal.h
@@ -56,6 +56,7 @@ #endif #define NUMVAR 50 +#define MAXVARVALUE 255 #define EMPHMODECHARSSIZE 256 #define NOEMPHCHARSSIZE 256 #define LETSIGNSIZE 256
diff --git a/tests/checkTable.c b/tests/checkTable.c index 914fb4d..64d1c97 100644 --- a/tests/checkTable.c +++ b/tests/checkTable.c
@@ -20,6 +20,7 @@ { const char *goodTable = "tables/en-us-g1.ctb"; const char *badTable = "tests/tables/bad.ctb"; + const char *badPassVariable = "tests/tables/bad-pass-variable.ctb"; int result = 0; if (lou_checkTable(goodTable) == 0) @@ -34,6 +35,13 @@ result = 1; } + /* A variable holds a value from 0 to 255 (issue #2219). */ + if (lou_checkTable(badPassVariable) != 0) + { + printf("Getting %s succeeded, expected failure\n", badPassVariable); + result = 1; + } + if (lou_checkTable(goodTable) == 0) { printf("Getting %s failed, expected success\n", goodTable);
diff --git a/tests/tables/Makefile.am b/tests/tables/Makefile.am index eeb5906..9f0987f 100644 --- a/tests/tables/Makefile.am +++ b/tests/tables/Makefile.am
@@ -1,6 +1,7 @@ SUBDIRS = moreTables resolve_table emphclass attribute EXTRA_DIST = \ + bad-pass-variable.ctb \ bad-replace-source.ctb \ bad-replace.ctb \ bad.ctb \
diff --git a/tests/tables/bad-pass-variable.ctb b/tests/tables/bad-pass-variable.ctb new file mode 100644 index 0000000..6b61d92 --- /dev/null +++ b/tests/tables/bad-pass-variable.ctb
@@ -0,0 +1 @@ +noback context "x" #1=256@1
diff --git a/tests/yaml/multipass-forward.yaml b/tests/yaml/multipass-forward.yaml index 30a36f3..0c84f72 100644 --- a/tests/yaml/multipass-forward.yaml +++ b/tests/yaml/multipass-forward.yaml
@@ -162,6 +162,19 @@ display: tables/unicode.dis table: | include tables/en-us-comp8.ctb + noback correct "=" #1=255? + noback correct "+" #1+? + noback correct #1=255"m" * + noback correct $a ? +flags: {testmode: forward} +tests: +- # a variable stops at 255 + - =+m + - ⠍ + +display: tables/unicode.dis +table: | + include tables/en-us-comp8.ctb noback correct #1=0"0" #1=1"1" noback context #1=0"1" #1=2@23 noback pass2 #1=0@23 #1=3@25