ALTracer commented on PR #6613: URL: https://github.com/apache/incubator-nuttx/pull/6613#issuecomment-1186605916
> You can't compare signed and unsigned. If you try to compare signed and unsigned, signed value will be converted to unsigned. And so, considering 8bits vars, `-1 < UCHAR_MAX` will return `false`, when you would expect it to return `true`. Reason for this is, that netagive numbers are represented as 2 complement, so -1 in bit representation will be `11111111` (-2 will be `11111110` and so on) which is equal to `UCHAR_MAX`. Thanks for the refresher. The logic is always nice and understandable with small values and intervals. > `(LONG_MIN <= x && x <= LONG_MAX)` This part only checks if `x` is within signed values, so around +-2million. Unsigned values can be bigger and can be up to around 4million. If you'd pass 4million value, assert would trigger as 4million is bigger than 2million. That's where the second part comes in. That is usually right. However, vsprintf flips the sign before passing the x to `__ultoa_invert(unsigned long long val, char str, int base)` (and before the first assert) https://github.com/apache/incubator-nuttx/blob/8f465c43cee41c99d857c8cf838c0d86de4ff186/libs/libc/stdio/lib_libvsprintf.c#L1032 ```c if (x < 0) { x = -x; flags |= FL_NEGATIVE; } ``` > `(0 < x && x <= ULONG_MAX)` It simply checks, if number is bigger than 0, then check if it's lower than 4million. So for 4million value, first part will return `false` but second will return `true` so whole assert will be true and it won't trigger. There are two asserts against differently typed x: first is `long long x` in %d/%i branch, second is `unsigned long long x` in %u branch. Why should we check a %u argument against int32_t limits anyways? > 0 in that case will be promoted to unsigned long. No, because x is `long long` there (with sign already flipped to positive) and 0 is int by default. Yes, because x is `unsigned long long` there and 0 will be promoted to its type. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: commits-unsubscr...@nuttx.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org