Skip to content

[C++][Python] JSON reader segfaults or silently corrupts values when a field changes between array/object and scalar across blocks #55833

In progress

Description

@SohamDutta02

Describe the bug, including details regarding any error messages, version, and platform.

pyarrow.json.read_json crashes the process, or silently returns wrong values, when a field is an array or object in one block and a scalar in a later block. With default options this happens on any file just over 1 MiB. If the conflicting rows are in the same block, it correctly raises ArrowInvalid: JSON parse error: Column(/c) changed from object to number.

import io
import pyarrow.json as pj

data = b'{"c": {"x": 1}}\n' * (2**20 // 16) + b'{"c": 7}\n' * 1000
pj.read_json(io.BytesIO(data))  
pj.read_json(io.BytesIO(b'{"c": [1]}\n{"c": 1}\n'),
             read_options=pj.ReadOptions(block_size=11))  
pj.read_json(io.BytesIO(b'{"c": {"x": 1}}\n{"c": 1}\n'),
             read_options=pj.ReadOptions(block_size=16)).column("c").to_pylist()

Reproduced on pyarrow 12.0.1 through 26.0.1 (Linux x86-64) and with arrow::json::TableReader built from main @ 5ef973d.

Cause: blocks are parsed independently, and ChunkedStructArrayBuilder::InsertChildren passes a field's column to the child builder created for that field's first block without checking its kind (chunked_builder.cc#L419). The list and struct builders then checked_cast it to ListArray / StructArray, guarded only by a DCHECK (#L224-L225, #L318). They should instead return the same changed from X to Y error.

Component(s)

C++, Python

Activity

  1. RajaBabu15 commented on Nov 29, 2026

    @RajaBabu15

    I reproduced this on macOS arm64 with PyArrow 26.0.1 and with a debug build of main. It also crashes in the other order, when a number or string comes first. That path fails in the converters.

    Environment

    • macOS 26.5, arm64
    • PyArrow 26.0.1, Python 3.14.8
    • main at 6f040e8: Debug, ASan and UBSan, Apple clang 21.0.0, CMake 4.5.1

    Reproduce on 26.0.1

    import io
    import pyarrow.json as pj
    
    def read(first, second):
        options = pj.ReadOptions(block_size=len(first) + 1, use_threads=False)
        data = io.BytesIO(f"{first}\n{second}\n".encode())
        return pj.read_json(data, read_options=options).to_pylist()
    Call Observed
    read('{"c":1}', '{"c":{"x":1}}') segfault in 18 of 20 script runs; the other 2 raised ArrowInvalid: Resize capacity must be positive with a negative size
    read('{"c":[1]}', '{"c":1}') segfault (20 of 20)
    read('{"c":{"x":1}}', '{"c":1}') no error, returns [{'c': {'x': 1}}, {'c': {'x': None}}] (20 of 20 script runs)

    Expected: ArrowInvalid ... Column(/c) changed from ... to .... A single-block read already does this for object then number, and open_json does it for object and number in both orders.

    All kind pairs on main

    One field, one line per block, use_threads=false. Row is block 1, column is block 2.

    null bool number string list object
    null ok ok ok ok N N
    bool ok ok error error error error
    number ok A ok error A A
    string ok A ok ok A A
    list ok B B B ok B
    object ok C C C C ok
    • A: abort, converter.cc:51, Check failed: (in->type_id()) == (Type::DICTIONARY)
    • B: abort, chunked_builder.cc:224, Check failed: (unconverted->type_id()) == (Type::LIST)
    • C: abort, uncaught std::bad_cast
    • N: NotImplemented: JSON conversion to ... is not supported. This looks like [C++] An unstable crash may appears when reading table from a json file. #20253, a separate path.
    • error: returns Invalid
    Stack: number then object on main (A)

    Frames shortened.

    converter.cc:51        GetDictionaryArray
    converter.cc:123       NumericConverter<Int64Type>::Convert
    chunked_builder.cc:138 InferringChunkedArrayBuilder::TryConvertChunk
    chunked_builder.cc:120 InferringChunkedArrayBuilder::Insert
    chunked_builder.cc:419 ChunkedStructArrayBuilder::InsertChildren
    reader.cc:290          TableReaderImpl::ParseAndInsert
    reader.cc:269          TableReaderImpl::Read
    
    Stack: object then number on main (C)
    checked_cast.h:38      checked_cast<const StructArray&>
                           ChunkedStructArrayBuilder::Insert
    chunked_builder.cc:419 ChunkedStructArrayBuilder::InsertChildren
                           ChunkedStructArrayBuilder::Insert
    reader.cc:290          TableReaderImpl::ParseAndInsert
    reader.cc:269          TableReaderImpl::Read
    
    macOS crash report: list then number on 26.0.1

    Argument lists and some namespaces shortened.

    EXC_BAD_ACCESS (SIGSEGV), KERN_INVALID_ADDRESS at 0x0000000000000008
    0 libarrow.2600.dylib  arrow::json::InferringChunkedArrayBuilder::Insert(...) + 708
    1 libarrow.2600.dylib  arrow::json::ChunkedListArrayBuilder::Insert(...) + 676
    2 libarrow.2600.dylib  arrow::json::ChunkedStructArrayBuilder::Insert(...) + 1124
    3 libarrow.2600.dylib  TableReaderImpl::Read() lambda invoke + 200
    4 libarrow.2600.dylib  SerialTaskGroup::AppendReal(...) + 132
    5 libarrow.2600.dylib  arrow::json::TableReaderImpl::Read() + 596
    6 _json.cpython-314-darwin.so  pyarrow._json.read_json + 2588
    
    C++ reproducer and build commands
    #include <iostream>
    #include <string>
    
    #include "arrow/api.h"
    #include "arrow/io/memory.h"
    #include "arrow/json/api.h"
    
    arrow::Status Run(const std::string& first, const std::string& second) {
      auto input = std::make_shared<arrow::io::BufferReader>(
          arrow::Buffer::FromString(first + "\n" + second + "\n"));
      auto read = arrow::json::ReadOptions::Defaults();
      read.use_threads = false;
      read.block_size = static_cast<int32_t>(first.size() + 1);
      ARROW_ASSIGN_OR_RAISE(auto reader, arrow::json::TableReader::Make(
                                             arrow::default_memory_pool(), input, read,
                                             arrow::json::ParseOptions::Defaults()));
      ARROW_ASSIGN_OR_RAISE(auto table, reader->Read());
      ARROW_RETURN_NOT_OK(table->ValidateFull());
      std::cout << "returned " << table->num_rows() << " rows: "
                << table->schema()->ToString() << std::endl;
      return arrow::Status::OK();
    }
    
    int main(int, char** argv) {
      arrow::Status status = Run(argv[1], argv[2]);
      if (!status.ok()) std::cout << status.ToString() << std::endl;
    }

    From the repository root:

    cmake -S cpp -B build -G Ninja -DCMAKE_BUILD_TYPE=Debug \
      -DARROW_DEPENDENCY_SOURCE=BUNDLED -DARROW_JSON=ON -DARROW_BUILD_TESTS=ON \
      -DARROW_BUILD_STATIC=OFF -DARROW_USE_ASAN=ON -DARROW_USE_UBSAN=ON
    cmake --build build --target arrow-json-test
    c++ -std=c++20 -g -fsanitize=address,undefined -Icpp/src -Ibuild/src repro.cc \
      -Lbuild/debug -larrow -Wl,-rpath,$PWD/build/debug -o repro
    ./repro '{"c":1}' '{"c":{"x":1}}'

    Notes for a fix

    • Both traces above go through InsertChildren, the place the report points to. When the number comes first, the failure is further down: GetDictionaryArray only has a DCHECK. BooleanConverter already returns Invalid for a wrong input type.
    • A check in InsertChildren alone would miss values inside lists. {"c":[1]} then {"c":[{"x":1}]} also aborts at converter.cc:51.
    • The 246 tests in arrow-json-test pass on this build.
    • Not checked: Linux, x86, nested explicit schemas.
    Terminal output

    PyArrow 26.0.1: negative size error, then segfault. Traceback trimmed.

    >>> read('{"c":1}', '{"c":{"x":1}}')
    pyarrow.lib.ArrowInvalid: Resize capacity must be positive (requested: -504403121473044216)
    >>> read('{"c":[1]}', '{"c":1}')
    zsh: segmentation fault
    
    pyarrow-26 0 1-crash

    main at 6f040e8, debug build: number then object

    $ repro/repro_cc '{"c":1}' '{"c":{"x":1}}'
    src/cpp/src/arrow/json/converter.cc:51:  Check failed: (in->type_id()) == (Type::DICTIONARY)
    zsh: abort
    
    main-converter-check
  2. RajaBabu15 commented on Nov 29, 2026

    @RajaBabu15

    I am working on a fix for this.

  3. added 10 commits that reference this issue on Nov 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions