BUG/MEDIUM: applet: Fix API for function to push new data in channels buffer
authorChristopher Faulet <cfaulet@haproxy.com>
Wed, 6 Sep 2023 06:33:57 +0000 (08:33 +0200)
committerChristopher Faulet <cfaulet@haproxy.com>
Wed, 6 Sep 2023 07:29:27 +0000 (09:29 +0200)
All applets only check the -1 error value (need room) for applet_put*
functions while the underlying functions may also return -2 if the input is
closed or -3 if the data length is invalid. It means applets already handle
other cases by their own.

The API should be fixed but for now, to ease backports, we only fix
applet_put* functions to always return -1 on error. This way, at least for
the applets point of view, the API is consistent.

This patch should be backported to 2.8. Probably not further. Except if we
suspect it could fix a bug.

include/haproxy/applet.h

index 62df5c8..2e342f8 100644 (file)
@@ -177,8 +177,15 @@ static inline int applet_putchk(struct appctx *appctx, struct buffer *chunk)
        int ret;
 
        ret = ci_putchk(sc_ic(se->sc), chunk);
-       if (ret == -1)
+       if (ret < 0) {
+               /* XXX: Handle all errors as a lack of space because callers
+                * don't handles other cases for now. So applets must be
+                * carefull to handles shutdown (-2) and invalid calls (-3) by
+                * themselves.
+                */
                sc_need_room(se->sc, chunk->data);
+               ret = -1;
+       }
 
        return ret;
 }
@@ -193,8 +200,15 @@ static inline int applet_putblk(struct appctx *appctx, const char *blk, int len)
        int ret;
 
        ret = ci_putblk(sc_ic(se->sc), blk, len);
-       if (ret == -1)
+       if (ret < -1) {
+               /* XXX: Handle all errors as a lack of space because callers
+                * don't handles other cases for now. So applets must be
+                * carefull to handles shutdown (-2) and invalid calls (-3) by
+                * themselves.
+                */
                sc_need_room(se->sc, len);
+               ret = -1;
+       }
 
        return ret;
 }
@@ -210,8 +224,15 @@ static inline int applet_putstr(struct appctx *appctx, const char *str)
        int ret;
 
        ret = ci_putstr(sc_ic(se->sc), str);
-       if (ret == -1)
+       if (ret == -1) {
+               /* XXX: Handle all errors as a lack of space because callers
+                * don't handles other cases for now. So applets must be
+                * carefull to handles shutdown (-2) and invalid calls (-3) by
+                * themselves.
+                */
                sc_need_room(se->sc, strlen(str));
+               ret = -1;
+       }
 
        return ret;
 }
@@ -226,8 +247,15 @@ static inline int applet_putchr(struct appctx *appctx, char chr)
        int ret;
 
        ret = ci_putchr(sc_ic(se->sc), chr);
-       if (ret == -1)
+       if (ret == -1) {
+               /* XXX: Handle all errors as a lack of space because callers
+                * don't handles other cases for now. So applets must be
+                * carefull to handles shutdown (-2) and invalid calls (-3) by
+                * themselves.
+                */
                sc_need_room(se->sc, 1);
+               ret = -1;
+       }
 
        return ret;
 }