odhcp6c: do cleanup at exit
authorPaul Donald <newtwen+github@gmail.com>
Mon, 22 Dec 2025 23:35:01 +0000 (00:35 +0100)
committerÁlvaro Fernández Rojas <noltari@gmail.com>
Sun, 28 Dec 2025 11:46:37 +0000 (12:46 +0100)
cleanup frees all state buffers, releases any auth token,
and closes the random FD to avoid lingering allocations
after exit.

Signed-off-by: Paul Donald <newtwen+github@gmail.com>
Link: https://github.com/openwrt/odhcp6c/pull/145
Signed-off-by: Álvaro Fernández Rojas <noltari@gmail.com>
src/odhcp6c.c

index 72705ffc873162a0e10f006cd6d67c9cf6458b47..1966d276727d4a808206201ff95a7d0acd71d64b 100644 (file)
@@ -51,6 +51,7 @@
 
 static void sighandler(int signal);
 static int usage(void);
+static void odhcp6c_cleanup(void);
 
 static uint8_t *state_data[_STATE_MAX] = {NULL};
 static size_t state_len[_STATE_MAX] = {0};
@@ -67,6 +68,25 @@ static time_t last_update = 0;
 static char *ifname = NULL;
 struct config_dhcp *config_dhcp = NULL;
 
+static void odhcp6c_cleanup(void)
+{
+       for (int i = 0; i < _STATE_MAX; ++i) {
+               free(state_data[i]);
+               state_data[i] = NULL;
+               state_len[i] = 0;
+       }
+
+       if (config_dhcp && config_dhcp->auth_token) {
+               free(config_dhcp->auth_token);
+               config_dhcp->auth_token = NULL;
+       }
+
+       if (urandom_fd >= 0) {
+               close(urandom_fd);
+               urandom_fd = -1;
+       }
+}
+
 void __iflog(int lvl, const char *fmt, ...)
 {
        va_list ap;
@@ -206,6 +226,8 @@ int main(_o_unused int argc, char* const argv[])
        config_dhcp = config_dhcp_get();
        config_dhcp_reset();
 
+       atexit(odhcp6c_cleanup);
+
        while ((c = getopt(argc, argv, "SDN:V:P:FB:c:i:r:Ru:Ux:s:EkK:t:C:m:Lhedp:favl:")) != -1) {
                switch (c) {
                case 'S':