Re: git: c18a16ebcf5b - main - kern_proc_kqueues_out(): maxlen == -1 means there is no maxlen
Date: Mon, 24 Mar 2025 15:59:20 UTC
Hi, > (snip) > > + if (maxlen == -1 || maxlen == 0) > As maxlen is of the unsigned type size_t, how can it be -1? > Or am I mistaken on this? It's of course true that 'maxlen' can't be -1 mathematically as it has an unsigned type, but that's not what 'maxlen == -1' tests. In this example, 'maxlen' is an unsigned type of size at least equal to 'unsigned int', whose values cannot all be represented in a signed 'int' (well, that last part is true but not the real reason, which has to do with integer ranks). According to the standard promotion rules, and because -1 as an integer constant is interpreted as of type 'int', both arguments of '==' will be converted to 'unsigned int', and conversion of a signed type to an unsigned one is done by computing its congruence to the number of values the latter can represent (this is the mathematical description; with two-complement representation, that's basically just truncating the bits that are "too high", or sign-extending if the unsigned type destination is wider, and in this precise case, just keeping the same exact representation bits). So, basically, this test is equivalent to 'maxlen == UINT_MAX' on 32-bit machines or 'maxlen == ULONG_MAX' on 64-bit ones. Relevant C standard passages are, in section Language > Conversions > Arithmetic operands, the "Boolean, characters, and integers", "Signed and unsigned integers" and "Usual arithmetic conversions" chapters, and, under Language > Expressions, the chapter about equality operators (in particular, the fact that it states that the usual arithmetic conversions apply), and chapter Language > Lexical elements > Constants > Integer constants. Regards. -- Olivier Certner