Skip to content

Commit

Permalink
Remove all unsafe FFI imports
Browse files Browse the repository at this point in the history
See #19

libpq can callback into Haskell, or do IO, so it's safer to just not use unsafe.
The overhead should be minimal.
  • Loading branch information
phadej committed Jul 29, 2023
1 parent 5a7d295 commit 4e63251
Showing 1 changed file with 50 additions and 50 deletions.
100 changes: 50 additions & 50 deletions src/Database/PostgreSQL/LibPQ/FFI.hs
Original file line number Diff line number Diff line change
Expand Up @@ -35,53 +35,53 @@ foreign import capi "hs-libpq.h PQconnectStart"
foreign import capi "hs-libpq.h PQconnectPoll"
c_PQconnectPoll :: Ptr PGconn -> IO CInt

foreign import capi unsafe "hs-libpq.h PQdb"
foreign import capi "hs-libpq.h PQdb"
c_PQdb :: Ptr PGconn -> IO CString

foreign import capi unsafe "hs-libpq.h PQuser"
foreign import capi "hs-libpq.h PQuser"
c_PQuser :: Ptr PGconn -> IO CString

foreign import capi unsafe "hs-libpq.h PQpass"
foreign import capi "hs-libpq.h PQpass"
c_PQpass :: Ptr PGconn -> IO CString

foreign import capi unsafe "hs-libpq.h PQhost"
foreign import capi "hs-libpq.h PQhost"
c_PQhost :: Ptr PGconn -> IO CString

foreign import capi unsafe "hs-libpq.h PQport"
foreign import capi "hs-libpq.h PQport"
c_PQport :: Ptr PGconn -> IO CString

foreign import capi unsafe "hs-libpq.h PQoptions"
foreign import capi "hs-libpq.h PQoptions"
c_PQoptions :: Ptr PGconn -> IO CString

foreign import capi unsafe "hs-libpq.h PQbackendPID"
foreign import capi "hs-libpq.h PQbackendPID"
c_PQbackendPID :: Ptr PGconn -> IO CInt

foreign import capi unsafe "hs-libpq.h PQconnectionNeedsPassword"
foreign import capi "hs-libpq.h PQconnectionNeedsPassword"
c_PQconnectionNeedsPassword :: Ptr PGconn -> IO CInt

foreign import capi unsafe "hs-libpq.h PQconnectionUsedPassword"
foreign import capi "hs-libpq.h PQconnectionUsedPassword"
c_PQconnectionUsedPassword :: Ptr PGconn -> IO CInt

foreign import capi unsafe "hs-libpq.h PQstatus"
foreign import capi "hs-libpq.h PQstatus"
c_PQstatus :: Ptr PGconn -> IO CInt

foreign import capi unsafe "hs-libpq.h PQtransactionStatus"
foreign import capi "hs-libpq.h PQtransactionStatus"
c_PQtransactionStatus :: Ptr PGconn -> IO CInt

-- TODO: GHC #22043
foreign import ccall "hs-libpq.h PQparameterStatus"
c_PQparameterStatus :: Ptr PGconn -> CString -> IO CString

foreign import capi unsafe "hs-libpq.h PQprotocolVersion"
foreign import capi "hs-libpq.h PQprotocolVersion"
c_PQprotocolVersion :: Ptr PGconn -> IO CInt

foreign import capi unsafe "hs-libpq.h PQserverVersion"
foreign import capi "hs-libpq.h PQserverVersion"
c_PQserverVersion :: Ptr PGconn -> IO CInt

foreign import capi unsafe "hs-libpq.h PQlibVersion"
foreign import capi "hs-libpq.h PQlibVersion"
c_PQlibVersion :: IO CInt

foreign import capi unsafe "hs-libpq.h PQsocket"
foreign import capi "hs-libpq.h PQsocket"
c_PQsocket :: Ptr PGconn -> IO CInt

foreign import capi "hs-libpq.h PQerrorMessage"
Expand All @@ -99,7 +99,7 @@ foreign import capi "hs-libpq.h PQresetStart"
foreign import capi "hs-libpq.h PQresetPoll"
c_PQresetPoll :: Ptr PGconn -> IO CInt

foreign import capi unsafe "hs-libpq.h PQclientEncoding"
foreign import capi "hs-libpq.h PQclientEncoding"
c_PQclientEncoding :: Ptr PGconn -> IO CInt

-- TODO: GHC #22043
Expand All @@ -110,7 +110,7 @@ foreign import capi "hs-libpq.h PQsetClientEncoding"
c_PQsetClientEncoding :: Ptr PGconn -> CString -> IO CInt

