Skip to content

NodeJS 22 throws std::bad_alloc when calling std::regex #57639

Open
@Retribution98

Description

@Retribution98

Version

v22.14.0

Platform

Run on `Ubuntu 22.04.4 LTS`
Build on `CentOS Linux 7 (Core)`

Subsystem

No response

What steps will reproduce the bug?

  1. Create addon.cpp using regex.
#include <napi.h>
#include <regex>

const std::regex UNIVERSAL_PATTERN{R"(\d+)"};

Napi::Object Init(Napi::Env env, Napi::Object exports) {
    return exports;
}

NODE_API_MODULE(test, Init)
  1. Create CMakeLists.txt.
set(CMAKE_CXX_STANDARD 17)
cmake_minimum_required(VERSION 3.18)
set(dist_folder "${CMAKE_SOURCE_DIR}/bin/")
project(test)
add_definitions(-DNAPI_VERSION=8)
include(FetchContent)
FetchContent_Declare(
    node-api-headers
    URL      https://github.com/nodejs/node-api-headers/archive/refs/tags/v1.1.0.tar.gz
    URL_HASH SHA256=70608bc1e6dddce280285f3462f18a106f687c0720a4b90893e1ecd86e5a8bbf
)
FetchContent_MakeAvailable(node-api-headers)
FetchContent_Declare(
    node-addon-api
    URL      https://github.com/nodejs/node-addon-api/archive/refs/tags/v8.0.0.tar.gz
    URL_HASH SHA256=42424c5206b9d67b41af4fcff5d6e3cb22074168035a03b8467852938a281d47
)
FetchContent_MakeAvailable(node-addon-api)
add_library(${PROJECT_NAME} SHARED
    ${CMAKE_CURRENT_SOURCE_DIR}/addon.cpp
    ${CMAKE_JS_SRC}
)
target_include_directories(${PROJECT_NAME} PRIVATE
  "${node-api-headers_SOURCE_DIR}/include"
  "${node-addon-api_SOURCE_DIR}"
)
target_link_libraries(${PROJECT_NAME} PRIVATE ${DELAYIMP_LIB} ${CMAKE_JS_LIB})
set_target_properties(${PROJECT_NAME} PROPERTIES
    PREFIX ""
    SUFFIX ".node")
add_custom_command(
    TARGET ${PROJECT_NAME} POST_BUILD
    COMMAND ${CMAKE_COMMAND} -E make_directory ${dist_folder}
    COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:${PROJECT_NAME}> ${dist_folder}
)
  1. Build the addon on CentOS using docker
docker run --rm -v $(pwd):/work/src -w /work/src -it quay.io/pypa/manylinux2014_x86_64 /bin/bash -c "mkdir build && cd build && cmake -DCMAKE_LIBRARY_OUTPUT_DIRECTORY=../bin .. && cmake --build . && cmake --install ."
  1. Import the addon from NodeJS 22 on Ubuntu
const addon = require('./bin/test.node');

How often does it reproduce? Is there a required condition?

✔️ If addon is built on CentOS and run from CentOS - it works
✔️ If addon is built on Ubuntu and run from Ubuntu - it works
❌ If addon is built on CentOS and run from Ubuntu - it doesn't work

What is the expected behavior? Why is that the expected behavior?

The addon has been successfully imported.

What do you see instead?

terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc

Additional information

This works for NodeJS 20 and older.

Metadata

Metadata

Assignees

Labels

addonsIssues and PRs related to native addons.

Type

No type

Projects

Status

In Progress

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions