[ruby/prism] Split assertion per expressions

Expressions joined with `&&` are better asserted separately, so that
it is clear from the failure message which expression is false.

Also, `unsigned long` may not be enough for `ptrdiff_t`, e.g., Windows.
Saying that `ptrdiff_t` can be larger than `SIZE_MAX` means that
`PTRDIFF_MAX` is larger than `SIZE_MAX` and `ptrdiff_t` is sufficient
to represent `SIZE_MAX`, otherwise if `PTRDIFF_MAX` is smaller than
`SIZE_MAX`, `diff` will always be smaller than `SIZE_MAX` as well.
`diff` could be equal to `SIZE_MAX` only if `PTRDIFF_MAX` is equal to
`SIZE_MAX` and these assertions would pass, but I don't think there is
any platform where that is the case.

https://github.com/ruby/prism/commit/1fc6dfcada
This commit is contained in:
Nobuyoshi Nakada 2025-01-29 17:51:21 +09:00 committed by git
parent 9826047f01
commit 2b92172894

View File

@ -6169,7 +6169,10 @@ pm_numbered_reference_read_node_number(pm_parser_t *parser, const pm_token_t *to
const uint8_t *end = token->end;
ptrdiff_t diff = end - start;
assert(diff > 0 && ((unsigned long) diff < SIZE_MAX));
assert(diff > 0);
#if PTRDIFF_MAX > SIZE_MAX
assert(diff < (ptrdiff_t) SIZE_MAX);
#endif
size_t length = (size_t) diff;
char *digits = xcalloc(length + 1, sizeof(char));