Skip to content

src: enforce assumptions in FIXED_ONE_BYTE_STRING #58155

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

tniessen
Copy link
Member

@tniessen tniessen commented May 4, 2025

These functions are both meant to be used with a null-terminated and thus non-empty sequence of chars. However, there is nothing stopping call sites from passing zero-length sequences, which would certainly not be null-terminated and also would cause an underflow in N - 1. Therefore, this commit

  • changes the size N of the array from int to std::size_t,
  • ensures that compilation will fail if N = 0, and
  • adds a runtime assertion that fails if the N-th char is not \0.

Note that the runtime assertion should be eliminated by any optimizing compiler when given a string literal, which is how these functions are used for the most part (though not exclusively).

These functions are both meant to be used with a null-terminated and
thus non-empty sequence of `char`s. However, there is nothing stopping
call sites from passing zero-length sequences, which would certainly not
be null-terminated and also would cause an underflow in `N - 1`.
Therefore, this commit

- changes the size `N` of the array from `int` to `std::size_t`,
- ensures that compilation will fail if `N = 0`, and
- adds a runtime assertion that fails if the `N`-th `char` is not `\0`.

Note that the runtime assertion should be eliminated by any optimizing
compiler when given a string literal, which is how these functions are
used for the most part (though not exclusively).
@tniessen tniessen added the c++ Issues and PRs that require attention from people who are familiar with C++. label May 4, 2025
@nodejs-github-bot nodejs-github-bot added the needs-ci PRs that need a full CI run. label May 4, 2025
Copy link

codecov bot commented May 4, 2025

Codecov Report

Attention: Patch coverage is 33.33333% with 2 lines in your changes missing coverage. Please review.

Project coverage is 90.17%. Comparing base (92102c0) to head (0e394be).
Report is 6 commits behind head on main.

Files with missing lines Patch % Lines
src/util.h 33.33% 0 Missing and 2 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #58155      +/-   ##
==========================================
- Coverage   90.18%   90.17%   -0.02%     
==========================================
  Files         630      630              
  Lines      186473   186505      +32     
  Branches    36612    36618       +6     
==========================================
+ Hits       168169   168178       +9     
- Misses      11116    11129      +13     
- Partials     7188     7198      +10     
Files with missing lines Coverage Δ
src/util.h 89.65% <33.33%> (-1.58%) ⬇️

... and 30 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c++ Issues and PRs that require attention from people who are familiar with C++. needs-ci PRs that need a full CI run.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants