GithubHelp home page GithubHelp logo

Comments (10)

vrogier avatar vrogier commented on July 30, 2024

Hi,

Can you indicate the ocilib version , the oracle version and the platform (32/64bits) ?

The following code (taken from you) give the expected results on Win32/Oracle 12c:

int main()
{
    OCI_Connection* cn;
    OCI_Statement* st;
    OCI_Resultset* rs;

    int i;

    OCI_Initialize(err_handler, NULL, OCI_ENV_DEFAULT);

    cn = OCI_ConnectionCreate("db12c", "usr", "pwd", OCI_SESSION_DEFAULT);
    st = OCI_StatementCreate(cn);

    OCI_ExecuteStmtFmt(st, OTEXT("select val_char_1, val_char_2, val_double, val_long, val_char_3 from test_fetch_struct2"));

    rs = OCI_GetResultset(st);

    OCI_SetStructNumericType(rs, 3, OCI_NUM_DOUBLE);
    OCI_SetStructNumericType(rs, 4, OCI_NUM_INT);

    i = 0;
    while (OCI_FetchNext(rs))
    {
        prd.val_char_1 = 0;
        prd.val_char_2 = 0;
        prd.val_double = 0.0;
        prd.val_long = 0;
        prd.val_char_3 = 0;
        prd.extra = 0;

        OCI_GetStruct(rs, &prd, &ind);
        i++;

        printf("\nrow %d\n", i);
        printf("  > val_char_1 : %08X: %s\n", prd.val_char_1, prd.val_char_1);
        printf("  > val_char_2 : %08X: %s\n", prd.val_char_2, prd.val_char_2);
        printf("  > val_double : %Lf\n", prd.val_double);
        printf("  > val_long : %d\n", prd.val_long);
        printf("  > val_char_3 : %08X %s\n", prd.val_char_3, prd.val_char_3);
        printf("  > extra      : %08X: %s\n", prd.extra, prd.extra);
        printf("\n");
    }

    OCI_Cleanup();

    return EXIT_SUCCESS;
}

the output is :

row 1

val_char_1 : 00203670: col1 row1
val_char_2 : 00203A60: col2 row1
val_double : 123.450000
val_long : 1
val_char_3 : 002044D0 col5 row1
extra : 00000000: (null)

row 2

val_char_1 : 0020368F: col1 row2
val_char_2 : 00203A7F: col2 row2
val_double : 234.560000
val_long : 2
val_char_3 : 002044EF col5 row2
extra : 00000000: (null)

row 3

val_char_1 : 002036AE: col1 row3
val_char_2 : 00203A9E: col2 row3
val_double : 345.670000
val_long : 3
val_char_3 : 0020450E col5 row3
extra : 00000000: (null)

Vincent

from ocilib.

JamesL2 avatar JamesL2 commented on July 30, 2024

Changelog.txt: 2015-05-05 Version 4.1.0 Vincent Rogier [email protected]

Using Windows, lib32, ociliba.dll, Oracle 11.2g

#define OCI_CHARSET_ANSI
#define OCI_IMPORT_RUNTIME

from ocilib.

vrogier avatar vrogier commented on July 30, 2024

Hi,

Can you try the code I've posted and tell me what results you got ?

Thanks

from ocilib.

JamesL2 avatar JamesL2 commented on July 30, 2024

Posted code modified to:
include structure definitions;
create, populate, and delete the oracle table using functions previously posted;
accept connection parameters;
split printing prd.val_char_3 into two statements in order to see the pointer value before attempting to use it.


output results:

  • test cleanup...
  • test setup...

Execute: create table test_fetch_struct2(val_char_1 varchar2(30),val_char_2 varchar2(30),val_double number,val_long number(10),val_char_3 varchar2(30))

Execute: insert into test_fetch_struct2 (val_char_1, val_char_2, val_double, val_long, val_char_3) values ('col1 row1', 'col2 row1', 123.45, 1, 'col5 row1')

Execute: insert into test_fetch_struct2 (val_char_1, val_char_2, val_double, val_long, val_char_3) values ('col1 row1', 'col2 row2', 234.56, 2, 'col5 row2')

Execute: insert into test_fetch_struct2 (val_char_1, val_char_2, val_double, val_long, val_char_3) values ('col1 row1', 'col2 row3', 345.67, 3, 'col5 row3')

row 1

val_char_1 : 009C7FE8: col1 row1
val_char_2 : 009C89C8: col2 row1
val_double : -92559641157289301000000000000000000000000000000000000000000000.000000
val_long : 1079958732
val_char_3 : 00000001

A segment fault (signal 11) occurs attempting to print the string referenced by prd.val_char_3.


