c-strip: NULL-pointer dereference (crash) on input that produces no output, e.g. a lone /

Open Beginner friendly
#1 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
82/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Quiet
Tech stack
c
Domain
cli

Research direction

Start in c-strip.c at strip_trailing_space() line 118 and trace the buffer handling from strip_file() line 262, using printf '/' | ./c-strip as the reproducer. Build with AddressSanitizer and UndefinedBehaviorSanitizer, then verify that a lone slash no longer causes a NULL-pointer write or crash.

Written by the indexing model from the issue text.

Description

c-strip crashes with a NULL-pointer write when a processed line produces no output, leaving the output buffer unallocated. The simplest trigger is an input consisting of a single '/'.

Reproduce:
printf '/' | ./c-strip

Built with -fsanitize=address,undefined:
c-strip.c:118:20: runtime error: store to null pointer of type 'char'
AddressSanitizer: SEGV on unknown address 0x000000000000 (WRITE)
#0 strip_trailing_space c-strip.c:118
#1 strip_file c-strip.c:262

Cause:

The output buffer o->p is only allocated on the first addc() call. A lone / sets state = SLASH without emitting output, so addc() is never called and o->p stays NULL. strip_trailing_space() then dereferences it:
buf->p[buf->len] = '\0'; /* c-strip.c:118 — buf->p is NULL, buf->len is 0 */

Dominant language
C
Stars
13
Forks
1
PR merge metrics
No merged PRs in 30d

Contributor guide

No contributing guide indexed for this repository

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.

Similar issues

More C issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.