type PGVerbosity = CInt
foreign import capi unsafe "hs-libpq.h PQsetErrorVerbosity"
foreign import capi "hs-libpq.h PQsetErrorVerbosity"
c_PQsetErrorVerbosity :: Ptr PGconn -> PGVerbosity -> IO PGVerbosity

foreign import capi "hs-libpq.h PQputCopyData"
Expand Down Expand Up @@ -159,22 +159,22 @@ foreign import capi "hs-libpq.h &PQfreeCancel"
foreign import capi "hs-libpq.h PQcancel"
c_PQcancel :: Ptr PGcancel -> CString -> CInt -> IO CInt

foreign import capi unsafe "hs-libpq.h PQnotifies"
foreign import capi "hs-libpq.h PQnotifies"
c_PQnotifies :: Ptr PGconn -> IO (Ptr Notify)

foreign import capi "hs-libpq.h PQconsumeInput"
c_PQconsumeInput :: Ptr PGconn -> IO CInt

foreign import capi unsafe "hs-libpq.h PQisBusy"
foreign import capi "hs-libpq.h PQisBusy"
c_PQisBusy :: Ptr PGconn -> IO CInt

foreign import capi "hs-libpq.h PQsetnonblocking"
c_PQsetnonblocking :: Ptr PGconn -> CInt -> IO CInt

foreign import capi unsafe "hs-libpq.h PQisnonblocking"
foreign import capi "hs-libpq.h PQisnonblocking"
c_PQisnonblocking :: Ptr PGconn -> IO CInt

foreign import capi unsafe "hs-libpq.h PQsetSingleRowMode"
foreign import capi "hs-libpq.h PQsetSingleRowMode"
c_PQsetSingleRowMode :: Ptr PGconn -> IO CInt

foreign import capi "hs-libpq.h PQgetResult"
Expand Down Expand Up @@ -207,67 +207,67 @@ foreign import capi "hs-libpq.h PQdescribePortal"
foreign import capi "hs-libpq.h &PQclear"
p_PQclear :: FunPtr (Ptr PGresult -> IO ())

foreign import capi unsafe "hs-libpq.h PQresultStatus"
foreign import capi "hs-libpq.h PQresultStatus"
c_PQresultStatus :: Ptr PGresult -> IO CInt

foreign import capi unsafe "hs-libpq.h PQresStatus"
foreign import capi "hs-libpq.h PQresStatus"
c_PQresStatus :: CInt -> IO CString

foreign import capi unsafe "hs-libpq.h PQresultErrorMessage"
foreign import capi "hs-libpq.h PQresultErrorMessage"
c_PQresultErrorMessage :: Ptr PGresult -> IO CString

foreign import capi "hs-libpq.h PQresultErrorField"
c_PQresultErrorField :: Ptr PGresult -> CInt -> IO CString

foreign import capi unsafe "hs-libpq.h PQntuples"
foreign import capi "hs-libpq.h PQntuples"
c_PQntuples :: Ptr PGresult -> CInt

foreign import capi unsafe "hs-libpq.h PQnfields"
foreign import capi "hs-libpq.h PQnfields"
c_PQnfields :: Ptr PGresult -> CInt

foreign import capi unsafe "hs-libpq.h PQfname"
foreign import capi "hs-libpq.h PQfname"
c_PQfname :: Ptr PGresult -> CInt -> IO CString

foreign import capi unsafe "hs-libpq.h PQfnumber"
foreign import capi "hs-libpq.h PQfnumber"
c_PQfnumber :: Ptr PGresult -> CString -> IO CInt

foreign import capi unsafe "hs-libpq.h PQftable"
foreign import capi "hs-libpq.h PQftable"
c_PQftable :: Ptr PGresult -> CInt -> IO Oid

foreign import capi unsafe "hs-libpq.h PQftablecol"
foreign import capi "hs-libpq.h PQftablecol"
c_PQftablecol :: Ptr PGresult -> CInt -> IO CInt

foreign import capi unsafe "hs-libpq.h PQfformat"
foreign import capi "hs-libpq.h PQfformat"
c_PQfformat :: Ptr PGresult -> CInt -> IO CInt

foreign import capi unsafe "hs-libpq.h PQftype"
foreign import capi "hs-libpq.h PQftype"
c_PQftype :: Ptr PGresult -> CInt -> IO Oid

foreign import capi unsafe "hs-libpq.h PQfmod"
foreign import capi "hs-libpq.h PQfmod"
c_PQfmod :: Ptr PGresult -> CInt -> IO CInt

