Skip to content

Commit 50388d8

Browse files
madolsonzuiderkwast
andcommitted
Fix undefined behavior defined by ASAN (#1451)
Asan now supports making sure you are passing in the correct pointer type, which seems useful but we can't support it since we pass in an incorrect pointer in several places. This is most commonly done with generic free functions, where we simply cast it to the correct type. It's not a lot of code to clean up, so it seems appropriate to cleanup instead of disabling the check. --------- Signed-off-by: Madelyn Olson <[email protected]> Co-authored-by: Viktor Söderqvist <[email protected]> Signed-off-by: Viktor Söderqvist <[email protected]>
1 parent 7fa6df6 commit 50388d8

File tree

16 files changed

+56
-23
lines changed

16 files changed

+56
-23
lines changed

src/acl.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -297,11 +297,6 @@ int ACLListMatchSds(void *a, void *b) {
297297
return sdscmp(a, b) == 0;
298298
}
299299

300-
/* Method to free list elements from ACL users password/patterns lists. */
301-
void ACLListFreeSds(void *item) {
302-
sdsfree(item);
303-
}
304-
305300
/* Method to duplicate list elements from ACL users password/patterns lists. */
306301
void *ACLListDupSds(void *item) {
307302
return sdsdup(item);
@@ -374,7 +369,7 @@ aclSelector *ACLCreateSelector(int flags) {
374369
listSetFreeMethod(selector->patterns, ACLListFreeKeyPattern);
375370
listSetDupMethod(selector->patterns, ACLListDupKeyPattern);
376371
listSetMatchMethod(selector->channels, ACLListMatchSds);
377-
listSetFreeMethod(selector->channels, ACLListFreeSds);
372+
listSetFreeMethod(selector->channels, sdsfreeVoid);
378373
listSetDupMethod(selector->channels, ACLListDupSds);
379374
memset(selector->allowed_commands, 0, sizeof(selector->allowed_commands));
380375

@@ -445,7 +440,7 @@ user *ACLCreateUser(const char *name, size_t namelen) {
445440
u->passwords = listCreate();
446441
u->acl_string = NULL;
447442
listSetMatchMethod(u->passwords, ACLListMatchSds);
448-
listSetFreeMethod(u->passwords, ACLListFreeSds);
443+
listSetFreeMethod(u->passwords, sdsfreeVoid);
449444
listSetDupMethod(u->passwords, ACLListDupSds);
450445

451446
u->selectors = listCreate();
@@ -489,6 +484,11 @@ void ACLFreeUser(user *u) {
489484
zfree(u);
490485
}
491486

487+
/* Used for generic free functions. */
488+
static void ACLFreeUserVoid(void *u) {
489+
ACLFreeUser(u);
490+
}
491+
492492
/* When a user is deleted we need to cycle the active
493493
* connections in order to kill all the pending ones that
494494
* are authenticated with such user. */
@@ -2449,12 +2449,12 @@ sds ACLLoadFromFile(const char *filename) {
24492449
c->user = new_user;
24502450
}
24512451

2452-
if (user_channels) raxFreeWithCallback(user_channels, (void (*)(void *))listRelease);
2453-
raxFreeWithCallback(old_users, (void (*)(void *))ACLFreeUser);
2452+
if (user_channels) raxFreeWithCallback(user_channels, listReleaseVoid);
2453+
raxFreeWithCallback(old_users, ACLFreeUserVoid);
24542454
sdsfree(errors);
24552455
return NULL;
24562456
} else {
2457-
raxFreeWithCallback(Users, (void (*)(void *))ACLFreeUser);
2457+
raxFreeWithCallback(Users, ACLFreeUserVoid);
24582458
Users = old_users;
24592459
errors =
24602460
sdscat(errors, "WARNING: ACL errors detected, no change to the previously active ACL rules was performed");

src/adlist.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,12 @@ void listRelease(list *list) {
7777
zfree(list);
7878
}
7979

80+
/* Just like listRelease, but takes the list as a (void *).
81+
* Useful as generic free callback. */
82+
void listReleaseVoid(void *l) {
83+
listRelease((list *)l);
84+
}
85+
8086
/* Add a new node to the list, to head, containing the specified 'value'
8187
* pointer as value.
8288
*

src/adlist.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ typedef struct list {
7272
/* Prototypes */
7373
list *listCreate(void);
7474
void listRelease(list *list);
75+
void listReleaseVoid(void *list);
7576
void listEmpty(list *list);
7677
list *listAddNodeHead(list *list, void *value);
7778
list *listAddNodeTail(list *list, void *value);

src/call_reply.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -559,7 +559,7 @@ CallReply *callReplyCreateError(sds reply, void *private_data) {
559559
sdsfree(reply);
560560
}
561561
list *deferred_error_list = listCreate();
562-
listSetFreeMethod(deferred_error_list, (void (*)(void *))sdsfree);
562+
listSetFreeMethod(deferred_error_list, sdsfreeVoid);
563563
listAddNodeTail(deferred_error_list, sdsnew(err_buff));
564564
return callReplyCreate(err_buff, deferred_error_list, private_data);
565565
}

src/db.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1088,7 +1088,7 @@ void scanGenericCommand(client *c, robj *o, unsigned long long cursor) {
10881088
* The exception to the above is ZSET, where we do allocate temporary
10891089
* strings even when scanning a dict. */
10901090
if (o && (!ht || o->type == OBJ_ZSET)) {
1091-
listSetFreeMethod(keys, (void (*)(void *))sdsfree);
1091+
listSetFreeMethod(keys, sdsfreeVoid);
10921092
}
10931093

10941094
/* For main dictionary scan or data structure using hashtable. */

src/eval.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ void scriptingInit(int setup) {
205205
* and we need to free them respectively. */
206206
lctx.lua_scripts = dictCreate(&shaScriptObjectDictType);
207207
lctx.lua_scripts_lru_list = listCreate();
208-
listSetFreeMethod(lctx.lua_scripts_lru_list, (void (*)(void *))sdsfree);
208+
listSetFreeMethod(lctx.lua_scripts_lru_list, sdsfreeVoid);
209209
lctx.lua_scripts_mem = 0;
210210

211211
luaRegisterServerAPI(lua);
@@ -777,7 +777,7 @@ void ldbInit(void) {
777777
ldb.conn = NULL;
778778
ldb.active = 0;
779779
ldb.logs = listCreate();
780-
listSetFreeMethod(ldb.logs, (void (*)(void *))sdsfree);
780+
listSetFreeMethod(ldb.logs, sdsfreeVoid);
781781
ldb.children = listCreate();
782782
ldb.src = NULL;
783783
ldb.lines = 0;

src/listpack.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,12 @@ void lpFree(unsigned char *lp) {
250250
lp_free(lp);
251251
}
252252

253+
/* Same as lpFree, but useful for when you are passing the listpack
254+
* into a generic free function that expects (void *) */
255+
void lpFreeVoid(void *lp) {
256+
lp_free((unsigned char *)lp);
257+
}
258+
253259
/* Shrink the memory to fit. */
254260
unsigned char *lpShrinkToFit(unsigned char *lp) {
255261
size_t size = lpGetTotalBytes(lp);

src/listpack.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ typedef struct {
5656

5757
unsigned char *lpNew(size_t capacity);
5858
void lpFree(unsigned char *lp);
59+
void lpFreeVoid(void *lp);
5960
unsigned char *lpShrinkToFit(unsigned char *lp);
6061
unsigned char *
6162
lpInsertString(unsigned char *lp, unsigned char *s, uint32_t slen, unsigned char *p, int where, unsigned char **newp);

src/module.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10380,7 +10380,7 @@ ValkeyModuleServerInfoData *VM_GetServerInfo(ValkeyModuleCtx *ctx, const char *s
1038010380
* context instead of passing NULL. */
1038110381
void VM_FreeServerInfo(ValkeyModuleCtx *ctx, ValkeyModuleServerInfoData *data) {
1038210382
if (ctx != NULL) autoMemoryFreed(ctx, VALKEYMODULE_AM_INFO, data);
10383-
raxFreeWithCallback(data->rax, (void (*)(void *))sdsfree);
10383+
raxFreeWithCallback(data->rax, sdsfreeVoid);
1038410384
zfree(data);
1038510385
}
1038610386

src/networking.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -565,7 +565,7 @@ void afterErrorReply(client *c, const char *s, size_t len, int flags) {
565565
if (c->flag.module) {
566566
if (!c->deferred_reply_errors) {
567567
c->deferred_reply_errors = listCreate();
568-
listSetFreeMethod(c->deferred_reply_errors, (void (*)(void *))sdsfree);
568+
listSetFreeMethod(c->deferred_reply_errors, sdsfreeVoid);
569569
}
570570
listAddNodeTail(c->deferred_reply_errors, sdsnewlen(s, len));
571571
return;

0 commit comments

Comments
 (0)