From 2b92172894e755362a7a0b74ef3b6a5543a89017 Mon Sep 17 00:00:00 2001 From: Nobuyoshi Nakada Date: Wed, 29 Jan 2025 17:51:21 +0900 Subject: [PATCH] [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 --- prism/prism.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/prism/prism.c b/prism/prism.c index dac4078cec..3b23bd3dee 100644 --- a/prism/prism.c +++ b/prism/prism.c @@ -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));