int main(int argc, oarg* argv[])
{
OCI_Connection* cn = 0;
OCI_Statement* st = 0;
OCI_Resultset* rs = 0;

int i;

// define the structure expected for the retrieval
struct product_t
{
char *val_char_1; // offset 0
char *val_char_2; // offset 4
double val_double; // offset 8 :: column is natually aligned - no adjustment needed
int val_long; // offset 16
char *val_char_3; // offset 20
char *extra; // offset 24 :: don't abend on buffer overflow
} prd;

struct prodind_t
{
    boolean   val_char_1;
    boolean   val_char_2;
    boolean   val_double;
    boolean   val_long;
    boolean   val_char_3;
} ind;

orauser[0] = '\0';
orapswd[0] = '\0';
dbalias[0] = '\0';
orarole[0] = '\0';

GET_ARG(orauser, ARG_USER);
GET_ARG(orapswd, ARG_PWD);
GET_ARG(dbalias, ARG_ALIAS);

OCI_Initialize(err_handler, NULL, OCI_ENV_DEFAULT);

// cn = OCI_ConnectionCreate("db12c", "usr", "pwd", OCI_SESSION_DEFAULT);
if ((cn = OCI_ConnectionCreate(dbalias, orauser, orapswd, OCI_SESSION_DEFAULT)) == NULL)
{
return(1);
}
if ((st = OCI_StatementCreate(cn)) == NULL)
{
return(1);
}

test_cleanup(cn, st, rs);
test_setup(cn, st, rs);

OCI_ExecuteStmtFmt(st, OTEXT("select val_char_1, val_char_2, val_double, val_long, val_char_3 from test_fetch_struct2"));

rs = OCI_GetResultset(st);

OCI_SetStructNumericType(rs, 3, OCI_NUM_DOUBLE);
OCI_SetStructNumericType(rs, 4, OCI_NUM_INT);

i = 0;
while (OCI_FetchNext(rs))
{
    prd.val_char_1 = 0;
    prd.val_char_2 = 0;
    prd.val_double = 0.0;
    prd.val_long = 0;
    prd.val_char_3 = 0;
    prd.extra = 0;

    OCI_GetStruct(rs, &prd, &ind);
    i++;

    printf("\nrow %d\n", i);
    printf("  > val_char_1 : %08X: %s\n", prd.val_char_1, prd.val_char_1);
    printf("  > val_char_2 : %08X: %s\n", prd.val_char_2, prd.val_char_2);
    printf("  > val_double : %Lf\n", prd.val_double);
    printf("  > val_long : %d\n", prd.val_long);
    printf("  > val_char_3 : %08X", prd.val_char_3);
    printf("%s\n", prd.val_char_3);
    printf("  > extra      : %08X: %s\n", prd.extra, prd.extra);
    printf("\n");
}

test_cleanup(cn, st, rs);

OCI_Cleanup();

return EXIT_SUCCESS;

}

from ocilib.

vrogier avatar vrogier commented on July 30, 2024

What compiler are you using ?

from ocilib.

JamesL2 avatar JamesL2 commented on July 30, 2024

MSVC 2010 generating 32bit executable.
Compiling & executing computer is Windows 7x64 professional & enterprise edition.

from ocilib.

JamesL2 avatar JamesL2 commented on July 30, 2024

Print statements added before the fetch loop:

#define PTR_DIFF(ptr1,ptr2) ((long)ptr1 - (long)ptr2)
printf("\nprd column offsets\n");
printf("  > val_char_1 : %08X, %2d\n", &prd.val_char_1, PTR_DIFF(&prd.val_char_1, &prd));
printf("  > val_char_2 : %08X, %2d\n", &prd.val_char_2, PTR_DIFF(&prd.val_char_2, &prd));
printf("  > val_double : %08X, %2d\n", &prd.val_double, PTR_DIFF(&prd.val_double, &prd));
printf("  > val_long   : %08X, %2d\n", &prd.val_long,   PTR_DIFF(&prd.val_long,   &prd));
printf("  > val_char_3 : %08X, %2d\n", &prd.val_char_3, PTR_DIFF(&prd.val_char_3, &prd));
printf("  > extra      : %08X, %2d\n", &prd.extra,      PTR_DIFF(&prd.extra,      &prd));

produces:

prd column offsets
  > val_char_1 : 002EF898,  0
  > val_char_2 : 002EF89C,  4
  > val_double : 002EF8A0,  8
  > val_long   : 002EF8A8, 16
  > val_char_3 : 002EF8AC, 20
  > extra      : 002EF8B0, 24

Print statements added to the OCI_GetStruct function (around the existing ROUNDUP logic):

printf("\nOCI_GetStruct: \n");
:
:
printf("\ti=%3d ptr=%08X size1=%3d align1=%2d size2=%3d align2=%2d", (int)i, ptr, (int)size1, (int)align1, (int)size2, (int)align2);
if (size2 > 0)
{
    size1 = ROUNDUP(size1, align2);
    printf(" size1 roundup: %d", (int)size1);
}
printf("\n");

produces the following, where the 2nd roundup should be 4 instead of 8:

OCI_GetStruct:
    i=  1 ptr=002EF898 size1=  4 align1= 4 size2=  4 align2= 4 size1 roundup: 4
    i=  2 ptr=002EF89C size1=  4 align1= 4 size2=  8 align2= 8 size1 roundup: 8
    i=  3 ptr=002EF8A4 size1=  8 align1= 8 size2=  4 align2= 4 size1 roundup: 8
    i=  4 ptr=002EF8AC size1=  4 align1= 4 size2=  4 align2= 4 size1 roundup: 4
    i=  5 ptr=002EF8B0 size1=  4 align1= 4 size2=  0 align2= 4

from ocilib.

vrogier avatar vrogier commented on July 30, 2024

Hi,

You were right indeed. My test and posted code were done using an 64bits build of OCILIB by mistake.
Using a 32bits build, i got the same issues than you.
I've committed a fix for it.
Now on both 32bits and 64bits platforms, results are correct !

Thanks :)

Regards,

Vincent

from ocilib.

vrogier avatar vrogier commented on July 30, 2024

Hi,

Is that alright fro you ?

Thanks

Vincent

from ocilib.

JamesL2 avatar JamesL2 commented on July 30, 2024

A fix to the issue is good.
Thank you.

While I am not using these functions, I did notice the same logic is used in the OCI_ObjectGetStructSize and OCI_ObjectGetUserStructSize functions, in case they also need an update.

from ocilib.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.