TS-3747: Error in Huffman decoder for HPACK (cherry picked from commit b04bc9b6adde276b07aa74549daac4ca4b5341e0) (cherry picked from commit f426e6368f0c1744ed85653f21772b6fe85e43b0) (cherry picked from commit 549f26680eaf5c8c9a30e9ea44620e5d83257a90)
Project: http://git-wip-us.apache.org/repos/asf/trafficserver/repo Commit: http://git-wip-us.apache.org/repos/asf/trafficserver/commit/3c4ee388 Tree: http://git-wip-us.apache.org/repos/asf/trafficserver/tree/3c4ee388 Diff: http://git-wip-us.apache.org/repos/asf/trafficserver/diff/3c4ee388 Branch: refs/heads/5.3.x Commit: 3c4ee38871e8e62a2684cccc37bb9cb5142b41c0 Parents: c83854a Author: Bryan Call <[email protected]> Authored: Wed Jul 8 22:02:44 2015 -0700 Committer: Phil Sorber <[email protected]> Committed: Tue Sep 8 13:40:30 2015 -0600 ---------------------------------------------------------------------- proxy/http2/HuffmanCodec.cc | 5 ++++- proxy/http2/Makefile.am | 13 ++++++++++++ proxy/http2/test_Huffmancode.cc | 41 ++++++++++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/trafficserver/blob/3c4ee388/proxy/http2/HuffmanCodec.cc ---------------------------------------------------------------------- diff --git a/proxy/http2/HuffmanCodec.cc b/proxy/http2/HuffmanCodec.cc index adf5ecf..0efe589 100644 --- a/proxy/http2/HuffmanCodec.cc +++ b/proxy/http2/HuffmanCodec.cc @@ -290,6 +290,7 @@ static const huffman_entry huffman_table[] = {{0x1ff8, 13}, typedef struct node { node *left, *right; char ascii_code; + bool leaf_node; } Node; Node *HUFFMAN_TREE_ROOT; @@ -301,6 +302,7 @@ make_huffman_tree_node() n->left = NULL; n->right = NULL; n->ascii_code = '\0'; + n->leaf_node = false; return n; } @@ -327,6 +329,7 @@ make_huffman_tree() bit_len--; } current->ascii_code = i; + current->leaf_node = true; } return root; } @@ -370,7 +373,7 @@ huffman_decode(char *dst_start, const uint8_t *src, uint32_t src_len) current = current->left; } - if (current->ascii_code) { + if (current->leaf_node == true) { *dst_end = current->ascii_code; ++dst_end; current = HUFFMAN_TREE_ROOT; http://git-wip-us.apache.org/repos/asf/trafficserver/blob/3c4ee388/proxy/http2/Makefile.am ---------------------------------------------------------------------- diff --git a/proxy/http2/Makefile.am b/proxy/http2/Makefile.am index 177d9e6..7dc06f8 100644 --- a/proxy/http2/Makefile.am +++ b/proxy/http2/Makefile.am @@ -46,3 +46,16 @@ libhttp2_a_SOURCES = \ Http2SessionAccept.h \ HuffmanCodec.cc \ HuffmanCodec.h + +noinst_PROGRAMS = \ + test_Huffmancode + +TESTS = test_Huffmancode + +test_Huffmancode_LDADD = \ + $(top_builddir)/lib/ts/libtsutil.la + +test_Huffmancode_SOURCES = \ + test_Huffmancode.cc \ + HuffmanCodec.cc \ + HuffmanCodec.h http://git-wip-us.apache.org/repos/asf/trafficserver/blob/3c4ee388/proxy/http2/test_Huffmancode.cc ---------------------------------------------------------------------- diff --git a/proxy/http2/test_Huffmancode.cc b/proxy/http2/test_Huffmancode.cc new file mode 100644 index 0000000..e7a973f --- /dev/null +++ b/proxy/http2/test_Huffmancode.cc @@ -0,0 +1,41 @@ +#include "HuffmanCodec.h" +#include <stdlib.h> +#include <iostream> + +using namespace std; + +void +test() +{ + const int size = 1024; + char *dst_start = (char *)malloc(size * 2); + char string[size]; + for (int i = 0; i < size; i++) { + long num = lrand48(); + string[i] = (char)num; + } + const uint8_t *src = (const uint8_t *)string; + uint32_t src_len = sizeof(string); + + int bytes = huffman_decode(dst_start, src, src_len); + + cout << "bytes: " << bytes << endl; + for (int i = 0; i < bytes; i++) { + cout << i << " " << (int)dst_start[i] << " " << dst_start[i] << endl; + } + + free(dst_start); +} + +int +main() +{ + hpack_huffman_init(); + + for (int i = 0; i < 100; i++) { + test(); + } + + hpack_huffman_fin(); + return 0; +}
