]> git.ipfire.org Git - thirdparty/lldpd.git/commitdiff
daemon/agent: guard against NULL/empty net-snmp log message
authorVincent Bernat <vincent@bernat.ch>
Sat, 9 May 2026 12:42:46 +0000 (14:42 +0200)
committerVincent Bernat <vincent@bernat.ch>
Sat, 9 May 2026 13:26:12 +0000 (15:26 +0200)
`strdup(NULL)` is undefined and `msg[strlen(msg) - 1]` reads `msg[-1]`
when the message is empty. Bail out on NULL and skip the
trailing-newline strip when the message is empty.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
src/daemon/agent.c

index cd631a8ea74d5b8492ffb4b18bf83165728c2fa5..682a5f6e692c80c5f9dd030873986aa8d3541a5d 100644 (file)
@@ -1852,12 +1852,16 @@ static int
 agent_log_callback(int major, int minor, void *serverarg, void *clientarg)
 {
        struct snmp_log_message *slm = (struct snmp_log_message *)serverarg;
-       char *msg = strdup(slm->msg);
+       char *msg = NULL;
+       size_t len;
        (void)major;
        (void)minor;
        (void)clientarg;
 
-       if (msg && msg[strlen(msg) - 1] == '\n') msg[strlen(msg) - 1] = '\0';
+       if (slm->msg == NULL) return 0;
+       msg = strdup(slm->msg);
+       if (msg && (len = strlen(msg)) > 0 && msg[len - 1] == '\n')
+               msg[len - 1] = '\0';
        switch (slm->priority) {
        case LOG_EMERG:
                log_warnx("libsnmp", "%s", msg ? msg : slm->msg);