Skip to content

Commit f6d19cc

Browse files
committed
Fix hexadecimal unescaping & test case
1 parent ecfcf57 commit f6d19cc

File tree

5 files changed

+22
-17
lines changed

5 files changed

+22
-17
lines changed

src/defs.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
/* definitions */
1212

13+
#define DEBUG_BUILD false
14+
1315
/* Common macro functions */
1416
#define is_whitespace(c) (c == ' ' || c == '\t')
1517
#define is_newline(c) (c == '\r' || c == '\n')
@@ -22,7 +24,6 @@
2224
/* Limitations */
2325
#define MAX_TOKEN_LEN 256
2426
#define MAX_ID_LEN 64
25-
#define MAX_ESCAPED_CHAR_LEN 5
2627
#define MAX_LINE_LEN 256
2728
#define MAX_VAR_LEN 32
2829
#define MAX_TYPE_LEN 32

src/globals.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -703,7 +703,7 @@ int unescape_string(const char *input, char *output, int output_size)
703703
i++;
704704
break;
705705
case 'e':
706-
output[j++] = 23;
706+
output[j++] = 27;
707707
i++;
708708
break;
709709
case 'n':
@@ -746,10 +746,12 @@ int unescape_string(const char *input, char *output, int output_size)
746746
return -1;
747747

748748
int value = 0;
749+
int count = 0;
749750

750-
while (is_hex(input[i])) {
751-
value = value * 16 + hex_digit_value(input[i]);
751+
while (is_hex(input[i]) && count < 2) {
752+
value = (value << 4) + hex_digit_value(input[i]);
752753
i++;
754+
count++;
753755
}
754756

755757
output[j++] = (char) value;
@@ -1363,6 +1365,7 @@ void global_release(void)
13631365
hashmap_free(CONSTANTS_MAP);
13641366
}
13651367

1368+
#if DEBUG_BUILD
13661369
void dbg_token(token_t *token)
13671370
{
13681371
char *name;
@@ -1637,6 +1640,7 @@ void dbg_token(token_t *token)
16371640
token->location.column);
16381641
}
16391642
}
1643+
#endif
16401644

16411645
/* Reports an error without specifying a position */
16421646
void fatal(char *msg)

src/parser.c

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1124,26 +1124,24 @@ void read_parameter_list_decl(func_t *func, bool anon)
11241124

11251125
void read_literal_param(block_t *parent, basic_block_t *bb)
11261126
{
1127-
char literal[MAX_TOKEN_LEN];
1128-
char unescaped[MAX_TOKEN_LEN];
1129-
char combined[MAX_TOKEN_LEN];
1127+
char literal[MAX_TOKEN_LEN], unescaped[MAX_TOKEN_LEN],
1128+
combined[MAX_LINE_LEN];
11301129
int combined_len = 0;
11311130

11321131
/* Read first string literal */
11331132
lex_ident(T_string, literal);
1134-
unescape_string(literal, unescaped, MAX_TOKEN_LEN);
1135-
strcpy(combined, unescaped);
1136-
combined_len = strlen(unescaped);
1133+
unescape_string(literal, combined, MAX_LINE_LEN);
1134+
combined_len = strlen(combined);
11371135

11381136
/* Check for adjacent string literals and concatenate them */
11391137
while (lex_peek(T_string, NULL)) {
11401138
lex_ident(T_string, literal);
1141-
unescape_string(literal, unescaped, MAX_TOKEN_LEN);
1139+
unescape_string(literal, unescaped, MAX_LINE_LEN - combined_len);
11421140
int unescaped_len = strlen(unescaped);
1143-
if (combined_len + unescaped_len >= MAX_TOKEN_LEN - 1)
1141+
if (combined_len + unescaped_len >= MAX_LINE_LEN - 1)
11441142
error("Concatenated string literal too long");
11451143

1146-
strcpy(combined + combined_len, literal);
1144+
strcpy(combined + combined_len, unescaped);
11471145
combined_len += unescaped_len;
11481146
}
11491147

src/riscv-codegen.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,14 +99,15 @@ void update_elf_offset(ph2_ir_t *ph2_ir)
9999
else
100100
elf_offset += 4;
101101
return;
102-
case OP_sign_ext:
102+
case OP_sign_ext: {
103103
/* Decode source size from upper 16 bits */
104104
int source_size = (ph2_ir->src1 >> 16) & 0xFFFF;
105105
if (source_size == 2)
106106
elf_offset += 8; /* short extension: 2 instructions */
107107
else
108108
elf_offset += 12; /* byte extension: 3 instructions */
109109
return;
110+
}
110111
case OP_cast:
111112
elf_offset += 4;
112113
return;
@@ -443,7 +444,7 @@ void emit_ph2_ir(ph2_ir_t *ph2_ir)
443444
fatal("Unsupported truncation operation with invalid target size");
444445
}
445446
return;
446-
case OP_sign_ext:
447+
case OP_sign_ext: {
447448
/* Decode size information:
448449
* Lower 16 bits: target size
449450
* Upper 16 bits: source size
@@ -468,6 +469,7 @@ void emit_ph2_ir(ph2_ir_t *ph2_ir)
468469
emit(__srai(rd, rd, shift_amount));
469470
}
470471
return;
472+
}
471473
case OP_cast:
472474
/* Generic cast operation - for now, just move the value */
473475
emit(__addi(rd, rs1, 0));

tests/driver.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5342,14 +5342,14 @@ EOF
53425342
# String literal and escape coverage (additional)
53435343
try_output 0 "AZ" << 'EOF'
53445344
int main() {
5345-
printf("%s", "\\x41Z"); /* hex escape then normal char */
5345+
printf("%s", "\x41Z"); /* hex escape then normal char */
53465346
return 0;
53475347
}
53485348
EOF
53495349

53505350
try_output 0 "AZ" << 'EOF'
53515351
int main() {
5352-
printf("%s", "A\\132"); /* octal escape for 'Z' */
5352+
printf("%s", "A\132"); /* octal escape for 'Z' */
53535353
return 0;
53545354
}
53555355
EOF

0 commit comments

Comments
 (0)