pgsql-io / multicorn2

http://multicorn2.org
Other
73 stars 16 forks source link

Stack overflow segfault when running postgres with --no-locale option #19

Closed fluffynukeit closed 9 months ago

fluffynukeit commented 1 year ago

Hi, thank you for your work in continuing multicorn. I'm making this issue to report a problem I saw and debugged when running with the --no-locale option to postgres. What I saw was a segmentation fault in multicorn within libpython when I returned a python integer in my execute() function, but no seg fault when I returned a python string.

    def execute(self, quals, columns):
        # Returning an integer value causes stack overflow when postgres is started with the
        # no-locale option.  This might possibly affect other py objects as well.
        line = [10] # Causes segfault
        #line = ['10'] # This does not result in a segfault
        yield line

I connected gdb and ran a BT and saw this. Note that the weird hash looking characters are because I used nix to build postgres and multicorn. (Additional recursive cycles omitted)

   from /nix/store/idjzlpbhd1pak8zvi4r8fmk9ksfm0nj6-postgresql-and-plugins-13.9/lib/multicorn.so
#130715 0x00007f207766648e in errorCheck ()
   from /nix/store/idjzlpbhd1pak8zvi4r8fmk9ksfm0nj6-postgresql-and-plugins-13.9/lib/multicorn.so
#130716 0x00007f2077666612 in PyString_AsString ()
   from /nix/store/idjzlpbhd1pak8zvi4r8fmk9ksfm0nj6-postgresql-and-plugins-13.9/lib/multicorn.so
#130717 0x00007f2077666254 in reportException ()
   from /nix/store/idjzlpbhd1pak8zvi4r8fmk9ksfm0nj6-postgresql-and-plugins-13.9/lib/multicorn.so
#130718 0x00007f207766648e in errorCheck ()
   from /nix/store/idjzlpbhd1pak8zvi4r8fmk9ksfm0nj6-postgresql-and-plugins-13.9/lib/multicorn.so
#130719 0x00007f2077666612 in PyString_AsString ()
   from /nix/store/idjzlpbhd1pak8zvi4r8fmk9ksfm0nj6-postgresql-and-plugins-13.9/lib/multicorn.so
#130720 0x00007f2077666254 in reportException ()
   from /nix/store/idjzlpbhd1pak8zvi4r8fmk9ksfm0nj6-postgresql-and-plugins-13.9/lib/multicorn.so
#130721 0x00007f207766648e in errorCheck ()
   from /nix/store/idjzlpbhd1pak8zvi4r8fmk9ksfm0nj6-postgresql-and-plugins-13.9/lib/multicorn.so
#130722 0x00007f20776666c1 in PyString_AsStringAndSize ()
   from /nix/store/idjzlpbhd1pak8zvi4r8fmk9ksfm0nj6-postgresql-and-plugins-13.9/lib/multicorn.so
#130723 0x00007f2077667abd in pynumberToCString ()
   from /nix/store/idjzlpbhd1pak8zvi4r8fmk9ksfm0nj6-postgresql-and-plugins-13.9/lib/multicorn.so
#130724 0x00007f2077668131 in pyobjectToDatum ()
   from /nix/store/idjzlpbhd1pak8zvi4r8fmk9ksfm0nj6-postgresql-and-plugins-13.9/lib/multicorn.so
#130725 0x00007f20776683bd in pythonSequenceToTuple ()
   from /nix/store/idjzlpbhd1pak8zvi4r8fmk9ksfm0nj6-postgresql-and-plugins-13.9/lib/multicorn.so
#130726 0x00007f207766b53a in multicornIterateForeignScan ()
   from /nix/store/idjzlpbhd1pak8zvi4r8fmk9ksfm0nj6-postgresql-and-plugins-13.9/lib/multicorn.so
#130727 0x0000561bc6b56b0b in ForeignNext ()
#130728 0x0000561bc6b3f48a in standard_ExecutorRun ()
#130729 0x0000561bc6c9a70c in PortalRunSelect ()
#130730 0x0000561bc6c9bad6 in PortalRun ()
#130731 0x0000561bc6c97831 in exec_simple_query ()
#130732 0x0000561bc6c998ea in PostgresMain ()
#130733 0x0000561bc6c2418e in ServerLoop ()
#130734 0x0000561bc6c250d5 in PostmasterMain ()
#130735 0x0000561bc69c4e81 in main ()

So there is a loop between PyString_AsString and reportException. I guessed the encoding name was the culprit based on the code, and tried running without the --no-locale option, and the problem went away. I originally tried the --no-locale option because I was having trouble starting postgres within nix (a common issue), but I had no issues removing the option, so I must have solved my difficulties some other way. I hope this helps you or someone else who might run into this problem. TYVM.