]> git.kaiwu.me - haproxy.git/commitdiff
REGTEST: spoe: fix the SPOP varint encoding in the mock agent
authorChristopher Faulet <cfaulet@haproxy.com>
Wed, 18 Nov 2026 07:55:45 +0000 (08:55 +0100)
committerChristopher Faulet <cfaulet@haproxy.com>
Fri, 20 Nov 2026 13:23:52 +0000 (14:23 +0100)
The mock agent implemented a LEB128-style variable-length
integer instead of the SPOP one: the continuation shift was
off by 16 on encode and the decode masked the continuation
bytes where the reference implementation adds their full
value. The echoed max-frame-size was thus wrong (14076 instead
of 16380 with the default tune.bufsize) and only passed the
negotiation because it stayed within the accepted range. With
a smaller tune.bufsize the test would fail.

Mirror encode_varint()/decode_varint() from
include/haproxy/intops.h. Validated against the C
implementation for all values below 2000 plus larger
boundaries.

reg-tests/spoe/spoa_mock.py

index a4020e74d8d3bb4daa0d9a09c322e1a99174507c..011dd19a7497f5381bceabf208ff35a28d276b91 100644 (file)
 import socket, struct, sys, threading
 
 def enc_varint(v):
+    # SPOP variable-length integer, mirroring encode_varint() in
+    # include/haproxy/intops.h: values below 240 fit on one byte, otherwise
+    # the low 4 bits ride on the first byte and each continuation byte
+    # carries the next 7 bits, most-significant first.
     if v < 240:
         return bytes([v])
     out = bytearray()
     out.append(0xF0 | (v & 0x0F))
-    v = (v >> 4) - 16
+    v = (v - 240) >> 4
     while v >= 128:
         out.append(0x80 | (v & 0x7F))
-        v = (v >> 7) - 1
-    out.append(v & 0x7F)
+        v = (v - 128) >> 7
+    out.append(v)
     return bytes(out)
 
 def dec_varint(buf, off):
-    b = buf[off]; off += 1
-    if b < 240:
-        return b, off
-    val = b & 0x0F
+    # SPOP variable-length integer, mirroring decode_varint() in
+    # include/haproxy/intops.h.
+    v = buf[off]; off += 1
+    if v < 240:
+        return v, off
+    val = v
     shift = 4
     while True:
         b = buf[off]; off += 1
-        val |= (b & 0x7F) << shift
+        val += b << shift
         shift += 7
         if b < 128:
             break