Skip to content

Commit

Permalink
Merge pull request #66 from larsewi/strndup
Browse files Browse the repository at this point in the history
Fixed issue with strndup on Windows
  • Loading branch information
larsewi authored May 3, 2024
2 parents a92b252 + 9237be3 commit d7d9252
Show file tree
Hide file tree
Showing 8 changed files with 153 additions and 166 deletions.
2 changes: 1 addition & 1 deletion configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Process this file with autoconf to produce a configure script.

AC_PREREQ([2.63])
AC_INIT([leech], [0.1.12], [https://github.com/larsewi/leech/issues], [leech],
AC_INIT([leech], [0.1.13], [https://github.com/larsewi/leech/issues], [leech],
[https://github.com/larsewi/leech])
AC_CONFIG_SRCDIR([lib/leech.h])

Expand Down
1 change: 1 addition & 0 deletions lib/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,6 @@ libleech_la_SOURCES = leech.c \
table.h table.c \
utils.h utils.c \
sha1.h sha1.c \
module.h module.c \
definitions.h
libleech_la_LDFLAGS = -ldl -lm
77 changes: 77 additions & 0 deletions lib/module.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#include "module.h"

#include <stdlib.h>

#if HAVE_DLFCN_H
#include <dlfcn.h>
#endif // HAVE_DLFCN_H

#if _WIN32
#include <libloaderapi.h>
#endif // _WIN32

#include "logger.h"

void *LCH_ModuleLoad(const char *const path) {
LCH_LOG_DEBUG("Loading dynamic shared library '%s'", path);
#if HAVE_DLFCN_H
void *handle = dlopen(path, RTLD_NOW);
if (handle == NULL) {
LCH_LOG_ERROR("Failed to load dynamic shared library '%s': %s", path,
dlerror());
return NULL;
}
return handle;
#elif defined(_WIN32)
void *handle = LoadLibraryA(path);
if (handle == NULL) {
LCH_LOG_ERROR("Failed to load dynamic shared library '%s': Error code %lu",
path, GetLastError());
}
return handle;
#else
LCH_LOG_ERROR("Failed to load dynamic shared library '%s'", path);
return NULL;
#endif
}

void *LCH_ModuleGetSymbol(void *const handle, const char *const symbol) {
LCH_LOG_DEBUG("Obtaining address of symbol '%s'", symbol);
#if HAVE_DLFCN_H
void *address = dlsym(handle, symbol);
if (address == NULL) {
LCH_LOG_ERROR("Failed to obtain address of symbol '%s': %s", symbol,
dlerror());
return NULL;
}
return address;
#elif defined(_WIN32)
void *address = GetProcAddress(handle, symbol);
if (address == NULL) {
LCH_LOG_ERROR("Failed to obtain address of symbol '%s': Error code %lu",
symbol, GetLastError());
}
return address;
#else
LCH_LOG_ERROR("Failed to obtain address of symbol '%s'", symbol);
return NULL;
#endif
}

void LCH_ModuleDestroy(void *const handle) {
if (handle == NULL) {
return;
}
#if HAVE_DLFCN_H
if (dlclose(handle) != 0) {
LCH_LOG_WARNING("Failed to release handle to dynamic shared library: %s",
dlerror());
}
#elif defined(_WIN32)
if (FreeLibrary(handle) == 0) {
LCH_LOG_WARNING(
"Failed to release handle to dynamic shared library: Error code %lu",
GetLastError());
}
#endif
}
12 changes: 12 additions & 0 deletions lib/module.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#ifndef _LEECH_MODULE_H
#define _LEECH_MODULE_H

#include <stdbool.h>

void *LCH_ModuleLoad(const char *path);

void *LCH_ModuleGetSymbol(void *handle, const char *const symbol);

void LCH_ModuleDestroy(void *handle);

#endif // _LEECH_MODULE_H
27 changes: 24 additions & 3 deletions lib/string_lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,8 @@ LCH_List *LCH_StringSplit(const char *str, const char *del) {
const char *end = strpbrk(str, del);

while (end != NULL) {
char *tmp = strndup(start, end - start);
char *tmp = LCH_StringNDuplicate(start, end - start);
if (tmp == NULL) {
LCH_LOG_ERROR("strndup(3): Failed to allocate memory: %s",
strerror(errno));
return NULL;
}

Expand Down Expand Up @@ -293,3 +291,26 @@ char *LCH_StringDuplicate(const char *const str) {
}
return dup;
}

char *LCH_StringNDuplicate(const char *const str, const size_t n) {
if (str == NULL) {
return NULL;
}

#if HAVE_STRNDUP
char *const dup = strndup(str, n);
if (dup == NULL) {
LCH_LOG_ERROR("strndup(3): Failed to allocate memory: %s", strerror(errno));
return NULL;
}
#else // HAVE_STRNDUP
char *const dup = malloc(n + 1);
if (dup == NULL) {
LCH_LOG_ERROR("malloc(3): Failed to allocate memory: %s", strerror(errno));
return NULL;
}
memcpy(dup, str, n);
dup[n] = '\0';
#endif // HAVE_STRNDUP
return dup;
}
2 changes: 2 additions & 0 deletions lib/string_lib.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ char *LCH_StringTruncate(const char *str, size_t len, size_t max);

char *LCH_StringDuplicate(const char *str);

char *LCH_StringNDuplicate(const char *str, size_t n);

char *LCH_StringFormat(const char *format, ...);

bool LCH_StringParseNumber(const char *str, long *number);
Expand Down
Loading

0 comments on commit d7d9252

Please sign in to comment.