Have written sqlite3 extension in cpp trying to load it from go lang,it works properly but it does not clean exist it gives exception.
CPP code
#include <regex>
#include <string>
#include <iostream>
#include "sqlite3ext.h"
SQLITE_EXTENSION_INIT1;
static void regex_match_func(sqlite3_context *context, int argc, sqlite3_value **argv) {
//sqlite3_result_text(context, "", -1, SQLITE_TRANSIENT);
if (argc >= 3) {
const char *target = (const char *)(sqlite3_value_text(argv[0]));
const char *pattern = (const char *)(sqlite3_value_text(argv[1]));
const int *x = (const int *)(sqlite3_value_text(argv[2]));
// Ensure target and pattern are not NULL
if (target==nullptr || pattern==nullptr) {
sqlite3_result_error(context, "Invalid NULL argument", -1);
return;
}
try {
std::regex re(pattern);
if (std::regex_match(target, re)) {
sqlite3_result_text(context, target, -1, SQLITE_TRANSIENT);
}else{
sqlite3_result_text(context, "", -1, SQLITE_TRANSIENT);
return;
}
} catch (const std::regex_error& e) {
sqlite3_result_error(context, e.what(), -1);
return;
}
} else {
sqlite3_result_error(context, "Two arguments required: pattern and target", -1);
return;
}
}
#ifdef _WIN32
# define EXPORT __declspec(dllexport)
#endif
//entry point
extern "C" {
EXPORT int sqlite3_extension_init(sqlite3 *db, char **errmsg, const sqlite3_api_routines *api) {
std::cout << "in sqlite3_extension_initn";
SQLITE_EXTENSION_INIT2(api);
return sqlite3_create_function(db, "regex_match", 3, SQLITE_UTF8, NULL, regex_match_func, NULL, NULL);
}
}
command used to build shared dll
g++ -shared -o sqlite3_regex_match.dll sqlite3_regex_match.cpp
Go lang code
package main
import (
"database/sql"
"fmt"
"github.com/mattn/go-sqlite3"
"log"
)
func main() {
sql.Register("sqlite3_with_extensions",
&sqlite3.SQLiteDriver{
Extensions: []string{
"sqlite3_regex_match",
},
})
db, err := sql.Open("sqlite3_with_extensions", ":memory:")
if err != nil {
log.Fatal(err)
}
defer db.Close()
// Force db to make a new connection in pool
// by putting the original in a transaction
tx, err := db.Begin()
if err != nil {
log.Fatal(err)
}
defer tx.Commit()
// New connection works (hopefully!)
rows, err := db.Query("select 'hello world' where regex_match( 'hello world','^hello.*d$'",0));
if err != nil {
log.Fatal(err)
}
defer rows.Close()
for rows.Next() {
var helloworld string
rows.Scan(&helloworld)
fmt.Println(helloworld)
}
}
Expected result – clean exit
current output
Exception 0xc0000005 0x8 0x7ffa905d4a00 0x7ffa905d4a00
PC=0x7ffa905d4a00
runtime: g 0: unknown pc 0x7ffa905d4a00
stack: frame={sp:0x13527ff7c8, fp:0x0} stack=[0x0,0x13527ffc30)
0x00000013527ff6c8: 0x0000000000000000 0x000022bbd4263e3c
0x00000013527ff6d8: 0x0000018c916e0000 0x00007ffaddaf0000
0x00000013527ff6e8: 0x000000007ffe0384 0x0000000000000000
0x00000013527ff6f8: 0x00007ffadea8688c 0x000022bbd4263e2c
0x00000013527ff708: 0x00007ffaddc7cfc0 0x00000013527ffbb0
0x00000013527ff718: 0x0000000000000001 0x00007ffaddaf0000
0x00000013527ff728: 0x0000000000000008 0x00007ffaddb31d10
0x00000013527ff738: 0x00007ffaddb65e3a 0x0000000000000000
0x00000013527ff748: 0x00007ffadea70000 0x00007ffaddbe04a0
0x00000013527ff758: 0x00007ffadc3e97b1 0x0000000000140012
0x00000013527ff768: 0x00007ffaddbe0468 0x000000007ffe0384
0x00000013527ff778: 0x00007ffaddb67432 0x00007ffa00000000
0x00000013527ff788: 0x00007ffaddb65e3a 0x0000000000190018
0x00000013527ff798: 0x00007ffaddbe04a0 0x0000000000000000
0x00000013527ff7a8: 0x00007ffaddb65e51 0x000000007ffe0384
0x00000013527ff7b8: 0x00007ffadead4910 0x00007ffa905d4a00
0x00000013527ff7c8: <0x00007ffaddeda73b 0x0000000000000000
0x00000013527ff7d8: 0x00007ffaddb64dd6 0x0000000000000000
0x00000013527ff7e8: 0x00007ffadc2b1000 0x0000018c91970760
0x00000013527ff7f8: 0x0000018c91970750 0x0000018c91970750
0x00000013527ff808: 0x0000018c91970768 0x0000000000000001
0x00000013527ff818: 0x00007ffaddea0000 0x0000000000000008
0x00000013527ff828: 0x00007ffaddea7850 0x000000007ffe0384
0x00000013527ff838: 0x00007ffaddea7938 0x0000000000000001
0x00000013527ff848: 0x0000000000000000 0x0000001300000001
0x00000013527ff858: 0x00007ffadea89aae 0x000000007ffe0385
0x00000013527ff868: 0x00007ffadea89a1d 0x000000007ffe0385
0x00000013527ff878: 0x0000000000000000 0x0000000000000001
0x00000013527ff888: 0x000000007ffe0385 0x0000000000000000
0x00000013527ff898: 0x00007ffadeacf341 0x000022bbd4263f00
0x00000013527ff8a8: 0x0000000000000014 0x00007ffaddea7850
0x00000013527ff8b8: 0x000000c000032000 0x0000000000000000
runtime: g 0: unknown pc 0x7ffa905d4a00
stack: frame={sp:0x13527ff7c8, fp:0x0} stack=[0x0,0x13527ffc30)
0x00000013527ff6c8: 0x0000000000000000 0x000022bbd4263e3c
0x00000013527ff6d8: 0x0000018c916e0000 0x00007ffaddaf0000
0x00000013527ff6e8: 0x000000007ffe0384 0x0000000000000000
0x00000013527ff6f8: 0x00007ffadea8688c 0x000022bbd4263e2c
0x00000013527ff708: 0x00007ffaddc7cfc0 0x00000013527ffbb0
0x00000013527ff718: 0x0000000000000001 0x00007ffaddaf0000
0x00000013527ff728: 0x0000000000000008 0x00007ffaddb31d10
0x00000013527ff738: 0x00007ffaddb65e3a 0x0000000000000000
0x00000013527ff748: 0x00007ffadea70000 0x00007ffaddbe04a0
0x00000013527ff758: 0x00007ffadc3e97b1 0x0000000000140012
0x00000013527ff768: 0x00007ffaddbe0468 0x000000007ffe0384
0x00000013527ff778: 0x00007ffaddb67432 0x00007ffa00000000
0x00000013527ff788: 0x00007ffaddb65e3a 0x0000000000190018
0x00000013527ff798: 0x00007ffaddbe04a0 0x0000000000000000
0x00000013527ff7a8: 0x00007ffaddb65e51 0x000000007ffe0384
0x00000013527ff7b8: 0x00007ffadead4910 0x00007ffa905d4a00
0x00000013527ff7c8: <0x00007ffaddeda73b 0x0000000000000000
0x00000013527ff7d8: 0x00007ffaddb64dd6 0x0000000000000000
0x00000013527ff7e8: 0x00007ffadc2b1000 0x0000018c91970760
0x00000013527ff7f8: 0x0000018c91970750 0x0000018c91970750
0x00000013527ff808: 0x0000018c91970768 0x0000000000000001
0x00000013527ff818: 0x00007ffaddea0000 0x0000000000000008
0x00000013527ff828: 0x00007ffaddea7850 0x000000007ffe0384
0x00000013527ff838: 0x00007ffaddea7938 0x0000000000000001
0x00000013527ff848: 0x0000000000000000 0x0000001300000001
0x00000013527ff858: 0x00007ffadea89aae 0x000000007ffe0385
0x00000013527ff868: 0x00007ffadea89a1d 0x000000007ffe0385
0x00000013527ff878: 0x0000000000000000 0x0000000000000001
0x00000013527ff888: 0x000000007ffe0385 0x0000000000000000
0x00000013527ff898: 0x00007ffadeacf341 0x000022bbd4263f00
0x00000013527ff8a8: 0x0000000000000014 0x00007ffaddea7850
0x00000013527ff8b8: 0x000000c000032000 0x0000000000000000
goroutine 1 [running]:
runtime.systemstack_switch()
C:/Program Files/Go/src/runtime/asm_amd64.s:474 +0x8 fp=0xc00006dec0 sp=0xc00006deb0 pc=0x7ff797f15608
runtime.stdcall(0x7ff7980fb6c0?)
C:/Program Files/Go/src/runtime/os_windows.go:1048 +0x7b fp=0xc00006def8 sp=0xc00006dec0 pc=0x7ff797ee6a7b
runtime.stdcall1(0x7ff7980cd7b8, 0x0)
C:/Program Files/Go/src/runtime/os_windows.go:1069 +0x56 fp=0xc00006df10 sp=0xc00006def8 pc=0x7ff797ee6b56
runtime.exit(0x0?)
C:/Program Files/Go/src/runtime/os_windows.go:676 +0x45 fp=0xc00006df40 sp=0xc00006df10 pc=0x7ff797ee59a5
runtime.main()
C:/Program Files/Go/src/runtime/proc.go:291 +0x30d fp=0xc00006dfe0 sp=0xc00006df40 pc=0x7ff797eec0cd
runtime.goexit()
C:/Program Files/Go/src/runtime/asm_amd64.s:1650 +0x1 fp=0xc00006dfe8 sp=0xc00006dfe0 pc=0x7ff797f17641
goroutine 2 [force gc (idle)]:
runtime.gopark(0x0?, 0x0?, 0x0?, 0x0?, 0x0?)
C:/Program Files/Go/src/runtime/proc.go:398 +0xce fp=0xc000035fa8 sp=0xc000035f88 pc=0x7ff797eec48e
runtime.goparkunlock(...)
C:/Program Files/Go/src/runtime/proc.go:404
runtime.forcegchelper()
C:/Program Files/Go/src/runtime/proc.go:322 +0xb8 fp=0xc000035fe0 sp=0xc000035fa8 pc=0x7ff797eec318
runtime.goexit()
C:/Program Files/Go/src/runtime/asm_amd64.s:1650 +0x1 fp=0xc000035fe8 sp=0xc000035fe0 pc=0x7ff797f17641
created by runtime.init.6 in goroutine 1
C:/Program Files/Go/src/runtime/proc.go:310 +0x1a
goroutine 3 [GC sweep wait]:
runtime.gopark(0x0?, 0x0?, 0x0?, 0x0?, 0x0?)
C:/Program Files/Go/src/runtime/proc.go:398 +0xce fp=0xc000037f78 sp=0xc000037f58 pc=0x7ff797eec48e
runtime.goparkunlock(...)
C:/Program Files/Go/src/runtime/proc.go:404
runtime.bgsweep(0x0?)
C:/Program Files/Go/src/runtime/mgcsweep.go:280 +0x94 fp=0xc000037fc8 sp=0xc000037f78 pc=0x7ff797ed6e54
runtime.gcenable.func1()
C:/Program Files/Go/src/runtime/mgc.go:200 +0x25 fp=0xc000037fe0 sp=0xc000037fc8 pc=0x7ff797ecbfc5
runtime.goexit()
C:/Program Files/Go/src/runtime/asm_amd64.s:1650 +0x1 fp=0xc000037fe8 sp=0xc000037fe0 pc=0x7ff797f17641
created by runtime.gcenable in goroutine 1
C:/Program Files/Go/src/runtime/mgc.go:200 +0x66
goroutine 4 [GC scavenge wait]:
runtime.gopark(0xc00001a070?, 0x7ff79815b0b8?, 0x1?, 0x0?, 0xc000032b60?)
C:/Program Files/Go/src/runtime/proc.go:398 +0xce fp=0xc000047f70 sp=0xc000047f50 pc=0x7ff797eec48e
runtime.goparkunlock(...)
C:/Program Files/Go/src/runtime/proc.go:404
runtime.(*scavengerState).park(0x7ff7982628e0)
C:/Program Files/Go/src/runtime/mgcscavenge.go:425 +0x49 fp=0xc000047fa0 sp=0xc000047f70 pc=0x7ff797ed4709
runtime.bgscavenge(0x0?)
C:/Program Files/Go/src/runtime/mgcscavenge.go:653 +0x3c fp=0xc000047fc8 sp=0xc000047fa0 pc=0x7ff797ed4c9c
runtime.gcenable.func2()
C:/Program Files/Go/src/runtime/mgc.go:201 +0x25 fp=0xc000047fe0 sp=0xc000047fc8 pc=0x7ff797ecbf65
runtime.goexit()
C:/Program Files/Go/src/runtime/asm_amd64.s:1650 +0x1 fp=0xc000047fe8 sp=0xc000047fe0 pc=0x7ff797f17641
created by runtime.gcenable in goroutine 1
C:/Program Files/Go/src/runtime/mgc.go:201 +0xa5
goroutine 5 [finalizer wait]:
runtime.gopark(0x7ff7981231c0?, 0x197eed301?, 0x0?, 0x0?, 0x7ff797ef4805?)
C:/Program Files/Go/src/runtime/proc.go:398 +0xce fp=0xc000039e28 sp=0xc000039e08 pc=0x7ff797eec48e
runtime.runfinq()
C:/Program Files/Go/src/runtime/mfinal.go:193 +0x107 fp=0xc000039fe0 sp=0xc000039e28 pc=0x7ff797ecb087
runtime.goexit()
C:/Program Files/Go/src/runtime/asm_amd64.s:1650 +0x1 fp=0xc000039fe8 sp=0xc000039fe0 pc=0x7ff797f17641
created by runtime.createfing in goroutine 1
C:/Program Files/Go/src/runtime/mfinal.go:163 +0x3d
goroutine 6 [runnable]:
runtime.gopark(0xc000049f88?, 0x2?, 0x0?, 0x0?, 0xc000049f84?)
C:/Program Files/Go/src/runtime/proc.go:398 +0xce fp=0xc000049e30 sp=0xc000049e10 pc=0x7ff797eec48e
runtime.selectgo(0xc000049f88, 0xc000049f80, 0x0?, 0x0, 0x0?, 0x1)
C:/Program Files/Go/src/runtime/select.go:327 +0x725 fp=0xc000049f50 sp=0xc000049e30 pc=0x7ff797efad85
database/sql.(*DB).connectionOpener(0xc00001e340, {0x7ff79815d420, 0xc00004e050})
C:/Program Files/Go/src/database/sql/sql.go:1218 +0x87 fp=0xc000049fb8 sp=0xc000049f50 pc=0x7ff797f99407
database/sql.OpenDB.func1()
C:/Program Files/Go/src/database/sql/sql.go:791 +0x28 fp=0xc000049fe0 sp=0xc000049fb8 pc=0x7ff797f97828
runtime.goexit()
C:/Program Files/Go/src/runtime/asm_amd64.s:1650 +0x1 fp=0xc000049fe8 sp=0xc000049fe0 pc=0x7ff797f17641
created by database/sql.OpenDB in goroutine 1
C:/Program Files/Go/src/database/sql/sql.go:791 +0x165
goroutine 7 [runnable]:
runtime.gopark(0x7ff797f451e5?, 0x0?, 0x98?, 0x3f?, 0x7ff797f45159?)
C:/Program Files/Go/src/runtime/proc.go:398 +0xce fp=0xc000043f08 sp=0xc000043ee8 pc=0x7ff797eec48e
runtime.chanrecv(0xc000086060, 0x0, 0x1)
C:/Program Files/Go/src/runtime/chan.go:583 +0x3d0 fp=0xc000043f80 sp=0xc000043f08 pc=0x7ff797eba570
runtime.chanrecv1(0xc00004e0a0?, 0x0?)
C:/Program Files/Go/src/runtime/chan.go:442 +0x12 fp=0xc000043fa8 sp=0xc000043f80 pc=0x7ff797eba192
database/sql.(*Tx).awaitDone(0xc000074000)
C:/Program Files/Go/src/database/sql/sql.go:2174 +0x2b fp=0xc000043fc8 sp=0xc000043fa8 pc=0x7ff797f9e94b
database/sql.(*DB).beginDC.func2()
C:/Program Files/Go/src/database/sql/sql.go:1887 +0x25 fp=0xc000043fe0 sp=0xc000043fc8 pc=0x7ff797f9d985
runtime.goexit()
C:/Program Files/Go/src/runtime/asm_amd64.s:1650 +0x1 fp=0xc000043fe8 sp=0xc000043fe0 pc=0x7ff797f17641
created by database/sql.(*DB).beginDC in goroutine 1
C:/Program Files/Go/src/database/sql/sql.go:1887 +0x20d
rax 0x7ffa905d4a00
rbx 0x7ffa905d4a00
rcx 0x2c
rdi 0x18c91970760
rsi 0x18c91970750
rbp 0x13527ffbb0
rsp 0x13527ff7c8
r8 0x1
r9 0x1
r10 0x7ffe0384
r11 0x13527ff8b0
r12 0x18c91970750
r13 0x0
r14 0x1
r15 0x18c91970768
rip 0x7ffa905d4a00
rflags 0x10206
cs 0x33
fs 0x53
gs 0x2b
Tryout done already but any solution below did not worked
1.https://github.com/golang/go/issues/42593
2.https://github.com/golang/go/issues/41138
- also tried few solution from stack overflow but none worked
I know there are possible duplicates of it but I tired them none worked
can any one help me finding my mistake?