Browse Source

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

fix(schema registry): avoid atom leak
Thales Macedo Garitezi 1 year ago
parent
commit
9ef3eff4c6

+ 11 - 3
apps/emqx_schema_registry/src/emqx_schema_registry.erl

@@ -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.

+ 7 - 0
apps/emqx_schema_registry/test/emqx_schema_registry_http_api_SUITE.erl

@@ -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">>,