Skip to content

[b2] std::length_error crash when empty string is passed to var_defines on LoongArch #786

Open

Description

@setarcos

Environment:

  • Boost: 1.91.0
  • Arch: LoongArch (loongarch64)
  • Compiler: GCC 15.2.1

Problem:
b2 crashes with std::length_error on basic commands like ./b2 --show-libraries.

Backtrace:

boostorg/boost#11 var_defines (variable.cpp:84)
boostorg/boost#12 guarded_main (jam.cpp:527)
boostorg/boost#13 main (jam.cpp:694)

Root cause:
In variable.cpp:84, when an empty string is passed to var_defines(), string_view::find('=') returns npos, causing var to be constructed with npos as length. Then std::string{var.begin(), var.end()} tries to allocate 18446744073709551615 bytes -> length_error.

Activity

  1. iv-m commented on Jul 1, 2026

    @iv-m
    Contributor

    I've just stumbled upon this on my loongarch64 machine. Trivial fix like this seems to help:

    --- a/boost/tools/build/src/engine/variable.cpp
    +++ b/boost/tools/build/src/engine/variable.cpp
    @@ -78,6 +78,7 @@ void var_defines(struct module_t * module, const char * const * e, int preproces
            for (; *e; ++e)
            {
                    ::b2::string_view def(*e);
    +               if (def.empty()) continue;
                    ::b2::string_view var(def.begin(), def.find('='));
                    ::b2::string_view val(def.begin() + var.size() + 1);
                    b2::jam::variable jam_var { module,

    But I'm not sure if this is the right thing to do, or why this empty string is passed here in the first place.

  2. iv-m commented on Jul 1, 2026

    @iv-m
    Contributor

    BTW this issue probably should be moved to https://github.com/boostorg/build/issues.

  3. iv-m commented on Aug 14, 2026

    @iv-m
    Contributor

    This might be fixed in 708353c

  4. pdimov commented on Oct 28, 2026

    @pdimov
    Member

    I can move this issue to boostorg/build, but the upstream repository of B2 is https://github.com/bfgroup/b2, and I can't move it there. :-)

  5. transferred this issue fromboostorg/booston Oct 28, 2026
  6. grafikrobot commented on Oct 28, 2026

    @grafikrobot
    Member

    3. This might be fixed in 708353c

    Yes, certain that is the fix. And you can try it out by using a more recent b2 (which you can do with any Boost version).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions