Module: kamailio
Branch: master
Commit: 0c543cde25624f1068c73c948ecb865e35708d88
URL: 
https://github.com/kamailio/kamailio/commit/0c543cde25624f1068c73c948ecb865e35708d88

Author: Richard Fuchs <[email protected]>
Committer: Richard Fuchs <[email protected]>
Date: 2026-03-11T10:54:16-04:00

core: double-buffering for su2a()

- keep two static buffers for output of su2a
- add static state variable to alternate between 0/1
- now two addresses can be printed from the same statement

---

Modified: src/core/ip_addr.c

---

Diff:  
https://github.com/kamailio/kamailio/commit/0c543cde25624f1068c73c948ecb865e35708d88.diff
Patch: 
https://github.com/kamailio/kamailio/commit/0c543cde25624f1068c73c948ecb865e35708d88.patch

---

diff --git a/src/core/ip_addr.c b/src/core/ip_addr.c
index dcbf474f99c..f133cc67ac9 100644
--- a/src/core/ip_addr.c
+++ b/src/core/ip_addr.c
@@ -352,29 +352,33 @@ char *ip_addr2xstrz(struct ip_addr *ip)
  */
 char *su2a(union sockaddr_union *su, int su_len)
 {
-       static char buf[SU2A_MAX_STR_SIZE];
+       static char bufs[2][SU2A_MAX_STR_SIZE];
+       static int buf_idx = 0;
+       char *buf;
        int offs;
 
+       buf = bufs[buf_idx ^= 1];
+
        if(unlikely(su->s.sa_family == AF_INET6)) {
                if(unlikely(su_len < sizeof(su->sin6)))
                        return "<addr. error>";
                buf[0] = '[';
                offs = 1
                           + ip6tosbuf((unsigned char 
*)su->sin6.sin6_addr.s6_addr, &buf[1],
-                                          sizeof(buf) - 4);
+                                          SU2A_MAX_STR_SIZE - 4);
                buf[offs] = ']';
                offs++;
        } else {
                if(unlikely(su_len < sizeof(su->sin)))
                        return "<addr. error>";
                else
-                       offs = ip4tosbuf(
-                                       (unsigned char *)&su->sin.sin_addr, 
buf, sizeof(buf) - 2);
+                       offs = ip4tosbuf((unsigned char *)&su->sin.sin_addr, 
buf,
+                                       SU2A_MAX_STR_SIZE - 2);
        }
        buf[offs] = ':';
        offs += 1
                        + ushort2sbuf(su_getport(su), &buf[offs + 1],
-                                       sizeof(buf) - (offs + 1) - 1);
+                                       SU2A_MAX_STR_SIZE - (offs + 1) - 1);
        buf[offs] = 0;
        return buf;
 }

_______________________________________________
Kamailio - Development Mailing List -- [email protected]
To unsubscribe send an email to [email protected]
Important: keep the mailing list in the recipients, do not reply only to the 
sender!

Reply via email to