foreign import capi unsafe "hs-libpq.h PQfsize"
foreign import capi "hs-libpq.h PQfsize"
c_PQfsize :: Ptr PGresult -> CInt -> IO CInt

foreign import capi unsafe "hs-libpq.h PQgetvalue"
foreign import capi "hs-libpq.h PQgetvalue"
c_PQgetvalue :: Ptr PGresult -> CInt -> CInt -> IO CString

foreign import capi unsafe "hs-libpq.h PQgetisnull"
foreign import capi "hs-libpq.h PQgetisnull"
c_PQgetisnull :: Ptr PGresult -> CInt -> CInt -> IO CInt

foreign import capi unsafe "hs-libpq.h PQgetlength"
foreign import capi "hs-libpq.h PQgetlength"
c_PQgetlength :: Ptr PGresult -> CInt -> CInt -> IO CInt

foreign import capi unsafe "hs-libpq.h PQnparams"
foreign import capi "hs-libpq.h PQnparams"
c_PQnparams :: Ptr PGresult -> IO CInt

foreign import capi unsafe "hs-libpq.h PQparamtype"
foreign import capi "hs-libpq.h PQparamtype"
c_PQparamtype :: Ptr PGresult -> CInt -> IO Oid

foreign import capi unsafe "hs-libpq.h PQcmdStatus"
foreign import capi "hs-libpq.h PQcmdStatus"
c_PQcmdStatus :: Ptr PGresult -> IO CString

foreign import capi unsafe "hs-libpq.h PQcmdTuples"
foreign import capi "hs-libpq.h PQcmdTuples"
c_PQcmdTuples :: Ptr PGresult -> IO CString

foreign import capi "hs-libpq.h PQescapeStringConn"
Expand All @@ -290,38 +290,38 @@ foreign import capi "hs-libpq.h PQunescapeBytea"
-> Ptr CSize
-> IO (Ptr Word8) -- Actually (IO (Ptr CUChar))

foreign import capi unsafe "hs-libpq.h PQescapeIdentifier"
foreign import capi "hs-libpq.h PQescapeIdentifier"
c_PQescapeIdentifier :: Ptr PGconn
-> CString
-> CSize
-> IO CString

foreign import capi unsafe "hs-libpq.h &PQfreemem"
foreign import capi "hs-libpq.h &PQfreemem"
p_PQfreemem :: FunPtr (Ptr a -> IO ())

foreign import capi unsafe "hs-libpq.h PQfreemem"
foreign import capi "hs-libpq.h PQfreemem"
c_PQfreemem :: Ptr a -> IO ()

-------------------------------------------------------------------------------
-- FFI imports: noticebuffers
-------------------------------------------------------------------------------

foreign import capi unsafe "hs-libpq.h hs_postgresql_libpq_malloc_noticebuffer"
foreign import capi "hs-libpq.h hs_postgresql_libpq_malloc_noticebuffer"
c_malloc_noticebuffer :: IO (Ptr CNoticeBuffer)

foreign import capi unsafe "hs-libpq.h hs_postgresql_libpq_free_noticebuffer"
foreign import capi "hs-libpq.h hs_postgresql_libpq_free_noticebuffer"
c_free_noticebuffer :: Ptr CNoticeBuffer -> IO ()

foreign import capi unsafe "hs-libpq.h hs_postgresql_libpq_get_notice"
foreign import capi "hs-libpq.h hs_postgresql_libpq_get_notice"
c_get_notice :: Ptr CNoticeBuffer -> IO (Ptr PGnotice)

foreign import capi unsafe "hs-libpq.h &hs_postgresql_libpq_discard_notices"
foreign import capi "hs-libpq.h &hs_postgresql_libpq_discard_notices"
p_discard_notices :: FunPtr NoticeReceiver

foreign import capi unsafe "hs-libpq.h &hs_postgresql_libpq_store_notices"
foreign import capi "hs-libpq.h &hs_postgresql_libpq_store_notices"
p_store_notices :: FunPtr NoticeReceiver

foreign import capi unsafe "hs-libpq.h PQsetNoticeReceiver"
foreign import capi "hs-libpq.h PQsetNoticeReceiver"
c_PQsetNoticeReceiver :: Ptr PGconn -> FunPtr NoticeReceiver -> Ptr CNoticeBuffer -> IO (FunPtr NoticeReceiver)

-------------------------------------------------------------------------------
Expand Down

0 comments on commit 4e63251

Please sign in to comment.