#1361·hiredis

`redisConnectUnix`: 当路径长度为 `sizeof(sun_path)-1` 或更长时,`strncpy` 会忽略 `NUL`

作者: yayong3创建于 2026年8月28日更新于 2026年8月28日

static void *fill_malloc(size_t n) { void *p = malloc(n); if (p) memset(p, 0xAB, n); return p; } int main(void) { hiredisAllocFuncs ha = { .mallocFn = fill_malloc, .callocFn = calloc, .reallocFn = realloc, .strdupFn = strdup, .freeFn = free, }; size_t cap = sizeof((struct sockaddr_un){0}.sun_path); char *path = malloc(cap); size_t i; redisContext *c; const struct sockaddr_un *un; const char *nul; unsigned last; hiredisSetAllocators(&ha); for (i = 0; i < cap - 1; i++) path[i] = 'a'; path[cap - 1] = '\0'; fprintf(stderr, "path_len=%zu sun_path_cap=%zu\n", strlen(path), cap); c = redisConnectUnix(path); if (c == NULL || c->saddr == NULL) return 1; un = (const struct sockaddr_un *)c->saddr; nul = memchr(un->sun_path, 0, sizeof