python / python/cpython

Summary When running _testembed under LeakSanitizer, test_initconfig_get_api reports a memory leak.

Open
#145,204 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

tests topic-C-API type-bug
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

Bug report

Bug description:
Summary

When running _testembed under LeakSanitizer,
test_initconfig_get_api reports a memory leak.

Reproducer

Build CPython with sanitizers:

./configure \
  --with-pydebug \
  --with-address-sanitizer \
  --with-undefined-behavior-sanitizer
make -j

Run the specific test:

ASAN_OPTIONS=detect_leaks=1 \
./Programs/_testembed test_initconfig_get_api
Observed result

LeakSanitizer reports:

ERROR: LeakSanitizer: detected memory leaks

Direct leak of 1 byte(s) in 1 object(s) allocated from:
    #0  malloc
    #1  PyInitConfig_GetStrList Python/initconfig.c:4024
    #2  test_initconfig_get_api Programs/_testembed.c:1844
Root cause

In test_initconfig_get_api:

  1. The initial call to
PyInitConfig_GetStrList(config, "xoptions", &length, &items)

returns an empty list (length == 0).

  1. The returned items buffer was not freed.
  2. The PyInitConfig *config created by PyInitConfig_Create() was not released before returning.

Under LeakSanitizer, this results in a small direct leak and indirect leaks associated with the configuration lifetime.

Environment
  • Architecture: aarch64-linux-gnu

  • Built with: --with-address-sanitizer

  • Leak detected via: ASAN_OPTIONS=detect_leaks=1

CPython versions tested on:

CPython main branch

Operating systems tested on:

No response

Linked PRs
  • gh-145206

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in Programs/_testembed.c at test_initconfig_get_api and review the PyInitConfig_GetStrList result and configuration lifetime described in the report. Run the provided sanitizer build and ./Programs/_testembed test_initconfig_get_api; done means the test completes without LeakSanitizer reporting the direct or associated configuration leaks.

Written by the indexing model from the issue text.

Assessment

Tech stack
c, python
Domain
testing
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.