Skip to content

apps: Check string allocation failures#3497

Merged
linguini1 merged 2 commits into
apache:masterfrom
nightt5879:nightt5879/fix-allocation-checks-1727
May 23, 2026
Merged

apps: Check string allocation failures#3497
linguini1 merged 2 commits into
apache:masterfrom
nightt5879:nightt5879/fix-allocation-checks-1727

Conversation

@nightt5879
Copy link
Copy Markdown
Contributor

Summary

Refs #1727.

This PR adds missing allocation-failure handling for selected strdup() and asprintf() call sites.

Commit structure:

  • Commit 1 (apps: Fix unchecked strdup()/asprintf() as requested in #1727) fixes the direct strdup() / asprintf() cases requested in Strings allocations checks #1727 where the allocated result is used locally before a failure check.
  • Commit 2 (netutils/thttpd: Apply the same check to related allocation sites) applies the same check to httpd_strdup(), the thttpd-local wrapper around strdup().

The second commit is logically separable, happy to drop if out of scope.

Scope notes:

  • The direct fixes cover app-owned call sites where failure would otherwise flow into immediate use, such as path splitting, directory list construction, login path setup, and web directory listing formatting.
  • The related extension is limited to httpd_strdup() because it has the same NULL-on-allocation-failure contract as strdup() and the caller used the result before checking it.
  • I did not expand this PR to malloc(), calloc(), realloc(), or other raw allocation APIs because Strings allocations checks #1727 specifically asks about strdup() / asprintf(), and covering all allocation APIs would broaden the review substantially.

Impact

No user-facing behavior change is intended except that low-memory paths now fail cleanly instead of using NULL allocation results.

  • New feature: NO
  • User adaptation required: NO
  • Build process change: NO
  • Hardware/architecture/board change: NO
  • Documentation update required: NO
  • Security impact: NO intended security impact; this improves low-memory failure handling.
  • Compatibility impact: NO intended compatibility impact.

Testing

Host:

  • Windows with WSL Ubuntu 24.04
  • CPU: x86_64
  • Compiler: GCC 13.3.0

Target:

  • sim:nsh

Checks run:

  • git diff --check upstream/master..HEAD
  • checkpatch.sh -c -u -m -g HEAD~2..HEAD from a WSL temp clone with codespell / cvt2utf in a temporary venv
  • ./tools/configure.sh -a ../nuttx-apps-check-1727 sim:nsh
  • make -j$(nproc)

Results:

  • git diff --check: pass
  • checkpatch.sh: pass
  • sim:nsh build: pass; build completed and generated nuttx.tgz

Note: the WSL temp build printed clock-skew warnings on .config; compilation and link completed successfully.

Add missing failure handling for direct strdup() and asprintf() calls where the allocated result is consumed locally before any NULL/error check.

This keeps the scope to the functions named in apache#1727 and avoids changing pass-through return sites where callers already receive NULL on allocation failure.

Signed-off-by: Nightt <87569709+nightt5879@users.noreply.github.com>
Extend the allocation-failure check to httpd_strdup(), the thttpd-local wrapper that has the same failure contract as strdup().

This is logically separable from the direct strdup()/asprintf() fixes: it prevents using hs->hostname before checking whether the wrapped string allocation succeeded, and releases the partially allocated server state on failure.

The scope intentionally does not extend to malloc(), calloc(), or other raw allocators because apache#1727 specifically calls out strdup()/asprintf(), and covering all allocation APIs would make this PR much broader.

Signed-off-by: Nightt <87569709+nightt5879@users.noreply.github.com>
@nightt5879 nightt5879 marked this pull request as ready for review May 23, 2026 03:04
@xiaoxiang781216 xiaoxiang781216 linked an issue May 23, 2026 that may be closed by this pull request
@linguini1 linguini1 merged commit 8568013 into apache:master May 23, 2026
41 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Strings allocations checks

3 participants