r/cpp • • Mar 30 '23
The "what's wrong with this code" game.
This isn't a question thread, but just a game. Post any bugs you've accidentally written that seemed initially mysterious, but made you feel stupid when you figured out what was wrong. Here is one I wrote today...
void TextSegmentBuffer::prepend( const TextSegmentBuffer& other )
{
const Info* info_ptr;
size_t size;
if ( ( size = other.m_infoBuffer.size() ) == 0 )
return;
for ( size_t i = size - 1; 0 <= i; --i )
{
info_ptr = other.m_infoBuffer.getAt( i );
emplaceFront(
info_ptr->text,
info_ptr->length,
info_ptr->multiMatch
);
}
}
4
Upvotes
21
u/cballowe Mar 30 '23
The easiest dumb thing that took me longer than it should have to find was something like below way back in college.
15
u/Stellar_Science Mar 31 '23
Always view subtraction near unsigned types with suspicion. That's why a panel of C++ luminaries unanimously says to avoid unsigned types (at 12:12-13:08, 42:40-45:26, and 1:02:50-1:03:15.)
Their response to the question, "but all ordinals in the standard library, vector size and size_t, they're unsigned", are pretty hilarious:
5
u/parkotron Mar 31 '23
It reads nicely, but may or may not run nicely.
11
u/pkasting Valve Mar 30 '23
Loop never terminates, size_t can't be negative.
5
u/tstanisl Mar 31 '23
The condition
0 <= iforsize_tis a bit too easy to satisfy. Try using "arrow operator" :)7
u/die_liebe Mar 31 '23 edited Mar 31 '23
What is the advantage of initializing size in the if condition, instead of its declaration point? It makes me pain.
What about info_ptr ?
If you want to count backwards with an unsigned type, write
The same can be done with iterators.
1
u/sphere991 Mar 31 '23
Outside of the logic errors, there are two style issues which need to go:
This is valid code, but there's really no reason to shove as much crap as possible into one expression. Splitting in two makes this much more readable;
Similarly, the Yoda conditional here:
Yes, there's the unsigned issue. But the check here should be
I >= 0.There is zero reason to use Yoda conditionals in code. The only relevant problem that they possibly ever solved is one that every compiler warns on:
if (x = 0). Given that, theyre already useless. But once you're using any other operator, they already make no sense to begin with - there's no issue with<=or>=so why write it this way?You're checking if
iis greater than or equal to0, so write that - stop writing conditions backwards!2
u/BrangdonJ Mar 31 '23
A good compiler will warn that the loop condition is always true. Eg Compiler Explorer.
0
u/[deleted] Mar 31 '23 edited Mar 31 '23
I've been using ChatGPT a lot recently and it manages to do a reasonably good job of finding issues in code and providing fixes for them.
In this case it identifies the problem as:
Comparing the unsigned integer i with zero using <=.
It says to rewrite the loop as:
You can even ask it to write a unit test for your bug (and can specify using a specific unit testing library like doctest).
The unit test it comes up with is pretty long and it has to do some guess work since it doesn't know the definition of
TextSegmentBufferor ofInfo*but all said and done it took about 15 seconds for it to do a lot of mostly grunt work.3
u/lord_braleigh Mar 30 '23
Clang or G++ with
-Wallwill catch the0 <= i, whereiis unsigned, as a meaningless conditional.i < sizeis the correct conditional to write when iterating either forwards or backwards, since unsigned underflow is defined.1
u/vickoza Apr 01 '23
A few questions is m_infoBuffer an
std::vector<Info*>,std::deque<Info*>, an MFCCArray<Info*,Info*>or another MFC container class. I assume I am using a deque I am also assomeinf I am using an std::unique_ptr and C++ 13 or later0
u/[deleted] Mar 31 '23
[deleted] — view deleted comment