Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

zstd: Fix copy of dict binary to environment #9447

Merged
merged 1 commit into from
Feb 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 34 additions & 23 deletions erts/emulator/nifs/common/zstd_nif.c
Original file line number Diff line number Diff line change
Expand Up @@ -820,29 +820,34 @@ static ERL_NIF_TERM create_cdict_nif(
ErlNifBinary bin;
int level;

if (!enif_inspect_iolist_as_binary(env, argv[0], &bin) ||
!enif_get_int(env, argv[1], &level)) {
if (!enif_is_binary(env, argv[0]) || !enif_get_int(env, argv[1], &level)) {
return enif_make_badarg(env);
} else {
ERL_NIF_TERM result;
ZstdDict *dict;
ZSTD_CDict *cdict = ZSTD_createCDict_advanced(bin.data, bin.size,
ERL_NIF_TERM result, binary_copy;
ZstdDict *dictp, dict;

dict.env = enif_alloc_env();
binary_copy = enif_make_copy(dict.env, argv[0]);

(void)enif_inspect_binary(dict.env, binary_copy, &bin);

dict.c = ZSTD_createCDict_advanced(bin.data, bin.size,
ZSTD_dlm_byRef, ZSTD_dct_auto,
ZSTD_getCParams(level, 0, bin.size),
zstd_customMem);

if (!cdict)
if (!dict.c) {
enif_free_env(dict.env);
return enif_make_tuple2(env, am_error,
enif_make_atom(env, "invalid_compress_dict"));
}

dict = enif_alloc_resource(compress_dict_type, sizeof(ZstdDict));
dictp = enif_alloc_resource(compress_dict_type, sizeof(ZstdDict));

dict->c = cdict;
dict->env = enif_alloc_env();
enif_make_binary(dict->env, &bin);
*dictp = dict;

result = enif_make_resource(env, (void *)dict);
enif_release_resource((void *)dict);
result = enif_make_resource(env, (void *)dictp);
enif_release_resource((void *)dictp);
return result;
}
}
Expand All @@ -857,26 +862,32 @@ static ERL_NIF_TERM create_ddict_nif(
ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]) {
ErlNifBinary bin;

if (!enif_inspect_iolist_as_binary(env, argv[0], &bin)) {
if (!enif_is_binary(env, argv[0])) {
return enif_make_badarg(env);
} else {
ERL_NIF_TERM result;
ZstdDict *dict;
ZSTD_DDict *ddict = ZSTD_createDDict_advanced(bin.data, bin.size,
ERL_NIF_TERM result, binary_copy;
ZstdDict *dictp, dict;

dict.env = enif_alloc_env();
binary_copy = enif_make_copy(dict.env, argv[0]);

(void)enif_inspect_binary(dict.env, binary_copy, &bin);

dict.d = ZSTD_createDDict_advanced(bin.data, bin.size,
ZSTD_dlm_byRef, ZSTD_dct_auto, zstd_customMem);

if (!ddict)
if (!dict.d) {
enif_free_env(dict.env);
return enif_make_tuple2(env, am_error,
enif_make_atom(env, "invalid_decompress_dict"));
}

dict = enif_alloc_resource(decompress_dict_type, sizeof(ZstdDict));
dictp = enif_alloc_resource(decompress_dict_type, sizeof(ZstdDict));

dict->d = ddict;
dict->env = enif_alloc_env();
enif_make_binary(dict->env, &bin);
*dictp = dict;

result = enif_make_resource(env, (void *)dict);
enif_release_resource((void *)dict);
result = enif_make_resource(env, (void *)dictp);
enif_release_resource((void *)dictp);
return result;
}
}
Expand Down
9 changes: 7 additions & 2 deletions erts/emulator/zstd/zstd.mk
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,17 @@ endif

ifeq ($(TYPE),gcov)
ZSTD_CFLAGS = -O0 -fprofile-arcs -ftest-coverage $(DEBUG_CFLAGS) $(DEFS) $(THR_DEFS)
else # gcov
else # !gcov
ifeq ($(TYPE),debug)
## DEBUGLEVEL=1 enables asserts, see common/debug.h for details
ZSTD_CFLAGS = -DDEBUGLEVEL=1 $(DEBUG_CFLAGS) $(DEFS) $(THR_DEFS)
else # debug
else # !debug && !gcov

ZSTD_CFLAGS = $(subst -O2, -O3, $(CONFIGURE_CFLAGS) $(DEFS) $(THR_DEFS))
ifeq ($(TYPE), asan)
ZSTD_CFLAGS += -DZSTD_ADDRESS_SANITIZER
endif # asan

endif # debug
endif # gcov

Expand Down
3 changes: 3 additions & 0 deletions lib/stdlib/test/zstd_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,9 @@ dict_api(Config) ->
{ok, DCtx} = zstd:context(decompress, #{ dictionary => DDict }),
{'EXIT', _} = catch zstd:set_parameter(DCtx, dictionary, CDict),

{'EXIT', _} = catch zstd:dict(compress, [1,2,3]),
{'EXIT', _} = catch zstd:dict(decompress, [1,2,3]),

ok.


Expand Down
Loading