[llvm-bugs] [Bug 38657] Test using strcmp fails after r339410

via llvm-bugs llvm-bugs at lists.llvm.org
Tue Aug 21 02:40:04 PDT 2018


https://bugs.llvm.org/show_bug.cgi?id=38657

Dimitry Andric <dimitry at andric.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |dimitry at andric.com
             Status|NEW                         |RESOLVED
         Resolution|---                         |INVALID

--- Comment #3 from Dimitry Andric <dimitry at andric.com> ---
As far as I can see, the code is invalid. The String() temporary object created
in the first line of main() is immediately destructed afterwards, making str1
point to invalidated memory.

Demonstration:

/* test.cpp */
#include <string.h>
#include <assert.h>
#include <stdio.h>

struct String {
  char    content[100];
  String (const char* a) {
    strcpy(content, a);
  }
  ~String () {
    printf("destroying String\n");
  }
  operator const char* () const {
    return content;
  }
};

int main()
{
  char const* str1 = String("three");
  printf("checking assertion\n");
  assert(strcmp(str1, "three") == 0);

  return 0;
}

$ clang -O2 pr38657-2.cpp -o pr38657-2
$ ./pr38657-2
destroying String
checking assertion
Assertion failed: (strcmp(str1, "three") == 0), function main, file
pr38657-2.cpp, line 23.
Abort trap (core dumped)

E.g., you should ensure the String object is not destroyed before checking the
assertion.

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20180821/1416a5a2/attachment-0001.html>


More information about the llvm-bugs mailing list