Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Real-life example of bug caused by identifier starting with an underscore [closed]

There is a rule that states that one shouldn't define or use identifiers in C or C++ that start with an underscore followed by a capital, e.g. _Foo. This is because these identifiers are reserved by the compiler and thus might collide with some compiler code and result in undefined behavior.

Although this rule is well-known and adopted by many coding standards, I have never seen a real life situation in which this rule could have prevented a lot of damage.

Does somebody know a real-life example of a violation of this rule? EDIT: I am talking about code that compiles and links fine but shows unexpected behavior because of this.

like image 892
Paul Jansen Avatar asked Jun 13 '17 14:06

Paul Jansen


1 Answers

I worked on a system where our application code had a function _bind() (for binding host variables in SQL statements) that was not made static so it was publicly available.

On one revision (at least) of one O/S (I forget which; it was all in the last millennium), the system provided a function _bind() which — surprise, surprise — had a different interface and did a different job (binding sockets to IP addresses, or thereabouts). When the application code was linked with the system library, the system library code ended up calling our _bind() function when trying to establish network connections. Things did not progress well.

We either renamed the function or made it static (or both) and the problem went away. Modern shared libraries minimize the chances of this sort of thing happening. Our code used static libraries, and I think the O/S was using a static C library too (it was a long time ago!). Using shared libraries alters the dynamics.

Apocryphal information with lots of blurry details omitted — this is the sort of thing that tends to be a transient problem as people fix them fairly quickly.

like image 155
Jonathan Leffler Avatar answered Oct 19 '22 22:10

Jonathan Leffler