|
|
Log in / Subscribe / Register

A tale of two data-corruption bugs

A tale of two data-corruption bugs

Posted May 26, 2015 10:39 UTC (Tue) by alankila (guest, #47141)
Parent article: A tale of two data-corruption bugs

Would probably be good idea to go through the kernel source tree now and replace every lowercase macro name with uppercase version.


to post comments

A tale of two data-corruption bugs

Posted May 26, 2015 12:57 UTC (Tue) by JdGordy (subscriber, #70103) [Link] (6 responses)

Switching to uppercase wouldn't solve anything in this case....

A tale of two data-corruption bugs

Posted May 26, 2015 15:41 UTC (Tue) by iabervon (subscriber, #722) [Link]

If the macro name had been uppercase from the start, the original code probably wouldn't have had the bug of changing sector. And the way to avoid that bug that the author chose would probably not have been to reload it, since the author was more likely at that time to know that bio had changed than Neil Brown was recently.

It's also easier to catch the second problem in review with it being new code than as a change to old code.

A tale of two data-corruption bugs

Posted May 27, 2015 20:05 UTC (Wed) by alankila (guest, #47141) [Link] (4 responses)

Macros can do strange things, such as appear to manipulate values which are not passed as a pointer. Syntactically, it is better if they are different from functions.

A tale of two data-corruption bugs

Posted May 27, 2015 23:00 UTC (Wed) by cesarb (subscriber, #6266) [Link] (1 responses)

> Syntactically, it is better if they are different from functions.

Interestingly, the Rust language does precisely that: macros always have a ! suffix (like format!(...)).

But I agree with the parent comment: making it obvious that it's a macro (all-uppercase, special suffix, etc) would have made no difference. If a programmer sees macro called SECTOR_DIV, receiving a sector number and a sector size, what would the programmer think? "It's a macro which does whatever magic is necessary to divide a sector number by a sector size." The programmer would not expect it to overwrite the sector size passed in! The kernel is full of function-like macros (or functions which are macros in some kernel configurations), no matter how they're named people will expect them to behave like normal functions.

Now suppose the macro had one extra parameter, like "... = SECTOR_DIV(sector, sector, sector_size)". The programmer would question, "why is sector passed twice? Clearly one of them must be an output, or some other arcane trickery; I must look into its definition," and the bug wouldn't happen.

A tale of two data-corruption bugs

Posted May 28, 2015 0:24 UTC (Thu) by alankila (guest, #47141) [Link]

I can't believe that I have to argue such an obvious point.

I assert that chances of mistakes like these are almost definitely increased when macros look like function calls. If sector_div had actually been a function, it couldn't have altered the input argument given the way it was called. Because it looks like a function, it dupes programmers with a false expectation. To crystallize, things that are different in behavior should also look different. Therefore, having upper-case named macros is prudent (especially when they do macroy stuff that is impossible for functions to do) and it would increase the likelihood that someone checks the implementation because a macro can really do very strange things that break syntactic expectations. You don't know about a C macro where exactly the argument is expanded, or how many times it will be evaluated.

Anyway, macros are evil and should die, especially macros for which inline functions are a perfectly acceptable substitute.

A tale of two data-corruption bugs

Posted Jun 7, 2015 14:17 UTC (Sun) by parcs (guest, #71985) [Link] (1 responses)

Thankfully C++ makes this strange behavior a feature of normal functions.

A tale of two data-corruption bugs

Posted Jun 8, 2015 13:22 UTC (Mon) by zlynx (guest, #2285) [Link]

Which means that as a C++ programmer you realize that you always need to check the function declaration. Always.

It isn't a bad idea to do it for C either. Sure you can see that you are using the & to create a pointer and pass it to a function. What do you do for the second level function that passes that pointer to a third level? No visual indicator there at all.


Copyright © 2026, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds