Dear All, I've a strange bug, I've attached a small source which shows my issue.
So, I am trying to assign a string containing an embedded \0 in it. You will find that in test() I use std::string::assign to do this, but in two cases the results are different.
Please see the output of the attached source, and let me know if I am missing something, or it is a bug in g++/stdc++.
Of course, the third form in test() works as expected, but in my assumption all should.
With this g++ it behaves bad: $ g++ -v Using built-in specs. COLLECT_GCC=g++ COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/4.7/lto-wrapper Target: x86_64-linux-gnuConfigured with: ../src/configure -v --with-pkgversion='Debian 4.7.1-2' --with-bugurl=file:///usr/share/doc/gcc-4.7/README.Bugs --enable-languages=c,c++,go,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.7 --enable-shared --enable-linker-build-id --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.7 --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-gnu-unique-object --enable-plugin --enable-objc-gc --with-arch-32=i586 --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix gcc version 4.7.1 (Debian 4.7.1-2) But with freebsd's g++: $ g++ -v Using built-in specs. Target: amd64-undermydesk-freebsd Configured with: FreeBSD/amd64 system compiler Thread model: posix gcc version 4.2.1 20070831 patched [FreeBSD] It works fine. Thanks in advance, Kojedzinszky Richard Euronet Magyarorszag Informatikai Zrt.
#include <iostream> #include <string> #include <cstring> void test(const char *p) { std::string v; v.assign(p, ::strlen(p)+1); std::cerr << "v=" << static_cast<const void*>(v.c_str()) << " size=" << v.size() << std::endl; v.assign(p, 1); std::cerr << "v=" << static_cast<const void*>(v.c_str()) << " size=" << v.size() << std::endl; v.assign(p); v.append(1, '\0'); std::cerr << "a=" << static_cast<const void*>(v.c_str()) << " size=" << v.size() << std::endl; } int main() { std::string x = ""; test(""); test(x.c_str()); }