Skip to content

libs/libc: make printf octal formatting optional for DEFAULT_SMALL - #22162

Merged
GUIDINGLI merged 1 commit into
apache:masterfrom
Swatantra-66:fix/libc-printf-octal-default-small
Nov 23, 2026
Merged

GUIDINGLI merged 1 commit into
apache:masterfrom
Swatantra-66:fix/libc-printf-octal-default-small

Conversation

@Swatantra-66

Copy link
Copy Markdown
Contributor

Summary

Memory-constrained embedded targets (DEFAULT_SMALL=y) rarely format integers in octal notation (%o). The vsprintf engine in libs/libc/stream/lib_libvsprintf.c already provides dead-code elimination via CONFIG_LIBC_PRINTF_OCTAL, printing '?' when octal support is disabled. However, CONFIG_LIBC_PRINTF_OCTAL currently defaults unconditionally to y.

Change CONFIG_LIBC_PRINTF_OCTAL to default to LIBC_PRINTF_FULL || !DEFAULT_SMALL. This eliminates octal parsing, alternate form (%#o) checks, and base-8 conversion logic on small profiles, saving 178 bytes of Flash (text) and 32 bytes of RAM (bss) in lib_libvsprintf.o (176 bytes Flash in the linked nuttx binary on sim:nsh), while preserving full conformance when LIBC_PRINTF_FULL is selected or on standard targets.

Part of Issue #20414.

Impact

  • Is new feature added? No (adjusts default of existing Kconfig option CONFIG_LIBC_PRINTF_OCTAL).
  • Is existing feature changed? Yes, printf("%o") outputs '?' on DEFAULT_SMALL=y targets unless explicitly enabled or using LIBC_PRINTF_FULL.
  • Impact on user: None for standard builds or when LIBC_PRINTF_FULL=y. On DEFAULT_SMALL=y targets, saves Flash without manual Kconfig tuning.
  • Impact on build: None.
  • Impact on hardware: None.
  • Impact on documentation: Updated Documentation/os/libs/libc/printf.rst.

Size Difference (sim:nsh)

Configuration: sim:nsh (x86_64)

libs/libc/bin/lib_libvsprintf.o

Section CONFIG_LIBC_PRINTF_OCTAL=y CONFIG_LIBC_PRINTF_OCTAL=n Difference
text 12589 12411 -178 bytes (-1.4%)
data 560 560 0 bytes
bss 1840 1808 -32 bytes
Total 14989 14779 -210 bytes (-1.4%)

Linked Binary (nuttx)

Section CONFIG_LIBC_PRINTF_OCTAL=y CONFIG_LIBC_PRINTF_OCTAL=n Difference
text 1245740 1245564 -176 bytes
data 287336 287336 0 bytes
bss 207136 207104 -32 bytes
Total 1740212 1740004 -208 bytes

Testing

Build Verification

  • Clean build with CONFIG_LIBC_PRINTF_OCTAL=y: Success.
  • Clean build with CONFIG_LIBC_PRINTF_OCTAL=n: Success.

Runtime Test Log (sim:nsh, CONFIG_LIBC_PRINTF_OCTAL=n)

nsh> help
help usage:  help [-v] [<cmd>]

    .           dirname     free        mount       set         umount      
    [           date        help        mv          kill        unset       
    ?           df          hexdump     pidof       pkill       uptime      
    alias       dmesg       losetup     poweroff    sleep       watch       
    unalias     echo        ln          quit        usleep      xd          
    basename    env         ls          printf      source      wait        
    break       exec        du          ps          test        
    cat         exit        mkdir       pwd         time        
    cd          expr        mkfatfs     readlink    true        
    cp          false       mkfifo      rm          truncate    
    cmp         fdinfo      mkrd        rmdir       uname       

Builtin Apps:
    dd           gcov         hello        nsh          sh           
    dumpstack    gpio         init         ostest       
nsh> ps
  TID   PID  PPID PRI POLICY   TYPE    NPX STATE    EVENT     SIGMASK            STACK COMMAND
    0     0     0   0 FIFO     Kthread   - Ready              0000000000000000 0069584 Idle_Task
    1     0     0 224 FIFO     Kthread   - Waiting  Semaphore 0000000000000000 0067456 sim_loop_wq 0x74a702200400 0x74a702200488
    2     0     0 224 FIFO     Kthread   - Waiting  Semaphore 0000000000000000 0067472 hpwork 0x4017ccc0 0x4017cd48
    4     4     0 100 FIFO     Task      - Waiting  Semaphore 0000000000000000 0067504 init_main
    7     7     4 100 FIFO     Task      - Running            0000000000000000 0067504 sh
nsh> poweroff

Memory-constrained embedded targets (DEFAULT_SMALL=y) rarely format
integers in octal notation (%o). The vsprintf engine in
libs/libc/stream/lib_libvsprintf.c already provides dead-code elimination
via CONFIG_LIBC_PRINTF_OCTAL, printing '?' when octal support is disabled.
However, CONFIG_LIBC_PRINTF_OCTAL currently defaults unconditionally to 'y'.

Change CONFIG_LIBC_PRINTF_OCTAL to default to LIBC_PRINTF_FULL ||
!DEFAULT_SMALL. This eliminates octal parsing, alternate form (%#o)
checks, and base-8 conversion logic on small profiles, saving 178 bytes
of Flash (text) and 32 bytes of bss in lib_libvsprintf.o (176 bytes of
Flash in the linked nuttx binary on sim:nsh), while preserving full
conformance when LIBC_PRINTF_FULL is selected or on standard targets.

Assisted-by: Claude:claude-opus-4.6
Signed-off-by: Swatantra Yadav <[email protected]>
@github-actions github-actions Bot added Area: Documentation Improvements or additions to documentation Size: XS The size of the change in this PR is very small labels Nov 22, 2026
@github-actions

Copy link
Copy Markdown

MemBrowse Memory Report · Explore what changed →

arduino-mega2560

  • flash: .text -48 B (-0.1%, 64,839 B / 262,144 B, total: 25% used)

hifive1-revb

  • flash: .text -48 B (-0.1%, 87,548 B / 4,194,304 B, total: 2% used)

mirtoo

  • kseg0_progmem: .text -44 B (-0.1%, 66,896 B / 131,072 B, total: 51% used)

stm32-nucleo-f103rb

  • flash: .text +36 B (+0.1%, 35,432 B / 131,072 B, total: 27% used)

No memory changes detected for:


Catch memory regressions in CI with MemBrowse. Free for open source.

@GUIDINGLI
GUIDINGLI merged commit efd476f into apache:master Nov 23, 2026
59 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area: Documentation Improvements or additions to documentation Size: XS The size of the change in this PR is very small

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants