Skip to content

Commit bfe9e39

Browse files
eliottnessEliott Bouhana
andauthored
dlfcn: rework error check (ebitengine#119)
Closes ebitengine#118 Signed-off-by: Eliott Bouhana <[email protected]> Co-authored-by: Eliott Bouhana <[email protected]>
1 parent ebd8567 commit bfe9e39

File tree

4 files changed

+52
-5
lines changed

4 files changed

+52
-5
lines changed

callback_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func TestCallGoFromSharedLib(t *testing.T) {
3131

3232
lib, err := purego.Dlopen(libFileName, purego.RTLD_NOW|purego.RTLD_GLOBAL)
3333
if err != nil {
34-
t.Fatalf("Dlopen(%q) faled: %v", libFileName, err)
34+
t.Fatalf("Dlopen(%q) failed: %v", libFileName, err)
3535
}
3636

3737
var callCallback func(p uintptr, s string) int

dlfcn.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ func init() {
3535
// Dlopen calls should be balanced with a Dlclose call.
3636
func Dlopen(path string, mode int) (uintptr, error) {
3737
u := fnDlopen(path, mode)
38-
if errStr := fnDlerror(); errStr != "" {
39-
return 0, Dlerror{errStr}
38+
if u == 0 {
39+
return 0, Dlerror{fnDlerror()}
4040
}
4141
return u, nil
4242
}
@@ -47,8 +47,8 @@ func Dlopen(path string, mode int) (uintptr, error) {
4747
// when that library was loaded, Dlsym returns zero.
4848
func Dlsym(handle uintptr, name string) (uintptr, error) {
4949
u := fnDlsym(handle, name)
50-
if errStr := fnDlerror(); errStr != "" {
51-
return 0, Dlerror{errStr}
50+
if u == 0 {
51+
return 0, Dlerror{fnDlerror()}
5252
}
5353
return u, nil
5454
}

dlfcn_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@ package purego_test
77

88
import (
99
"fmt"
10+
"os"
11+
"os/exec"
12+
"path/filepath"
1013
"runtime"
14+
"strings"
1115
"testing"
1216
"unsafe"
1317

@@ -90,3 +94,32 @@ func Test_qsort(t *testing.T) {
9094
}
9195
}
9296
}
97+
98+
func TestNestedDlopenCall(t *testing.T) {
99+
libFileName := filepath.Join(t.TempDir(), "libdlnested.so")
100+
t.Logf("Build %v", libFileName)
101+
102+
out, err := exec.Command("go", "env", "CXX").Output()
103+
if err != nil {
104+
t.Fatalf("go env error: %v", err)
105+
}
106+
107+
cxx := strings.TrimSpace(string(out))
108+
if cxx == "" {
109+
t.Fatal("CXX not found")
110+
}
111+
112+
args := []string{"-shared", "-Wall", "-Werror", "-o", libFileName, filepath.Join("libdlnested", "nested.cpp")}
113+
cmd := exec.Command(cxx, args...)
114+
if out, err := cmd.CombinedOutput(); err != nil {
115+
t.Fatalf("compile lib: %v\n%q\n%s", err, cmd, string(out))
116+
}
117+
defer os.Remove(libFileName)
118+
119+
lib, err := purego.Dlopen(libFileName, purego.RTLD_NOW|purego.RTLD_GLOBAL)
120+
if err != nil {
121+
t.Fatalf("Dlopen(%q) failed: %v", libFileName, err)
122+
}
123+
124+
purego.Dlclose(lib)
125+
}

libdlnested/nested.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
// SPDX-FileCopyrightText: 2023 The Ebitengine Authors
3+
4+
#include <dlfcn.h>
5+
6+
struct nested_libdl
7+
{
8+
nested_libdl() {
9+
// Fails for sure because a symbol cannot be named like this
10+
dlsym(RTLD_DEFAULT, "@/*<>");
11+
}
12+
};
13+
14+
static nested_libdl test;

0 commit comments

Comments
 (0)