Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check for invalid digits when parsing octal constants #153

Merged
merged 2 commits into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -634,7 +634,10 @@ void read_numeric_param(block_t *parent, basic_block_t *bb, int is_neg)
} while (is_hex(token[i]));
} else { /* octal */
do {
c = token[i++] - '0';
c = token[i++];
if (c > '7')
error("Invalid numeric constant");
c -= '0';
value = (value * 8) + c;
} while (is_digit(token[i]));
}
Expand Down
33 changes: 33 additions & 0 deletions tests/driver.sh
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,31 @@ function try_output() {
try "$expected" "$expected_output" "$input"
}

# try_compile_error - test shecc with invalid C program
# Usage:
# - try_compile_error invalid_input_code
# compile "invalid_input_code" with shecc so that shecc generates a
# compilation error message.
#
# This function uses shecc to compile invalid code and obtains the exit
# code returned by shecc. The exit code must be a non-zero value to
# indicate that shecc has the ability to parse the invalid code and
# output an error message.
function try_compile_error() {
jserv marked this conversation as resolved.
Show resolved Hide resolved
local input=$(cat)

local tmp_in="$(mktemp --suffix .c)"
local tmp_exe="$(mktemp)"
echo "$input" > "$tmp_in"
"$SHECC" -o "$tmp_exe" "$tmp_in"
local exit_code=$?

if [ 0 == $exit_code ]; then
echo "Error: compilation is passed."
exit 1
fi
}

function items() {
local expected="$1"
local input="$2"
Expand Down Expand Up @@ -238,6 +263,14 @@ int main() {
}
EOF

try_compile_error << EOF
int main() {
int a = 03, b = 01118, c = 091;
printf("%d %d %d\n", a, b, c);
return 0;
}
EOF

try_ 1 << EOF
int is_odd(int x);

Expand Down