Skip to content

basic_string::_M_construct null not valid on test #3821

@Proximyst

Description

@Proximyst

Hi, I've written a precise 62 LOC of Zig in my lifetime and thus this is probably just me being a doof (in which case, this is an extremely unhelpful error).

I've written this code:

const std = @import("std");

fn parseDecimal(comptime T: type, text: []const u8) !T {
    var x: T = 0;

    for (text) |char| {
        const digit = try charToDigit(char);
        if (@mulWithOverflow(T, x, 10, &x)) {
            return error.Overflow;
        }
        if (@addWithOverflow(T, x, digit, &x)) {
            return error.Overflow;
        }
    }

    return x;
}

fn charToDigit(char: u8) !u4 {
    return switch (char) {
        '0'...'9' => @intCast(u4, char - '0'),
        else => error.InvalidChar,
    };
}

test "parse numbers" {
    const lol = parseDecimal(u64, "6969696969") catch unreachable;
    std.debug.assert(lol == 6969696969);
    if (parseDecimal(i192, "test")) |n| {
        std.debug.panic("somehow got {}, expected error.Overflow", n);
    } else |e| switch (e) {
        error.Overflow => {},
        else => unreachable,
    }
}

Upon running it with zig test I get:

0 ~/W/a/a/z/day1 zig test src/main.zig
terminate called after throwing an instance of 'std::logic_error'
  what():  basic_string::_M_construct null not valid
fish: “zig test src/main.zig” terminated by signal SIGABRT (Abort)
134 # the numbers before paths are exit codes of last cmd

Metadata

Metadata

Assignees

No one assigned

    Labels

    backend-llvmThe LLVM backend outputs an LLVM IR Module.bugObserved behavior contradicts documented or intended behaviorcontributor friendlyThis issue is limited in scope and/or knowledge of Zig internals.frontendTokenization, parsing, AstGen, Sema, and Liveness.upstreamAn issue with a third party project that Zig uses.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions