bneradt commented on code in PR #13925:
URL: https://github.com/apache/trafficserver/pull/13925#discussion_r4556363960


##########
include/proxy/hdrs/HuffmanCodec.h:
##########
@@ -35,5 +35,6 @@
       Huffman expands to at most 8/5 of the encoded length, so sizing dst at
       2x src_len always suffices (see xpack_decode_string).
  */
+int64_t huffman_encoded_len(uint8_t const *src, uint32_t src_len);

Review Comment:
   This was added directly under the `huffman_decode` doc comment, so that 
comment now reads as if it describes `huffman_encoded_len`. Please add a doc 
comment for it: it returns the exact number of bytes `huffman_encode` produces 
for the input, `(bits + 7) / 8`.



##########
src/proxy/hdrs/XPACK.cc:
##########
@@ -229,7 +217,17 @@ xpack_encode_string(uint8_t *buf_start, const uint8_t 
*buf_end, const char *valu
 
   // Value
   if (payload_len) {
-    memcpy(p, use_huffman ? data : reinterpret_cast<const uint8_t *>(value), 
payload_len);
+    if (use_huffman) {
+      uint32_t const                 max_expected_size = value_len * 4;
+      ts::LocalBuffer<uint8_t, 4096> local_buffer(max_expected_size);

Review Comment:
   If the scratch buffer stays, please size it from `huffman_len` (plus 
whatever slack the encoder needs) rather than `value_len * 4`. The 4x size 
pushes values over 4096 bytes onto the heap, and `uint32_t max_expected_size = 
value_len * 4` can overflow for large `value_len` (pre-existing, but easy to 
avoid now).



##########
src/proxy/hdrs/unit_tests/test_Huffmancode.cc:
##########
@@ -337,6 +337,7 @@ TEST_CASE("decoder_roundtrip_fuzz", "[proxy][huffman]")
     int64_t              enc_len = huffman_encode(encoded.data(), 
encoded.size(), src.data(), src_len);
     REQUIRE(enc_len >= 0);
     REQUIRE(enc_len <= static_cast<int64_t>(encoded.size()));
+    REQUIRE(huffman_encoded_len(src.data(), src_len) == enc_len);

Review Comment:
   The length parity check in the fuzz test is good. Is there a direct test 
that `xpack_encode_string` takes the raw path for incompressible input and for 
a tie (`huffman_len == value_len`), and handles an empty string? If #13901 
already covers that, ignore this.



##########
src/proxy/hdrs/XPACK.cc:
##########
@@ -229,7 +217,17 @@ xpack_encode_string(uint8_t *buf_start, const uint8_t 
*buf_end, const char *valu
 
   // Value
   if (payload_len) {
-    memcpy(p, use_huffman ? data : reinterpret_cast<const uint8_t *>(value), 
payload_len);
+    if (use_huffman) {
+      uint32_t const                 max_expected_size = value_len * 4;

Review Comment:
   The Huffman path now walks the input twice (once for the length, once to 
encode) and still allocates the scratch buffer, so compressible values get 
slightly slower; only the incompressible case gets faster.
   
   Since `payload_len` is known before the value is written, could we encode 
directly into `p`? `buf_end - p >= payload_len` is already checked above, which 
would drop the scratch buffer and the `memcpy`. The encoder's 8-byte fast path 
is guarded by `dst_end`, so an exact-size destination should fall through to 
the byte loop, but the fuzz test should confirm that.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to