system/cle: overlong line executed as a truncated command at EOL

Open Beginner friendly
#4,108 4 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
72/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
c
Domain
cli, embedded-iot

Research direction

Read system/cle/cle.c: start at struct cle_s and the cle() loop that inserts printable/tab characters, then find where \n is handled and the line is returned to NSH. Add a bool overflow flag that is set when insertion no longer fits (emit BEL, keep consuming), and on newline while overflowing clear the buffer, echo \r\n and return OK. Verify by building sim:nsh with CONFIG_NSH_CLE=y and CONFIG_LINE_MAX=80 and feeding an overlong poweroff line followed by uname -a; done means the overlong line is discarded, the follow-up runs, and an exactly-fitting line still executes.

Written by the indexing model from the issue text.

Description

Repository: apache/nuttx-apps · Component: system/cle/cle.c (selected by CONFIG_NSH_CLE=y)

Environment: NuttX Apps 13.1.0, also master 7196658dffde; host sim:nsh (not QEMU, not a board); Linux x86_64, gcc 11.4; CONFIG_NSH_CLE=y, CONFIG_SYSTEM_CLE=y, CONFIG_LINE_MAX=80 (79 usable bytes).

Repro

  1. Build sim:nsh with the configuration above.
  2. Write poweroff followed by 200 A and \n to stdin.
  3. After 0.5 s write uname -a\n, then observe whether the process is still alive and whether the follow-up command runs. The test driver kills the process after a 2 s timeout if it did not exit on its own.

Actual (stock 13.1.0) — insertion stops at 79 bytes and further bytes ring the bell, but the newline still submits the truncated buffer:

nsh> poweroff AAAA...        <- 79 bytes accepted, further bytes rejected with BEL
(process exits with status 0 within 0.5 s; the follow-up command never runs)

The tail of the line is not discarded: it is consumed as new input and runs as further commands (e.g. a run of C characters ends up executed as a command named C).

Expected — the whole line is discarded through EOL and the next command runs:

nsh> poweroff AAAA...<BEL>   <- discarded; poweroff is not executed
(empty line returned to NSH)
nsh> uname -a
NuttX ... sim sim            <- follow-up command runs normally
(process stays alive; the harness kills it after the 2 s timeout)

Fix suggestion: add bool overflow to struct cle_s. When a printable or tab character no longer fits, set overflow, emit BEL, and keep consuming bytes without inserting them. On \n while overflowing, clear the buffer, echo \r\n, and return OK so NSH receives an empty line. A line that exactly fits must still execute, and a failed newline insertion alone must not set overflow. The patch changes only system/cle/cle.c.

Patch: 0002-cle-discard-overlong-line.patch, posted in the first comment. It applies to master 7196658dffde (git apply --check returns 0). CLE only covers the interactive editor; NSH script mode still calls readline_fd(), so a product using CLE needs both patches.


Reported from an audit of NSH input handling. Patch is in the first comment; happy to rework it if maintainers prefer a different approach.

Dominant language
C
Stars
474
Forks
807
Avg merge
1d 11h
Merged PRs (30d)
181

Getting set up

  • No Dockerfile or Docker Compose file
  • Has a pull request template
  • No 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.

More from apache/nuttx-apps

All issues in apache/nuttx-apps

Similar issues

More C issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.