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

CLOB Crashing #19

Open
KarlKeiser opened this issue Jul 1, 2020 · 0 comments
Open

CLOB Crashing #19

KarlKeiser opened this issue Jul 1, 2020 · 0 comments
Assignees

Comments

@KarlKeiser
Copy link
Contributor

There's a crash when fetching a CLOB in some cases, as demonstrated by following escript:

#!/user/bin/escript
%% -*- erlang -*-
%%! -pa _build/default/lib/oranif/ebin

-include_lib("eunit/include/eunit.hrl").
-define(DPI_MAJOR_VERSION, 3).
-define(DPI_MINOR_VERSION, 0).
-define(TNS, <<>>).

fetch_all(Stmt, Acc) ->
     io:format("Oassl.~n"),
    #{found := Found} = dpi:stmt_fetch(Stmt),
    io:format("Found ~p.~n", [Found]),
    case Found of true ->
        io:format("Oassl 1.~n"),
        #{data := Result, nativeTypeNum := Type} = dpi:stmt_getQueryValue(Stmt, 1),
        D1 = dpi:data_get(Result),
        io:format("Oassl 3. ~p~n", [D1]),
        fetch_all(Stmt, Acc ++ [D1]);
    %fetch_all(Stmt, Acc ++ [dpi:data_get(Result)] );
    _ -> Acc end.


main([]) ->
	dpi:load_unsafe(),
	Context = dpi:context_create(?DPI_MAJOR_VERSION, ?DPI_MINOR_VERSION),
    Conn = dpi:conn_create(
        Context, <<"foo">>, <<"bar">>, ?TNS,
        #{encoding => "AL32UTF8", nencoding => "AL32UTF8"}, #{}
    ),

    StmtD2 = dpi:conn_prepareStmt(Conn, false, <<"drop table lob_test">>, <<>>),
    catch dpi:stmt_execute(StmtD2, ['DPI_MODE_EXEC_COMMIT_ON_SUCCESS']),
    SqlCreation = <<"CREATE TABLE
                    lob_test (a clob)">>,
    StmtC = dpi:conn_prepareStmt(Conn, false, SqlCreation, <<>>),
    0 = dpi:stmt_execute(StmtC, ['DPI_MODE_EXEC_COMMIT_ON_SUCCESS']),
    SQLInsert = <<"INSERT INTO
                    lob_test
                    values (to_clob(dbms_random.string('x', 1000000)))">>,
    StmtI = dpi:conn_prepareStmt(Conn, false, SQLInsert, <<>>),
    0 = dpi:stmt_execute(StmtI, ['DPI_MODE_EXEC_COMMIT_ON_SUCCESS']),
    Sql = <<"select * from lob_test">>,

    Stmt = dpi:conn_prepareStmt(Conn, true, Sql, <<>>),

    1 = dpi:stmt_execute(Stmt, ['DPI_MODE_EXEC_COMMIT_ON_SUCCESS']),
    LobSize = 3,

    #{var := Var, data := Datas} = dpi:conn_newVar(Conn, 'DPI_ORACLE_TYPE_CLOB', 'DPI_NATIVE_TYPE_LOB', 100, LobSize, false, false, null),
    ok = dpi:stmt_define(Stmt, 1, Var), 
    #{found := Found, bufferRowIndex := BRI} = dpi:stmt_fetch(Stmt),

    D1 = dpi:data_get(lists:nth(1, Datas)),
    Bytes = dpi:lob_readBytes(D1, 1, LobSize),
    io:format("Got data: ~p~n", [binary_to_list(Bytes)]),
    %[io:format("got em ~p~n", [X]) || X <- fetch_all(Stmt, [])],
    StmtD = dpi:conn_prepareStmt(Conn, false, <<"drop table lob_test">>, <<>>),
    0 = dpi:stmt_execute(StmtD, ['DPI_MODE_EXEC_COMMIT_ON_SUCCESS']),
    io:format("Test successful.~n"),

    % cleanup
    ok = dpi:stmt_close(StmtC, <<>>),
    ok = dpi:stmt_close(Stmt, <<>>),
    ok = dpi:stmt_close(StmtI, <<>>),
    ok = dpi:stmt_close(StmtD, <<>>),
    ok = dpi:conn_close(Conn, [], <<>>),
    ok = dpi:context_destroy(Context),
    halt(1).

Usually there's a segmentation fault, but sometimes I get this error:

image

I have no idea why the context would sometimes be wrong, this is likely just memory corruption.

The issue is with the LobSize (which would be set in ddconfig). There seems to be a huge problem with mismatching encodings that can cause the size to be wrong. Warning such as this one are in multiple place in the ODPI documentation:

image

After some testing I've noticed that the actual amount of characters read is equal to the LobSize given divided by 4. So it would seem that a character is stored as four bytes, at least on my machine. The crash is caused by setting the LobSize to less than 4.

I don't know how we can portably handle this problem. We could just multiply the LobSize by 4, but that would just work on my machine, and I don't know what else my influence it. The ODPI documentation implies that the encoding in the database has to do it, so things might be different in different versions of oracle.

Perhaps for now the best solution would be to just ensure that the LobSize isn't below 4 in dderl.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants