-
Notifications
You must be signed in to change notification settings - Fork 227
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
header file <pqxx/pqxx> seems to cause: free() double free issues #681
Comments
Thank you for reporting this. (I edited your message slightly to stop Github from trying to interpret the code as Markdown.) Did you also get a nonzero return code, or did the program seem to complete successfully? |
Hello -
My SQL query was successful - query results were correct. But as I was
trying to isolate
the problem I began trimming down my code to try and find where the error
was coming from.
If I simply include the header and link with the libpqxx library with a
barebones main() I get that
message when I run the binary...
Thanks!
Dave
…On Wed, May 3, 2023 at 9:21 AM Jeroen Vermeulen ***@***.***> wrote:
Thank you for reporting this. (I edited your message slightly to stop
Github from trying to interpret the code as Markdown.)
Did you also get a nonzero return code, or did the program seem to
complete successfully?
—
Reply to this email directly, view it on GitHub
<#681 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/A7SSVMWTTU3OPPAAGUHRNITXEJLW5ANCNFSM6AAAAAAXUKQLM4>
.
You are receiving this because you authored the thread.Message ID:
***@***.***>
--
Regards,
Dave
|
Ah, I'm not actually sure that I looked closely enough at the output. I was expecting to see an error as well. I'll try again. My best guess at an explanation for now is that it's something to do with destruction and cleanup of a global constant, after In that murky execution environment it's possible to have subtle bugs, whether in libpqxx or in the infrastructure such as the libc implementation, or things that debugging tools can't quite oversee. I've had false positives of this kind even from very good tools (such as valgrind). So it's possible the message will just go away with a package upgrade — though of course we can't count on that. I'm kind of clutching at straws here, but it might be helpful to know...
If I can reproduce the problem on my end now, then it's probably easiest if I check these. |
Oh, another question! Did you build using CMake, or using the configure script? |
The questions don't stop. :-)
|
Unfortunately I couldn't bring up a docker container for Debian's Stable or Unstable with the right packages — a package failed to install. So can't try those right now. |
I used the configure script to build libpqxx.
My configure options as follows:
--prefix=/usr/local --with-postgres-include=/opt/postgres/include
--with-postgres-lib=/opt/postgres/lib --enable-shared
There is a file called compile_flags that has the following:
-I/opt/postgres/include -g -O2 -fvisibility=hidden
-fvisibility-inlines-hidden
I used gcc/g++ version 12.2 which defaults to c++17.
My binutils is:
ld --version
GNU ld (GNU Binutils for Debian) 2.31.1
Copyright (C) 2018 Free Software Foundation, Inc.
I searched for this in errno.h and the highest Linux goes is 133.
I found a 134 in a postgres header: ./server/utils/fmgroids.h but this
does not appear to be an error number.
Also, in my bare-bones main() code I do a 'return 0' so the fact that I'm
getting a return code of 134 suggests this is
happening after main().
…On Wed, May 3, 2023 at 4:47 PM Jeroen Vermeulen ***@***.***> wrote:
Unfortunately I couldn't bring up a docker container for Debian's Stable
or Unstable with the right packages — a package failed to install. So can't
try those right now.
—
Reply to this email directly, view it on GitHub
<#681 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/A7SSVMWN2X4C7Z2QVBEWAGDXEK75RANCNFSM6AAAAAAXUKQLM4>
.
You are receiving this because you authored the thread.Message ID:
***@***.***>
--
Regards,
Dave
|
Thanks. That should help me reproduce the problem once I get a working Debian image with the right versions. There's one more thing that might be worth checking: whether this message also occurs when you include libpq's header |
About the 134... Are you saying that the program's return code was 134? From what you said earlier I got the impression that the return code was 0. I think on Linux systems the higher return codes are normally used when the program terminates because of a signal. |
Correct: my return code is 0 but when the program exits I do an 'echo $?'
and it's telling me 134 - strange no?
here's my code:
#include <iostream>
#include <pqxx/pqxx>
using std::cout, std::endl;
int main(int argc, char *argv[])
{
cout << "Help!" << endl;
return 0;
}
then I compiled it to a binary called ttt (no rhyme no reason for the name)
and I execute as follows:
# ./ttt
Help!
free(): double free detected in tcache 2
Aborted
# echo $?
134
If you need more please let me know...
Thanks,
Dave
…On Wed, May 3, 2023 at 5:20 PM Jeroen Vermeulen ***@***.***> wrote:
About the 134... Are you saying that the program's return code was 134?
From what you said earlier I got the impression that the return code was 0.
I think on Linux systems the higher return codes are normally used when
the program terminates because of a signal.
—
Reply to this email directly, view it on GitHub
<#681 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/A7SSVMQS6EIDGHSUIPOWR6DXELD2NANCNFSM6AAAAAAXUKQLM4>
.
You are receiving this because you authored the thread.Message ID:
***@***.***>
--
Regards,
Dave
|
Ah, this is a terminology problem. Your program's return code then is 134. It's completely usual for What's annoying about this is that when I tried to reproduce the problem before, I would definitely have noticed if the return code was non-zero. So we're back to me being unable to reproduce the problem. |
Apologies for the confusion. What I should have said is my source code is
"trying" to return zero but
the program is actually returning 134. The thing is, when I remove the
#include <pqxx/pqxx> the
problem goes away. Could there be a macro being executed inside the
header? Or perhaps
a macro in a header that pqxx is picking up from postgres? Very strange
problem.
Appreciate you looking at this...
Dave
…On Wed, May 3, 2023 at 5:54 PM Jeroen Vermeulen ***@***.***> wrote:
Ah, this is a terminology problem.
*Your program's return code* then is 134. It's completely usual for main()
to return 0, but that's not always what ends up determining the return code.
What's annoying about this is that when I tried to reproduce the problem
before, I would definitely have noticed if the return code was non-zero. So
we're back to me being unable to reproduce the problem.
—
Reply to this email directly, view it on GitHub
<#681 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/A7SSVMWFHGL6PA6QCJZ5VHDXELHZNANCNFSM6AAAAAAXUKQLM4>
.
You are receiving this because you authored the thread.Message ID:
***@***.***>
--
Regards,
Dave
|
Just a bit more info - and again, this does not happen when I remove the
header <pqxx/pqxx>:
Bare-bones main():
#include <iostream>
#include <pqxx/pqxx>
using std::cout, std::endl, std::string;
int main(int argc, char *argv[])
{
std::cout << "Help!" << std::endl;
return 0;
}
compiled with:
g++ -std=c++17 -g -o ttt ttt.cc -lpqxx -lpq NOTE: Won't compile unless I
link with the pqxx and pq libraries
inside GDB:
Reading symbols from ttt...
(gdb) l
1 #include <iostream>
2 #include <pqxx/pqxx>
3
4 using std::cout, std::endl, std::string;
5
6 int main(int argc, char *argv[])
7 {
8 std::cout << "Help!" << std::endl;
9 return 0;
10 }
(gdb) b 9
Breakpoint 1 at 0x4025bc: file ttt.cc, line 9.
(gdb) r
Starting program: /home/odysseus/code/C++/ttt
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
Help!
Breakpoint 1, main (argc=1, argv=0x7fffffffdff8) at ttt.cc:9
9 return 0;
(gdb) n
10 }
(gdb) n
0x00007ffff798509b in __libc_start_main () from
/lib/x86_64-linux-gnu/libc.so.6
(gdb) n
Single stepping until exit from function __libc_start_main,
which has no line number information.
free(): double free detected in tcache 2
Program received signal SIGABRT, Aborted.
0x00007ffff79988eb in raise () from /lib/x86_64-linux-gnu/libc.so.6
On Wed, May 3, 2023 at 6:23 PM David Richards ***@***.***>
wrote:
… Apologies for the confusion. What I should have said is my source code is
"trying" to return zero but
the program is actually returning 134. The thing is, when I remove the
#include <pqxx/pqxx> the
problem goes away. Could there be a macro being executed inside the
header? Or perhaps
a macro in a header that pqxx is picking up from postgres? Very strange
problem.
Appreciate you looking at this...
Dave
On Wed, May 3, 2023 at 5:54 PM Jeroen Vermeulen ***@***.***>
wrote:
> Ah, this is a terminology problem.
>
> *Your program's return code* then is 134. It's completely usual for
> main() to return 0, but that's not always what ends up determining the
> return code.
>
> What's annoying about this is that when I tried to reproduce the problem
> before, I would definitely have noticed if the return code was non-zero. So
> we're back to me being unable to reproduce the problem.
>
> —
> Reply to this email directly, view it on GitHub
> <#681 (comment)>, or
> unsubscribe
> <https://github.com/notifications/unsubscribe-auth/A7SSVMWFHGL6PA6QCJZ5VHDXELHZNANCNFSM6AAAAAAXUKQLM4>
> .
> You are receiving this because you authored the thread.Message ID:
> ***@***.***>
>
--
Regards,
Dave
--
Regards,
Dave
|
This appears to be the same problem. Would building pqxx again with |
Not sure what you mean by building with libstdc++ - I am building with
libstdc++ - it's a default link with g++
…On Thu, May 4, 2023 at 6:43 PM tt4g ***@***.***> wrote:
This appears to be the same problem.
https://stackoverflow.com/questions/31200522/double-free-or-corruption-when-using-old-glibc-and-libstdc-library-versions
Would building pqxx again with libstdc++ work around the problem?
—
Reply to this email directly, view it on GitHub
<#681 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/A7SSVMWTCFNKOSXHEGJTQO3XEQWHJANCNFSM6AAAAAAXUKQLM4>
.
You are receiving this because you authored the thread.Message ID:
***@***.***>
--
Regards,
Dave
|
It is not important to build with libstdc++. |
I did read it - but I am not getting a core dump and I have core dumps
enabled. Are you saying that libpqxx does not build with stdc++ ???
…On Fri, May 5, 2023 at 8:24 AM tt4g ***@***.***> wrote:
It is not important to build with libstdc++.
I wanted to tell you that there is a known problem with core dumps caused
by different versions of libstdc++ that the library and the application are
linked to.
Please read the Stack Overflow question.
—
Reply to this email directly, view it on GitHub
<#681 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/A7SSVMWKQXCKC7FAUJN7FNDXETWPRANCNFSM6AAAAAAXUKQLM4>
.
You are receiving this because you authored the thread.Message ID:
***@***.***>
--
Regards,
Dave
|
I suspect that it is undefined behavior because of the different libstdc++ versions. |
I have g++ 12.2 on my system (the latest is 13.1 which I have not built).
It's a very recent version of g++ (released last summer).
…On Fri, May 5, 2023 at 9:05 AM tt4g ***@***.***> wrote:
I suspect that it is undefined behavior because of the different libstdc++
versions.
I wanted you to try to see if rebuilding libpqxx with the current
libstdc++ would work around the problem.
—
Reply to this email directly, view it on GitHub
<#681 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/A7SSVMQBUGXM2FF6C3LQ6LTXET3K7ANCNFSM6AAAAAAXUKQLM4>
.
You are receiving this because you authored the thread.Message ID:
***@***.***>
--
Regards,
Dave
|
So it's possible that there's a bug there. But complacency is death, so I'm still eager to figure this one out. @dnewtonrichards another debugging question... what happens if you include |
Well, that's certainly a possibility - there's no such thing as bugless
software. If you need anything else from me please let me know...
Thanks,
Dave
…On Fri, May 5, 2023 at 9:28 AM Jeroen Vermeulen ***@***.***> wrote:
So it's possible that there's a bug there. But complacency is death, so
I'm still eager to figure this one out.
@dnewtonrichards <https://github.com/dnewtonrichards> another debugging
question... what happens if you include <pqxx/pqxx> but *don't link* to
either libpqxx or libpq?
—
Reply to this email directly, view it on GitHub
<#681 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/A7SSVMWHFECTR25YKQ66WFLXET6BRANCNFSM6AAAAAAXUKQLM4>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
--
Regards,
Dave
|
AAAAAaaaaaighh! I have reproduced the problem. It only happens when linking dynamically. On non-Windows systems I don't recommend using libpqxx as a shared library in any case; C++ ABIs are just too detailed to make backward binary compatibility very useful. So as a workaround, I would suggest: get rid of any (I do have regular automated tests set up on Debian unstable, using libpqxx as a shared library — but at the moment that build uses clang, not gcc! And the problem happens with gcc only. ☹) |
SUCCESS!!! You did it - many thanks for all of your help!!! Perhaps I
should install clang? Anyway, I will go with a static build...
--Dave
…On Fri, May 5, 2023 at 11:14 AM Jeroen Vermeulen ***@***.***> wrote:
*AAAAAaaaaaighh!*
I have reproduced the problem.
It only happens when linking dynamically.
On non-Windows systems I don't recommend using libpqxx as a shared library
in any case; C++ ABIs are just too detailed to make backward binary
compatibility very useful.
So as a workaround, I would suggest: get rid of any .so versions of
libpqxx, do a make clean to get rid of any hidden shared library build
inside the build directory, then configure libpqxx with --disable-shared,
re-build, and re-install.
(I do have regular automated tests set up on Debian unstable, using
libpqxx as a shared library — but at the moment that build uses clang, not
gcc! And the problem happens with gcc only. ☹)
—
Reply to this email directly, view it on GitHub
<#681 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/A7SSVMRBJSKGPV7YEW6I4P3XEUKN5ANCNFSM6AAAAAAXUKQLM4>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
--
Regards,
Dave
|
When I do the broken build (gcc 12 with
Of course a leak is kind of the opposite of a double-free. But again this problem happens with the shared library, not with the static one, which makes me think it's related. I think the search is on for suspicious handling of a |
Found it! Turns out So there's something in PQXX_DECLARE_ENUM_CONVERSION(pqxx::internal::encoding_group); The offending part of template<> inline std::string const type_name<ENUM> \
{ \
# ENUM \
} \ (The If I remove the definition's |
Fixes #681. WHen building libpqxx as a shared library on Debian with gcc 12, any program that included libpqxx headers (with a few exceptions) would crash after `main()` returned. There was an error message: _free(): double free detected in tcache 2_ The problem turned out to be triggered by the inlining of any specialisation of `pqxx::type_name`, as we did in the macro `PQXX_DECLARE_ENUM_CONVERSION`. There's a header that uses that macro, thus triggering the problem.
No need to change compilers for this... Don't get me wrong, clang is an excellent compiler. But one of those tricks to producing top-quality code is to run it through as many compilers as you can, and pay attention to the errors or warnings they produce. So... do both. :-) |
Heh. Case in point: clang doesn't like the change I made! Making the variable non-inline seems to mean that every translation unit that contains the definition gets its own definition, and for some reason they clash. Something like that. It's a bit awkward to debug on my phone. I think I'll have to see about making it a |
Fixes #681. When building libpqxx as a shared library with gcc 12, any program that included libpqxx headers (with a few exceptions) would crash after `main()` returned. There was an error message: _free(): double free detected in tcache 2_ The problem turned out to be triggered by the inlining of any specialisation of `pqxx::type_name`, as we did in the macro `PQXX_DECLARE_ENUM_CONVERSION`. There's a header that uses that macro, thus triggering the problem. I had to make enum `type_name` a `std::string_view`. This pains me. It changes the type of the variable. But gcc 12 (in a shared-library build) won't let me get away with an `inline std::string` and clang won't let me get away with a none-`inline` `std::string`. A `std::string_view` is easier — trivial destructor, no allocations.
A very timely fix — Ubuntu 23.04 also ships with gcc 12 and has the same problem. Thanks for the report @dnewtonrichards, and your patience in helping me resolve it. |
Just an FYI: downloaded the latest and greatest; configured with
'--enable-shared'; recompiled my code and voila - perfection!
Thank you!
--Dave
…On Fri, May 5, 2023 at 9:20 PM Jeroen Vermeulen ***@***.***> wrote:
Avery timely fix — Ubuntu 23.04 also ships with gcc 12 and has the same
problem.
Thanks for the report @dnewtonrichards
<https://github.com/dnewtonrichards>, and your patience in helping me
resolve it.
—
Reply to this email directly, view it on GitHub
<#681 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/A7SSVMUXJE2ZIPQDXZLBSS3XEWRM5ANCNFSM6AAAAAAXUKQLM4>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
--
Regards,
Dave
|
System: Debian 10 (Buster) X86-64 bit system
libpqxx built with gcc/g++ version 12.2
Postgres version 15.2
The following code produces this message: 'free(): double free detected in tcache 2'
The text was updated successfully, but these errors were encountered: