[erlang-questions] Base64 decode binaries with ets bug?

Valentino Volonghi dialtone@REDACTED
Fri Jul 13 23:38:08 CEST 2012


If you take the code at the bottom and run it you'll get the following outputs:

1> wtf:bad().
Db: [{{test,<<"aHR0cDovL2hvb3RzdWl0ZS5jb20vYy9wcm8tYWRyb2xsLWFi">>},
      <<"/usr/local/Cellar/erlang/R15B01/lib/">>}]
ok
2> wtf:good().
Value: <<"http://hootsuite.com/c/pro-adroll-ab">>
Db: [{{test,<<"aHR0cDovL2hvb3RzdWl0ZS5jb20vYy9wcm8tYWRyb2xsLWFi">>},
      <<"http://hootsuite.com/c/pro-adroll-ab">>}]
ok
3> wtf:good2().
Db: [{{test,<<"aHR0cDovL2hvb3RzdWl0ZS5jb20vYy9wcm8tYWRyb2xsLWFi">>},
      "http://hootsuite.com/c/pro-adroll-ab"}]
ok
4> wtf:good3().
Db: [{{test,<<"aHR0cDovL2hvb3RzdWl0ZS5jb20vYy9wcm8tYWRyb2xsLWFi">>},
      <<"http://hootsuite.com/c/pro-adroll-ab">>}]

What's funny is that this doesn't appear to break when you take out the last 8 characters in the base64 encoded string so:

These 2 don't work:

aHR0cDovL2hvb3RzdWl0ZS5jb20vYy9wcm8tYWRyb2xsLWFi
aHR0cDovL2hvb3RzdWl0ZS5jb20vYy9wcm8tYWRyb2xs

This works:
aHR0cDovL2hvb3RzdWl0ZS5jb20vYy9wcm8tYWRy

Are we doing something that we shouldn't or is this a bug somewhere in the VM/library/ets?


%
% Code that exposes the problem from here down.
%
-module(wtf).
-compile(export_all).

%%
%% 1) base64:decode
%% 2) ets:insert
%%
bad() ->
    Key = {test, <<"aHR0cDovL2hvb3RzdWl0ZS5jb20vYy9wcm8tYWRyb2xsLWFi">>},
    Value = base64:decode(<<"aHR0cDovL2hvb3RzdWl0ZS5jb20vYy9wcm8tYWRyb2xsLWFi">>),

    Db = ets:new(undefined, [set, protected, {read_concurrency, true}, compressed]),
    ets:insert(Db, {Key, Value}),
    io:format("Db: ~p~n", [ets:lookup(Db, Key)]).


%%
%% 1) base64:decode_to_string
%% 2) io:format
%% 3) ets:insert
%%

good() ->
    Key = {test, <<"aHR0cDovL2hvb3RzdWl0ZS5jb20vYy9wcm8tYWRyb2xsLWFi">>},
    Value = base64:decode(<<"aHR0cDovL2hvb3RzdWl0ZS5jb20vYy9wcm8tYWRyb2xsLWFi">>),

    io:format("Value: ~p~n", [Value]),

    Db = ets:new(undefined, [set, protected, {read_concurrency, true}, compressed]),
    ets:insert(Db, {Key, Value}),
    io:format("Db: ~p~n", [ets:lookup(Db, Key)]).

%%
%% 1) base64:decode_to_string
%% 2) ets:insert
%%
good2() ->
    Key = {test, <<"aHR0cDovL2hvb3RzdWl0ZS5jb20vYy9wcm8tYWRyb2xsLWFi">>},
    Value = base64:decode_to_string(<<"aHR0cDovL2hvb3RzdWl0ZS5jb20vYy9wcm8tYWRyb2xsLWFi">>),

    Db = ets:new(undefined, [set, protected, {read_concurrency, true}, compressed]),
    ets:insert(Db, {Key, Value}),
    io:format("Db: ~p~n", [ets:lookup(Db, Key)]).

%%
%% 1) base64:decode_to_string
%% 2) list_to_binary
%% 3) ets:insert
%%
good3() ->
    Key = {test, <<"aHR0cDovL2hvb3RzdWl0ZS5jb20vYy9wcm8tYWRyb2xsLWFi">>},
    Value = list_to_binary(base64:decode_to_string(<<"aHR0cDovL2hvb3RzdWl0ZS5jb20vYy9wcm8tYWRyb2xsLWFi">>)),

    Db = ets:new(undefined, [set, protected, {read_concurrency, true}, compressed]),
    ets:insert(Db, {Key, Value}),
    io:format("Db: ~p~n", [ets:lookup(Db, Key)]).



-- 
Valentino Volonghi
http://www.adroll.com




More information about the erlang-questions mailing list