zig

fork of https://codeberg.org/ziglang/zig
Log | Files | Refs | README | LICENSE

commit 39f43fea8d0f6aa1c69cb7c3209f57f5ce00b273 (tree)
parent be2bd5848a880765f4bc7e2363ef201a0930a04b
Author: r00ster91 <r00ster91@proton.me>
Date:   Fri, 19 Aug 2022 12:03:14 +0200

fix: fix off-by-one for leading zeroes

Diffstat:
Msrc/translate_c.zig | 2+-
Mtest/translate_c.zig | 12++++++++++++
2 files changed, 13 insertions(+), 1 deletion(-)

diff --git a/src/translate_c.zig b/src/translate_c.zig @@ -5647,7 +5647,7 @@ fn parseCNumLit(c: *Context, m: *MacroCtx) ParseError!Node { switch (m.list[m.i].id) { .IntegerLiteral => |suffix| { var radix: []const u8 = "decimal"; - if (lit_bytes.len > 2 and lit_bytes[0] == '0') { + if (lit_bytes.len >= 2 and lit_bytes[0] == '0') { switch (lit_bytes[1]) { '0'...'7' => { // Octal diff --git a/test/translate_c.zig b/test/translate_c.zig @@ -3842,4 +3842,16 @@ pub fn addCases(cases: *tests.TranslateCContext) void { , &[_][]const u8{ \\pub const FOO = ""; }); + + cases.add("leading zeroes", + \\#define O_RDONLY 00 + \\#define HELLO 000 + \\#define ZERO 0 + \\#define WORLD 00000123 + , &[_][]const u8{ + \\pub const O_RDONLY = @as(c_int, 0o0); + \\pub const HELLO = @as(c_int, 0o00); + \\pub const ZERO = @as(c_int, 0); + \\pub const WORLD = @as(c_int, 0o0000123); + }); }