GertWollny

Strange bug resulting from linking

Discussion created by GertWollny on Oct 15, 2009
Latest reply on Nov 26, 2009 by sss4749
std::string::empty() may report an empty string

Hi,

when I compile the attached program with openCC, the resulting program reports the string to be empty, although it certainly is not as compiling the same program with g++ and running it prooves. 

g++ creates the following code:

if (a.empty())
 1e3:   48 8d 7d e0             lea    -0x20(%rbp),%rdi
 1e7:   e8 00 00 00 00          callq  1ec <main+0x64>
 1ec:   84 c0                   test   %al,%al
 1ee:   74 2a                   je     21a <main+0x92>

openCC does: 

 if (a.empty())
  d6:   48 8d bd 60 fe ff ff    lea    -0x1a0(%rbp),%rdi
  dd:   e8 00 00 00 00          callq  e2 <main+0x80>
  e2:   85 c0                   test   %eax,%eax
  e4:   74 33                   je     119 <main+0xb7>

i.e. openCC expects string.empty() to set the full 32 bit of  %eax to zero and g++ only requires the lower 8 bits %al to be set to zero.

Since the return type of empty() is bool, and sizeof(bool) is 1 for openCC, the code should most certainly do the same as g++ and only test  %al.

Best,

Ger t

#include <iostream> #include <string> #include <sstream> using namespace std; int main(int argc, char *args[]) { const string a("1.0*ssd"); if (a.empty()) cout << "boned: the string '" << a << "' is considered to be empty\n"; else { cout << "Live is good\n"; return 0; } bool second_try = a.empty(); if (second_try) cout << "Well, at least the compile sticks to his opinion.\n"; else cout << "Arrg, now its correct\n"; return 0; }

Outcomes