Public bug reported:

Ubuntu release: 24.04 LTS (noble), seen on arm64 and amd64
Package: bc 1.07.1-3ubuntu4

This is a low-severity report for the record. No stable release update
is requested. A fix, if any, belongs in the development release,
preferably inherited from upstream.

== What happens ==

Every run of /usr/bin/bc loses one 41-byte allocation made at startup.
valgrind memcheck reports it as "definitely lost" for any input,
including empty stdin.

== Steps to reproduce ==

 echo '1+1' | valgrind --leak-check=full --show-leak-kinds=definite bc

Result:

 41 (40 direct, 1 indirect) bytes in 1 blocks are definitely lost in loss 
record 11 of 21
   at malloc
   by ??? (in /usr/bin/bc)
   by ??? (in /usr/bin/bc)
   by (below main) (libc_start_call_main.h:58)

With bc-dbgsym the two unnamed frames resolve to bc_new_num
(lib/number.c:72) and main (bc/main.c:222, with init_storage and
bc_init_numbers inlined).

Expected: "definitely lost: 0 bytes in 0 blocks".

No growth: the result is the same 41 bytes in 1 block for 1, 1000 and
100000 input lines.

== Cause ==

bc_init_numbers() in lib/number.c allocates three global constants at
startup: _zero_, _one_ and _two_. _two_ is read only by bc_raisemod(),
and the only caller of bc_raisemod() is dc (dc/numeric.c). bc itself
uses bc_raise().

Ubuntu builds bc with -flto=auto. In the bc binary the linker removes
bc_raisemod(), and then the store to _two_, which is now write-only.
Neither symbol is present in the binary, while _zero_ and _one_ are. The
pointer returned for _two_ is therefore never kept, and the block is
unreachable from the start.

A build of the same source without LTO keeps the global, and the block
shows as "still reachable". Rebuilding the noble source with the archive
flags plus AddressSanitizer reproduces the 40 + 1 byte leak with the
full stack (bc_new_num, bc_init_numbers at number.c:112, init_storage at
storage.c:67, main at main.c:222). The same tree built at -O0 without
-flto reports nothing.

/usr/bin/dc from the same package is not affected, because dc uses
_two_.

== Impact ==

None at run time: one fixed 41-byte block per bc process, allocated
before any input is read, reclaimed at exit.

The only effect is on leak checking. A test harness that runs shell
scripts under valgrind --trace-children=yes and gates on "definitely
lost: 0" gets a record for every script that calls bc.

== Possible fixes ==

Either of:
 (a) free _zero_, _one_ and _two_ in bc_exit() (bc/util.c) before exit(). 
bc_free_num() is reference counted, so shared constants are only decremented.
 (b) allocate _two_ only on dc's startup path, so bc never makes the allocation.

== Workaround ==

A valgrind suppression for the single record (fun:malloc,
obj:/usr/bin/bc, obj:/usr/bin/bc).

== Other information ==

Upstream GNU bc 1.08.2 has the same bc_init_numbers() and no cleanup of
these constants, so there is no upstream fix to reference. Upstream does
accept small leak fixes: the 1.08.2 ChangeLog entry of 2025-01-10 plugs
a leak of the "parity" variable in bc_raisemod().

None of the Ubuntu patches in 1.07.1-3ubuntu4 touches bc_init_numbers().
No existing Launchpad or Debian report describes this.

** Affects: bc (Ubuntu)
     Importance: Undecided
         Status: New


** Tags: noble

-- 
You received this bug notification because you are a member of Ubuntu
Bugs, which is subscribed to Ubuntu.
https://bugs.launchpad.net/bugs/2174803

Title:
  bc: 41-byte startup constant _two_ is reported as definitely lost by
  valgrind because the LTO build drops its only reader

To manage notifications about this bug go to:
https://bugs.launchpad.net/ubuntu/+source/bc/+bug/2174803/+subscriptions


-- 
ubuntu-bugs mailing list
[email protected]
https://lists.ubuntu.com/mailman/listinfo/ubuntu-bugs

Reply via email to