From bf09f6c9fc82b3b5c51706b4a7a017bddffe30e8 Mon Sep 17 00:00:00 2001 From: cold-brewed <47337035+cold-brewed@users.noreply.github.com> Date: Wed, 27 Mar 2024 14:16:21 -0400 Subject: [PATCH] Fix gcc warning with use of strncpy --- src/disk/zip.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/disk/zip.c b/src/disk/zip.c index 7045b1e41..08d3baee3 100644 --- a/src/disk/zip.c +++ b/src/disk/zip.c @@ -532,6 +532,11 @@ zip_load(zip_t *dev, char *fn) fatal("zip_load(): Error seeking to the beginning of the file\n"); strncpy(dev->drv->image_path, fn, sizeof(dev->drv->image_path) - 1); + // After using strncpy, dev->drv->image_path needs to be explicitly null terminated to make gcc happy. + // In the event strlen(dev->drv->image_path) == sizeof(dev->drv->image_path) (no null terminator) + // it is placed at the very end. Otherwise, it is placed right after the string. + const size_t term = strlen(dev->drv->image_path) == sizeof(dev->drv->image_path) ? sizeof(dev->drv->image_path) - 1 : strlen(dev->drv->image_path); + dev->drv->image_path[term] = '\0'; return 1; }