Skip to content

Commit 6f89c06

Browse files
authored
Merge pull request #29 from msantos/bad_hdr_binary
Bad hdr binary
2 parents 46a4f86 + 293d0b8 commit 6f89c06

File tree

3 files changed

+16
-7
lines changed

3 files changed

+16
-7
lines changed

c_src/hdr_histogram_log.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,7 @@ int hdr_encode_compressed(
448448
len = new_len;
449449

450450
// Flush the zlib stream. Breaks without this.
451-
if (deflate(&strm, Z_SYNC_FLUSH) != Z_OK)
451+
if (strm.avail_in > 0 && deflate(&strm, Z_SYNC_FLUSH) != Z_OK)
452452
{
453453
FAIL_AND_CLEANUP(cleanup, result, HDR_DEFLATE_FAIL);
454454
}

c_src/hdr_histogram_nif.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -874,6 +874,11 @@ ERL_NIF_TERM _hh_to_binary(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
874874
uint8_t* data = NULL;
875875
int success = hdr_encode_compressed(ctx->data, &data, &size);
876876

877+
if (success != 0)
878+
{
879+
return make_error(env, "bad_hdr_binary");
880+
}
881+
877882
if (!enif_alloc_binary(size, &target))
878883
{
879884
return make_error(env, "bad_hdr_binary_alloc");
@@ -882,11 +887,6 @@ ERL_NIF_TERM _hh_to_binary(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
882887
memcpy(target.data,data,size);
883888
free(data);
884889

885-
if (success != 0)
886-
{
887-
return make_error(env, "bad_hdr_binary");
888-
}
889-
890890
return enif_make_binary(env, &target);
891891
}
892892

test/hdr_histogram_SUITE.erl

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
-export([t_counter_example_stddev/1]).
3131
-export([t_issue_004/1]).
3232
-export([t_issue_013/1]).
33+
-export([t_issue_021/1]).
3334
-export([t_unique_resource_types/1]).
3435
-export([t_use_after_close/1]).
3536

@@ -61,7 +62,7 @@ groups() ->
6162
, t_hdr_reset
6263
, t_hdr_close
6364
, t_hdr_binary
64-
, t_hdr_binary_nc %% Commented out. Issues arize when used with CT
65+
, t_hdr_binary_nc
6566
]},
6667
{iter, [], [
6768
t_iter_recorded
@@ -76,6 +77,7 @@ groups() ->
7677
{regression, [], [
7778
t_issue_004,
7879
t_issue_013,
80+
t_issue_021,
7981
t_unique_resource_types,
8082
t_use_after_close
8183
]}].
@@ -304,6 +306,13 @@ t_issue_013(_Config) ->
304306
end || X <- lists:seq(0,10) ],
305307
{error, value_out_of_range} = hdr_histogram:record(R, -1),
306308
{error, value_out_of_range} = hdr_histogram:record(R, 11).
309+
310+
t_issue_021(_Config) ->
311+
{ok, H1} = hdr_histogram:open(524287, 5),
312+
Bin = hdr_histogram:to_binary(H1),
313+
{ok, H2} = hdr_histogram:from_binary(Bin),
314+
Bin = hdr_histogram:to_binary(H2).
315+
307316

308317
t_unique_resource_types(_Config) ->
309318
{ok, H} = hdr_histogram:open(10, 1),

0 commit comments

Comments
 (0)