Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Function returns an address of out of bounds

Hi,

I'm trying to assign variables where vpSec0 points.

[code]
void * vpSec0 = NULL;
CreateHVFESection0(vpSec0);
[/code]

CreateHVFESection0 function is below.

[code]
void CreateHVFESection0(void * vpSec0)
{
int hSec0;
size_t * nbytes = (size_t *) malloc(sizeof(size_t));
hSec0 = bitio_o_open();

/* 'B','U','F','R' */
bitio_o_append(hSec0,66,8);
bitio_o_append(hSec0,85,8);
bitio_o_append(hSec0,70,8);
bitio_o_append(hSec0,82,8);

/* Total length of BUFR message in bytes */
bitio_o_append(hSec0,0,24);

/* BUFR Edition Number = 4 */
bitio_o_append(hSec0,4,8);
vpSec0 = bitio_o_close(hSec0, nbytes);

free(nbytes);
nbytes = NULL;


}
[/code]

CreateHVFESection0 uses some other functions. But I think the problem is here:
[code]vpSec0 = bitio_o_close(hSec0, nbytes);[/code]
because i run the program with gdb debugger. Before coming here, vpSec0 is 0x0, means NULL. Everything is normal to here.

[code]
void *bitio_o_close (handle, nbytes)

int handle;
size_t *nbytes;

/* This function closes a output-bitstream identified by HANDLE and returns
a pointer to the memory-area holding the bit-stream.

parameters:
HANDLE: Bit-stream-handle
NBYTES: number of bytes in the bitstream.

The funcion returns a pointer to the memory-area holding the bit-stream or
NULL if an invalid handle was specified. The memory area must be freed by
the calling function.
*/

{

if (!bios[handle].used) return NULL;

/******* Fill up the last byte with 0-bits */

while (bios[handle].nbits % 8 != 0) bitio_o_append (handle, 0, 1);

*nbytes = (size_t) ((bios[handle].nbits - 1) / 8 + 1);
bios[handle].used = 0;
return (void *) bios[handle].buf;
}
void *bitio_o_close (handle, nbytes)

int handle;
size_t *nbytes;

/* This function closes a output-bitstream identified by HANDLE and returns
a pointer to the memory-area holding the bit-stream.

parameters:
HANDLE: Bit-stream-handle
NBYTES: number of bytes in the bitstream.

The funcion returns a pointer to the memory-area holding the bit-stream or
NULL if an invalid handle was specified. The memory area must be freed by
the calling function.
*/

{

if (!bios[handle].used) return NULL;

/******* Fill up the last byte with 0-bits */

while (bios[handle].nbits % 8 != 0) bitio_o_append (handle, 0, 1);

*nbytes = (size_t) ((bios[handle].nbits - 1) / 8 + 1);
bios[handle].used = 0;
return (void *) bios[handle].buf;
}

[/code]
when i step into bitio_o_close function, before [code]return (void *) bios[handle].buf[/code] , the address of bios[handle].buf is 0x2a99700930. So we expect that after returning, vpSec0's address will also be 0x2a99700930. But after returning when i print vpSec0, it's address seems 0xffffffff99700930, and this is out of bounds which falls me in Segmentation faults further in my program.

Please help.
Thanx.

Comments

  • BitByBit_ThorBitByBit_Thor Member Posts: 2,444
    : when i step into bitio_o_close function, before [code]: return (void *) bios[handle].buf[/code]: , the address of bios[handle].buf is 0x2a99700930. So we expect
    : that after returning, vpSec0's address will also be 0x2a99700930.
    : But after returning when i print vpSec0, it's address seems
    : 0xffffffff99700930, and this is out of bounds which falls me in
    : Segmentation faults further in my program.
    :

    What is returned by the function is the value of bios[handle].buf and not its address.

    Best Regards,
    Richard

    The way I see it... Well, it's all pretty blurry
  • asilter79asilter79 Member Posts: 2
    : : when i step into bitio_o_close function, before [code]: : return (void *) bios[handle].buf[/code]: : , the address of bios[handle].buf is 0x2a99700930. So we expect
    : : that after returning, vpSec0's address will also be 0x2a99700930.
    : : But after returning when i print vpSec0, it's address seems
    : : 0xffffffff99700930, and this is out of bounds which falls me in
    : : Segmentation faults further in my program.
    : :
    :
    : What is returned by the function is the value of bios[handle].buf
    : and not its address.
    :
    : Best Regards,
    : Richard
    :
    : The way I see it... Well, it's all pretty blurry

    bios is an array of type :
    [code]
    typedef struct bitio_stream { /* structure that defines a bitstrem */
    int used; /* identifier if the bitstream is used */
    char *buf; /* buffer holding the bitstream */
    long nbits; /* currend size of bitstream (counted
    in bits !) */
    size_t size; /* current size of allocated memory for
    holding the bitstream. */
    } bitio_stream;

    [/code]
    it's a char stream. Doesn't it's address return to vpSec0?



  • BitByBit_ThorBitByBit_Thor Member Posts: 2,444
    This code piece:
    [code]
    void * vpSec0 = NULL;
    CreateHVFESection0(vpSec0);
    [/code]
    Are you trying to get a value returned in vpSec0?
    After this function, *vpSec0==NULL, and vpSec0 is some address on the stack (which doesn't change before or after the function call to CreateHVFESection0).
    If you want to change WHERE vpSec0 points to, use the following function definition:
    [code]void CreateHVFESection0(void ** vpSec0)[/code]
    And at the end of the function:
    [code]
    *vpSec0 = bitio_o_close(hSec0, nbytes);
    [/code]

    Then you have to call it lile this and everything will work:
    [code]
    void * vpSec0 = NULL;
    CreateHVFESection0(&vpSec0);
    [/code]

    (Note all the subtle changes - it's either an * or & extra)

    Best Regards,
    Richard

    The way I see it... Well, it's all pretty blurry
Sign In or Register to comment.