Skip to content

Commit

Permalink
Try to avoid strdup to avoid portability issues
Browse files Browse the repository at this point in the history
  • Loading branch information
LibretroAdmin committed Jun 29, 2024
1 parent 40c889b commit 47ec7c0
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions libretro-common/include/array/rhmap.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@
#include <string.h> /* for memcpy, memset */
#include <stddef.h> /* for ptrdiff_t, size_t */
#include <stdint.h> /* for uint32_t */
#include <string.h> /* for strdup */
#include <string.h> /* for strlen */

#define RHMAP_LEN(b) ((b) ? RHMAP__HDR(b)->len : 0)
#define RHMAP_MAX(b) ((b) ? RHMAP__HDR(b)->maxlen : 0)
Expand Down Expand Up @@ -261,7 +261,21 @@ RHMAP__UNUSED static ptrdiff_t rhmap__idx(struct rhmap__hdr* hdr, uint32_t key,
}
if (!hdr->keys[i])
{
if (add) { hdr->len++; hdr->keys[i] = key; if (str) hdr->key_strs[i] = strdup(str); return (ptrdiff_t)i; }
if (add)
{
int l;
char *t;

hdr->len++;
hdr->keys[i] = key;
l = strlen(str);
t = malloc(l + 1);
memcpy(t, s, l);
t[l] = '\0';
if (str)
hdr->key_strs[i] = t;
return (ptrdiff_t)i;
}
return (ptrdiff_t)-1;
}
}
Expand Down

0 comments on commit 47ec7c0

Please sign in to comment.