Skip to content

Commit 5fe150c

Browse files
wooster0kristoff-it
authored andcommitted
fix: fix off-by-one for leading zeroes
1 parent 102c28a commit 5fe150c

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

src/translate_c.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5647,7 +5647,7 @@ fn parseCNumLit(c: *Context, m: *MacroCtx) ParseError!Node {
56475647
switch (m.list[m.i].id) {
56485648
.IntegerLiteral => |suffix| {
56495649
var radix: []const u8 = "decimal";
5650-
if (lit_bytes.len > 2 and lit_bytes[0] == '0') {
5650+
if (lit_bytes.len >= 2 and lit_bytes[0] == '0') {
56515651
switch (lit_bytes[1]) {
56525652
'0'...'7' => {
56535653
// Octal

test/translate_c.zig

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3842,4 +3842,16 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
38423842
, &[_][]const u8{
38433843
\\pub const FOO = "";
38443844
});
3845+
3846+
cases.add("leading zeroes",
3847+
\\#define O_RDONLY 00
3848+
\\#define HELLO 000
3849+
\\#define ZERO 0
3850+
\\#define WORLD 00000123
3851+
, &[_][]const u8{
3852+
\\pub const O_RDONLY = @as(c_int, 0o0);
3853+
\\pub const HELLO = @as(c_int, 0o00);
3854+
\\pub const ZERO = @as(c_int, 0);
3855+
\\pub const WORLD = @as(c_int, 0o0000123);
3856+
});
38453857
}

0 commit comments

Comments
 (0)