Merge pull request #13394 from thalesmg/20240702-r57-atom-leak-schema-registry

fix(schema registry): avoid atom leak
This commit is contained in:
Thales Macedo Garitezi 2024-07-02 17:40:26 -03:00 committed by GitHub
commit 9ef3eff4c6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 18 additions and 3 deletions

View File

@ -77,7 +77,7 @@ is_existing_type(SchemaName, Path) ->
-spec get_schema(schema_name()) -> {ok, map()} | {error, not_found}. -spec get_schema(schema_name()) -> {ok, map()} | {error, not_found}.
get_schema(SchemaName) -> get_schema(SchemaName) ->
case try
emqx_config:get( emqx_config:get(
[?CONF_KEY_ROOT, schemas, schema_name_bin_to_atom(SchemaName)], undefined [?CONF_KEY_ROOT, schemas, schema_name_bin_to_atom(SchemaName)], undefined
) )
@ -86,6 +86,9 @@ get_schema(SchemaName) ->
{error, not_found}; {error, not_found};
Config -> Config ->
{ok, Config} {ok, Config}
catch
throw:not_found ->
{error, not_found}
end. end.
-spec add_schema(schema_name(), schema()) -> ok | {error, term()}. -spec add_schema(schema_name(), schema()) -> ok | {error, term()}.
@ -340,7 +343,7 @@ to_bin(A) when is_atom(A) -> atom_to_binary(A);
to_bin(B) when is_binary(B) -> B. to_bin(B) when is_binary(B) -> B.
schema_name_bin_to_atom(Bin) when size(Bin) > 255 -> schema_name_bin_to_atom(Bin) when size(Bin) > 255 ->
erlang:throw( throw(
iolist_to_binary( iolist_to_binary(
io_lib:format( io_lib:format(
"Name is is too long." "Name is is too long."
@ -351,4 +354,9 @@ schema_name_bin_to_atom(Bin) when size(Bin) > 255 ->
) )
); );
schema_name_bin_to_atom(Bin) -> schema_name_bin_to_atom(Bin) ->
binary_to_atom(Bin, utf8). try
binary_to_existing_atom(Bin, utf8)
catch
error:badarg ->
throw(not_found)
end.

View File

@ -193,6 +193,13 @@ t_crud(Config) ->
%% no schemas at first %% no schemas at first
?assertMatch({ok, 200, []}, request(get)), ?assertMatch({ok, 200, []}, request(get)),
?assertMatch(
{ok, 404, #{
<<"code">> := <<"NOT_FOUND">>,
<<"message">> := <<"Schema not found">>
}},
request({get, <<"some_name_that_is_not_an_atom_yet">>})
),
?assertMatch( ?assertMatch(
{ok, 404, #{ {ok, 404, #{
<<"code">> := <<"NOT_FOUND">>, <<"code">> := <<"NOT_FOUND">>,