Skip to content

Commit 3573b9f

Browse files
authored
Merge pull request #153 from DrXiao/fix-parsing-octal
Check for invalid digits when parsing octal constants
2 parents 2c852d0 + 823dde3 commit 3573b9f

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

src/parser.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -634,7 +634,10 @@ void read_numeric_param(block_t *parent, basic_block_t *bb, int is_neg)
634634
} while (is_hex(token[i]));
635635
} else { /* octal */
636636
do {
637-
c = token[i++] - '0';
637+
c = token[i++];
638+
if (c > '7')
639+
error("Invalid numeric constant");
640+
c -= '0';
638641
value = (value * 8) + c;
639642
} while (is_digit(token[i]));
640643
}

tests/driver.sh

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,31 @@ function try_output() {
6262
try "$expected" "$expected_output" "$input"
6363
}
6464

65+
# try_compile_error - test shecc with invalid C program
66+
# Usage:
67+
# - try_compile_error invalid_input_code
68+
# compile "invalid_input_code" with shecc so that shecc generates a
69+
# compilation error message.
70+
#
71+
# This function uses shecc to compile invalid code and obtains the exit
72+
# code returned by shecc. The exit code must be a non-zero value to
73+
# indicate that shecc has the ability to parse the invalid code and
74+
# output an error message.
75+
function try_compile_error() {
76+
local input=$(cat)
77+
78+
local tmp_in="$(mktemp --suffix .c)"
79+
local tmp_exe="$(mktemp)"
80+
echo "$input" > "$tmp_in"
81+
"$SHECC" -o "$tmp_exe" "$tmp_in"
82+
local exit_code=$?
83+
84+
if [ 0 == $exit_code ]; then
85+
echo "Error: compilation is passed."
86+
exit 1
87+
fi
88+
}
89+
6590
function items() {
6691
local expected="$1"
6792
local input="$2"
@@ -238,6 +263,14 @@ int main() {
238263
}
239264
EOF
240265

266+
try_compile_error << EOF
267+
int main() {
268+
int a = 03, b = 01118, c = 091;
269+
printf("%d %d %d\n", a, b, c);
270+
return 0;
271+
}
272+
EOF
273+
241274
try_ 1 << EOF
242275
int is_odd(int x);
243276

0 commit comments

Comments
 (0)