resolved the issue that go wrapper code stores the file name path in memory allocated with C.malloc
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
hyung-hwan 2024-02-24 17:32:42 +09:00
parent 44a6cda801
commit 06176f4900
2 changed files with 23 additions and 36 deletions

View File

@ -10,7 +10,6 @@ import "C"
import ( import (
"bufio" "bufio"
"fmt"
"io" "io"
"os" "os"
"path" "path"
@ -95,53 +94,48 @@ func hcl_go_cci_handler(c *C.hcl_t, cmd C.hcl_io_cmd_t, arg unsafe.Pointer) C.in
var ( var (
ioarg *C.hcl_io_cciarg_t ioarg *C.hcl_io_cciarg_t
name string name string
includer_name string
fd int fd int
tptr unsafe.Pointer tptr unsafe.Pointer
tlen C.size_t tlen C.size_t
) )
ioarg = (*C.hcl_io_cciarg_t)(arg) ioarg = (*C.hcl_io_cciarg_t)(arg)
if ioarg.name == nil { // main stream when it's not feed based.
name = ""
} else {
var k []rune = ucstr_to_rune_slice(ioarg.name)
name = string(k)
}
if ioarg.includer == nil /* || ioarg.includer.name == nil */ { if ioarg.includer == nil /* || ioarg.includer.name == nil */ {
includer_name = g.io.cci_main // main stream
name = "" name = g.io.cci_main
} else { } else {
// actual included stream
var includer_name string
var k []rune = ucstr_to_rune_slice(ioarg.name)
name = string(k)
//var k []rune = ucstr_to_rune_slice(ioarg.includer.name) //var k []rune = ucstr_to_rune_slice(ioarg.includer.name)
//var k []rune = ucstr_to_rune_slice(ioarg.includer.handle.remembered_path) //var k []rune = ucstr_to_rune_slice(ioarg.includer.handle.remembered_path)
tptr = ioarg.includer.handle tptr = ioarg.includer.handle
tlen = *(*C.size_t)(unsafe.Pointer(uintptr(tptr) + unsafe.Sizeof(fd))) tlen = *(*C.size_t)(unsafe.Pointer(uintptr(tptr) + unsafe.Sizeof(fd)))
includer_name = C.GoStringN((*C.char)(unsafe.Pointer(uintptr(tptr)+unsafe.Sizeof(fd)+unsafe.Sizeof(tlen))), C.int(tlen)) includer_name = C.GoStringN((*C.char)(unsafe.Pointer(uintptr(tptr)+unsafe.Sizeof(fd)+unsafe.Sizeof(tlen))), C.int(tlen))
fmt.Printf("xname.... [%s] ccimain [%s] [%s]\n", name, g.io.cci_main, includer_name)
name = filepath.Join(path.Dir(includer_name), name) name = filepath.Join(path.Dir(includer_name), name)
fmt.Printf("name.... [%s]\n", name)
} }
// len(name) is the number of bytes in the string tlen = C.size_t(len(name)) // number of bytes in the string
tlen = C.size_t(len(name))
tptr = C.malloc(C.size_t(unsafe.Sizeof(fd)) + C.size_t(unsafe.Sizeof(tlen)) + tlen) tptr = C.malloc(C.size_t(unsafe.Sizeof(fd)) + C.size_t(unsafe.Sizeof(tlen)) + tlen)
if tptr == nil { if tptr == nil {
g.set_errmsg(C.HCL_ESYSMEM, "memory allocation failure for cci name") g.set_errmsg(C.HCL_ESYSMEM, "memory allocation failure for cci name")
return -1 return -1
} }
if name != "" { if ioarg.includer == nil {
fd, err = g.io.cci.Open(g, name, "") fd = -1
} else {
fd, err = g.io.cci.Open(g, name)
if err != nil { if err != nil {
g.set_errmsg(C.HCL_EIOERR, err.Error()) g.set_errmsg(C.HCL_EIOERR, err.Error())
C.free(tptr) C.free(tptr)
return -1 return -1
} }
} else {
fd = -1
} }
C.memcpy(tptr, unsafe.Pointer(&fd), C.size_t(unsafe.Sizeof(fd))) C.memcpy(tptr, unsafe.Pointer(&fd), C.size_t(unsafe.Sizeof(fd)))
@ -335,7 +329,7 @@ type CciFileHandler struct {
g *HCL g *HCL
} }
func (p *CciFileHandler) Open(g *HCL, name string, includer_name string) (int, error) { func (p *CciFileHandler) Open(g *HCL, name string) (int, error) {
var ( var (
f *os.File f *os.File
r *bufio.Reader r *bufio.Reader
@ -346,14 +340,7 @@ func (p *CciFileHandler) Open(g *HCL, name string, includer_name string) (int, e
if name == "" { if name == "" {
f = os.Stdin f = os.Stdin
} else { } else {
var dir string f, err = os.Open(name)
dir = path.Dir(includer_name)
if dir != "/" && dir != "" {
dir = dir + "/"
}
f, err = os.Open(dir + name)
if err != nil { if err != nil {
return -1, err return -1, err
} }

View File

@ -5,17 +5,17 @@ package hcl
#include <hcl-utl.h> #include <hcl-utl.h>
#include <stdlib.h> // for C.free #include <stdlib.h> // for C.free
extern int hcl_go_cci_handler (hcl_t hcl, hcl_io_cmd_t cmd, void* arg); extern int hcl_go_cci_handler (hcl_t* hcl, hcl_io_cmd_t cmd, void* arg);
extern int hcl_go_udi_handler (hcl_t hcl, hcl_io_cmd_t cmd, void* arg); extern int hcl_go_udi_handler (hcl_t* hcl, hcl_io_cmd_t cmd, void* arg);
extern int hcl_go_udo_handler (hcl_t hcl, hcl_io_cmd_t cmd, void* arg); extern int hcl_go_udo_handler (hcl_t* hcl, hcl_io_cmd_t cmd, void* arg);
int hcl_cci_Handler_for_go (hcl_t hcl, hcl_io_cmd_t cmd, void* arg) { int hcl_cci_handler_for_go (hcl_t* hcl, hcl_io_cmd_t cmd, void* arg) {
return hcl_go_cci_handler(hcl, cmd, arg); return hcl_go_cci_handler(hcl, cmd, arg);
} }
int hcl_udi_handler_for_go (hcl_t hcl, hcl_io_cmd_t cmd, void* arg) { int hcl_udi_handler_for_go (hcl_t* hcl, hcl_io_cmd_t cmd, void* arg) {
return hcl_go_udi_handler(hcl, cmd, arg); return hcl_go_udi_handler(hcl, cmd, arg);
} }
int hcl_udo_handler_for_go (hcl_t hcl, hcl_io_cmd_t cmd, void* arg) { int hcl_udo_handler_for_go (hcl_t* hcl, hcl_io_cmd_t cmd, void* arg) {
return hcl_go_udo_handler(hcl, cmd, arg); return hcl_go_udo_handler(hcl, cmd, arg);
} }
*/ */
@ -31,7 +31,7 @@ import (
) )
type CciImpl interface { type CciImpl interface {
Open(g *HCL, name string, includer_name string) (int, error) Open(g *HCL, name string) (int, error)
Close(fd int) Close(fd int)
Read(fd int, buf []rune) (int, error) Read(fd int, buf []rune) (int, error)
} }
@ -221,7 +221,7 @@ func (hcl *HCL) AttachCCIO(cci CciImpl, main_cci_name string) error {
hcl.io.cci = cci hcl.io.cci = cci
hcl.io.cci_main = main_cci_name hcl.io.cci_main = main_cci_name
x = C.hcl_attachccio(hcl.c, C.hcl_io_impl_t(C.hcl_cci_Handler_for_go)) x = C.hcl_attachccio(hcl.c, C.hcl_io_impl_t(C.hcl_cci_handler_for_go))
if x <= -1 { if x <= -1 {
// restore the io handler set due to attachment failure // restore the io handler set due to attachment failure
hcl.io.cci_main = old_cci_name hcl.io.cci_main = old_cci_name