Skip to content

Commit 00c61d2

Browse files
jmarshalldaviesrob
authored andcommitted
Provide separate dlsym() wrappers for functions and data
Avoid -pedantic warnings about data pointer <-> function pointer conversions (which are required to work on POSIX platforms so that dlsym() works, but are nonetheless warned about by compilers). As we're providing wrappers around dlsym() anyway, we can provide separate function and data wrappers; change load_plugin()'s return type as it (practically) always returns a function. This uses the workaround recommended in POSIX Issue 6 dlsym() (since removed in Issue 7 by [1]; at least for GCC, type-punning through a union might be preferable). Hat tip Rob Davies. [1] https://www.austingroupbugs.net/view.php?id=74
1 parent 2cb229a commit 00c61d2

3 files changed

Lines changed: 25 additions & 9 deletions

File tree

hts_internal.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,10 @@ void hts_path_itr_setup(struct hts_path_itr *itr, const char *path,
9696

9797
const char *hts_path_itr_next(struct hts_path_itr *itr);
9898

99-
void *load_plugin(void **pluginp, const char *filename, const char *symbol);
99+
typedef void plugin_void_func(void);
100+
plugin_void_func *load_plugin(void **pluginp, const char *filename, const char *symbol);
100101
void *plugin_sym(void *plugin, const char *name, const char **errmsg);
102+
plugin_void_func *plugin_func(void *plugin, const char *name, const char **errmsg);
101103
void close_plugin(void *plugin);
102104

103105
/*

plugin.c

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,12 +132,13 @@ const char *hts_path_itr_next(struct hts_path_itr *itr)
132132
#define RTLD_NOLOAD 0
133133
#endif
134134

135-
void *load_plugin(void **pluginp, const char *filename, const char *symbol)
135+
plugin_void_func *load_plugin(void **pluginp, const char *filename, const char *symbol)
136136
{
137137
void *lib = dlopen(filename, RTLD_NOW | RTLD_LOCAL);
138138
if (lib == NULL) goto error;
139139

140-
void *sym = dlsym(lib, symbol);
140+
plugin_void_func *sym;
141+
*(void **) &sym = dlsym(lib, symbol);
141142
if (sym == NULL) {
142143
// Reopen the plugin with RTLD_GLOBAL and check for uniquified symbol
143144
void *libg = dlopen(filename, RTLD_NOLOAD | RTLD_NOW | RTLD_GLOBAL);
@@ -152,7 +153,7 @@ void *load_plugin(void **pluginp, const char *filename, const char *symbol)
152153
const char *basename = slash? slash+1 : filename;
153154
kputsn(basename, strcspn(basename, ".-+"), &symbolg);
154155

155-
sym = dlsym(lib, symbolg.s);
156+
*(void **) &sym = dlsym(lib, symbol);
156157
free(symbolg.s);
157158
if (sym == NULL) goto error;
158159
}
@@ -175,6 +176,13 @@ void *plugin_sym(void *plugin, const char *name, const char **errmsg)
175176
return sym;
176177
}
177178

179+
plugin_void_func *plugin_func(void *plugin, const char *name, const char **errmsg)
180+
{
181+
plugin_void_func *sym;
182+
*(void **) &sym = plugin_sym(plugin, name, errmsg);
183+
return sym;
184+
}
185+
178186
void close_plugin(void *plugin)
179187
{
180188
if (dlclose(plugin) != 0) {

test/plugins-dlhts.c

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,13 @@ void *sym(void *htslib, const char *name)
5555
return ptr;
5656
}
5757

58+
typedef void void_func(void);
59+
void_func *func(void *htslib, const char *name) {
60+
void_func *fptr;
61+
*(void **) &fptr = sym(htslib, name);
62+
return fptr;
63+
}
64+
5865
int errors = 0;
5966
int verbose = 0;
6067

@@ -112,11 +119,11 @@ int main(int argc, char **argv)
112119
*hts_verbosep += verbose;
113120

114121
typedef const char *cstr_func(void);
115-
printf("Loaded HTSlib %s\n", ((cstr_func *) sym(htslib, "hts_version"))());
122+
printf("Loaded HTSlib %s\n", ((cstr_func *) func(htslib, "hts_version"))());
116123
}
117124

118-
hopen_p = (hopen_func *) sym(htslib, "hopen");
119-
hclose_abruptly_p = (hclose_abruptly_func *) sym(htslib, "hclose_abruptly");
125+
hopen_p = (hopen_func *) func(htslib, "hopen");
126+
hclose_abruptly_p = (hclose_abruptly_func *) func(htslib, "hclose_abruptly");
120127

121128
test_hopen("bad-scheme:unsupported", 0);
122129
#ifdef HAVE_LIBCURL
@@ -129,8 +136,7 @@ int main(int argc, char **argv)
129136
test_hopen("s3:invalid", 1);
130137
#endif
131138

132-
typedef void void_func(void);
133-
((void_func *) sym(htslib, "hts_lib_shutdown"))();
139+
(func(htslib, "hts_lib_shutdown"))();
134140

135141
if (dlclose(htslib) < 0) {
136142
fprintf(stderr, "Can't dlclose \"%s\": %s\n", argv[optind], dlerror());

0 commit comments

Comments
 (0)