Welcome to little lamb

Code » limb » commit b986d11

obuffers_add{lob,dbg}: Fix buffer state in use..

author Olivier Brunel
2023-04-02 07:00:28 UTC
committer Olivier Brunel
2023-05-20 18:06:34 UTC
parent a7090361405dc8633f924c8126aab33684403bea

obuffers_add{lob,dbg}: Fix buffer state in use..

..when it fails to be added as an extra.

The buffer is considered in use when the fd has been set, and it was
left so in case of obuffers_addextra() failure. This would lead to be
impossibility to try again (getting EADDRINUSE) whilst being wrong.

Also, buffer_init() can only fail if its cbuffer_init() fails which can
only happen when the given buffer size it too low (<2), so here it cannot
fail.

src/liblimb/obuffers.h/obuffers_adddbg.c +7 -2
src/liblimb/obuffers.h/obuffers_addlog.c +7 -2

diff --git a/src/liblimb/obuffers.h/obuffers_adddbg.c b/src/liblimb/obuffers.h/obuffers_adddbg.c
index 6d2a65f..13f4d36 100644
--- a/src/liblimb/obuffers.h/obuffers_adddbg.c
+++ b/src/liblimb/obuffers.h/obuffers_adddbg.c
@@ -12,7 +12,12 @@ obuffers_adddbg(int fd)
 {
     if (buffer_dbg_.fd >= 0)
         return (errno = EADDRINUSE, 0);
-    if (!buffer_init(&buffer_dbg_, &fd_writev, fd, buf, sizeof(buf)))
+
+    buffer_init(&buffer_dbg_, &fd_writev, fd, buf, sizeof(buf));
+    if (!obuffers_addextra(&buffer_dbg_, OLVL_DEBUG)) {
+        buffer_dbg_.fd = -1;
         return 0;
-    return obuffers_addextra(&buffer_dbg_, OLVL_DEBUG);
+    }
+
+    return 1;
 }
diff --git a/src/liblimb/obuffers.h/obuffers_addlog.c b/src/liblimb/obuffers.h/obuffers_addlog.c
index 9e556dc..97a2d7a 100644
--- a/src/liblimb/obuffers.h/obuffers_addlog.c
+++ b/src/liblimb/obuffers.h/obuffers_addlog.c
@@ -12,7 +12,12 @@ obuffers_addlog(int fd)
 {
     if (buffer_log_.fd >= 0)
         return (errno = EADDRINUSE, 0);
-    if (!buffer_init(&buffer_log_, &fd_writev, fd, buf, sizeof(buf)))
+
+    buffer_init(&buffer_log_, &fd_writev, fd, buf, sizeof(buf));
+    if (!obuffers_addextra(&buffer_log_, OLVL_NORMAL)) {
+        buffer_log_.fd = -1;
         return 0;
-    return obuffers_addextra(&buffer_log_, OLVL_NORMAL);
+    }
+
+    return 1;
 }