News for hackers. Stuff that matters.
Re:Analog Devices Expands over 1000 Linux Device Drivers to ... by carapace on Sunday December 05, 2021 @09:21PM (#29455500)
Kind of a tangent, but I found:

https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/lin...

    static bool ltc4306_is_volatile_reg(struct device *dev, unsigned int reg)
    {
      return (reg == LTC_REG_CONFIG) ? true : false;
    }

Oh man, c'mon. That kind of thing bugs me. Just "return reg == LTC_REG_CONFIG" already, sheesh. (And maybe make it a preprocessor directive while you're at it?)
Re:Analog Devices Expands over 1000 Linux Device Drivers to ... by vlovich123 on Sunday December 05, 2021 @09:34PM (#29455566)
Int-> bool conversion is something that MSVC complained about (C4800) for the longest time for some kind of obscure reason. Wouldn’t surprise me if this code was written against a longer that had a similar warning OR that maybe this code is shared with their Windows driver and this needs to compile on both (at least that’s my attempt at finding the most generous explanation for that).
Re:Analog Devices Expands over 1000 Linux Device Drivers to ... by CamperBob2 on Monday December 06, 2021 @12:44AM (#29456571)
That reminds me of one of my favorite footguns:

   void foo(bool bar)
   {
   }

   int main(void)
   {
      foo("Whose bright idea was this?");
   }            
No error, not even a warning, perfectly legit code. If bar had been declared int or char, you'd get an error. Instead, your code ends up less safe because you used bool, thinking you were doing the right thing. Yes, they wanted to allow it anywhere a conditional expression would fit, but implicit conversion from anything other than int and char would have been a good place to draw the line.

So I wouldn't be surprised if the GP example was prompted by a similar design flaw in either the language or a particular compiler.

Re:Analog Devices Expands over 1000 Linux Device Drivers to ... by carapace on Sunday December 05, 2021 @10:01PM (#29455675)
Sorry, what int -> bool conversion?
Re:Analog Devices Expands over 1000 Linux Device Drivers to ... by testrun on Sunday December 05, 2021 @11:18PM (#29456091)
parameter reg is int, return value is bool.
Re:Analog Devices Expands over 1000 Linux Device Drivers to ... by NavinF on Monday December 06, 2021 @02:07AM (#29457032)
“reg == LTC_REG_CONFIG” is a bool. There’s no conversion going on here.
Re:Analog Devices Expands over 1000 Linux Device Drivers to ... by zargon on Monday December 06, 2021 @02:55AM (#29457299)
In the C language, the result of the equality operator is an int, not a bool.
Re:Analog Devices Expands over 1000 Linux Device Drivers to ... by carapace on Monday December 06, 2021 @03:24AM (#29457475)
Ah, whale oil beef hooked, you're right.

> The == (equal to) and != (not equal to) operators are analogous to the relational operators except for their lower precedence. Each of the operators yields 1 if the specified relation is true and 0 if it is false. The result has type int. For any pair of operands, exactly one of the relations is true.

https://port70.net/~nsz/c/c11/n1570.html#6.5.9p3

Thanks for clearing that up. (Although I feel a bit foolish now.)

Re:Analog Devices Expands over 1000 Linux Device Drivers to ... by xorcist on Monday December 06, 2021 @05:30AM (#29458086)
It's not really an explanation though. It's how the language should work.

That ternary operator "?" is just an if-statement which should have the same "problem" with an int argument. It is a broken compiler that would complain about an if statement but an identical ?-statement.

If the int nature of equals-to was a problem, you'd have to use limit its use to switch statements.

Re:Analog Devices Expands over 1000 Linux Device Drivers to ... by zargon on Monday December 06, 2021 @06:06AM (#29458261)
There's no problem with the "int argument". (Not sure if you mean the function argument ("reg") or the operand of the conditional operator, but either way not a problem.) The conflict is between the declared return type of the function and the type of the returned expression.
Re:Analog Devices Expands over 1000 Linux Device Drivers to ... by xorcist on Monday December 06, 2021 @06:54AM (#29458620)
Right, but the same could be said for the ternary operator.
Re:Analog Devices Expands over 1000 Linux Device Drivers to ... by LeifCarrotson on Monday December 06, 2021 @12:22PM (#29462634)
No, it could not. The controlling expression of any kind of conditional statement in C can be an integer. The return type of a function which returns boolean, though, should be boolean.

The strict requirement in the standard is just a 'scalar type': every conditional expression in C, even `if (true)`, technically evaluates on unequal comparison to zero. See section 6.8.4.1 "The if statement" of the standard:

http://www.open-std.org/jtc1/sc22/wg14/www/docs/n2310.pdf

This applies to ternary operators, if statements, do/while loops, for loop termination conditions, etc.

However, when defining a function that returns boolean, you've asked the compiler to make your program more strict than just returning an integer that's zero or nonzero, you're not allowed to return anything but 'true' or 'false' - no, you're not allowed to pack in error codes after the fact!

Re:Analog Devices Expands over 1000 Linux Device Drivers to ... by vlovich123 on Monday December 06, 2021 @09:15AM (#29459947)
The return type of the ternary is bool because of the explicit true/false.
Re:Analog Devices Expands over 1000 Linux Device Drivers to ... by zargon on Monday December 06, 2021 @07:19AM (#29458810)
I don't follow. What could be said about it?
Re:Analog Devices Expands over 1000 Linux Device Drivers to ... by xorcist on Monday December 06, 2021 @02:09PM (#29464180)
Not until quoting the above message did I realize I misread it this whole time.

There is nothing to understand. Sorry for being a bit dense.

Re:Analog Devices Expands over 1000 Linux Device Drivers to ... by zargon on Monday December 06, 2021 @06:30AM (#29458429)
It's not possible to remember all the details of C at one time. I feel like my low-level understanding shifts over different areas over time depending on the type of work I have been doing.

Then there's living in C++ land, which is 1000 times worse.