Skip to content

Commit cbe08dd

Browse files
madolsonzuiderkwast
authored andcommitted
Fix undefined behavior defined by ASAN (valkey-io#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]>
1 parent 1e20853 commit cbe08dd

16 files changed

Lines changed: 50 additions & 25 deletions

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. */
@@ -2445,12 +2445,12 @@ sds ACLLoadFromFile(const char *filename) {
24452445
c->user = new_user;
24462446
}
24472447

2448-
if (user_channels) raxFreeWithCallback(user_channels, (void (*)(void *))listRelease);
2449-
raxFreeWithCallback(old_users, (void (*)(void *))ACLFreeUser);
2448+
if (user_channels) raxFreeWithCallback(user_channels, listReleaseVoid);
2449+
raxFreeWithCallback(old_users, ACLFreeUserVoid);
24502450
sdsfree(errors);
24512451
return NULL;
24522452
} else {
2453-
raxFreeWithCallback(Users, (void (*)(void *))ACLFreeUser);
2453+
raxFreeWithCallback(Users, ACLFreeUserVoid);
24542454
Users = old_users;
24552455
errors =
24562456
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
@@ -1193,7 +1193,7 @@ void scanGenericCommand(client *c, robj *o, unsigned long long cursor) {
11931193
* are deep copied temporary strings. We must not free them if they are just
11941194
* a shallow copy - a pointer to the actual data in the data structure */
11951195
if (!shallow_copied_list_items) {
1196-
listSetFreeMethod(keys, (void (*)(void *))sdsfree);
1196+
listSetFreeMethod(keys, sdsfreeVoid);
11971197
}
11981198

11991199
/* For main hash table scan or scannable data structure. */

src/defrag.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@ static void activeDefragQuickListNodes(quicklist *ql) {
421421
static void defragLater(robj *obj) {
422422
if (!defrag_later) {
423423
defrag_later = listCreate();
424-
listSetFreeMethod(defrag_later, (void (*)(void *))sdsfree);
424+
listSetFreeMethod(defrag_later, sdsfreeVoid);
425425
defrag_later_cursor = 0;
426426
}
427427
sds key = sdsdup(objectGetKey(obj));

src/eval.c

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

210210
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/functions.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ libraryJoin(functionsLibCtx *functions_lib_ctx_dst, functionsLibCtx *functions_l
348348
} else {
349349
if (!old_libraries_list) {
350350
old_libraries_list = listCreate();
351-
listSetFreeMethod(old_libraries_list, (void (*)(void *))engineLibraryFree);
351+
listSetFreeMethod(old_libraries_list, engineLibraryDispose);
352352
}
353353
libraryUnlink(functions_lib_ctx_dst, old_li);
354354
listAddNodeTail(old_libraries_list, old_li);

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);

0 commit comments

Comments
 (0)