Strange resource manager problem

QNX 6.2NC

In the resource manager io_read() below it works fine if I leave the “int i”
declaration at the start, which is an unused variable, but exits (i.e.
returns -1 to the read() call) if the declaration is removed. The code is
basically an exact copy of what is in the docs/Rob Krten’s book but I cannot
see what I have done wrong. I suspect that the output_data pointer is being
used to overwrite the stack variables and the “int i” provides 4 bytes of
protection. Can anyone tell me what my newbie mistake is?


//**************************************************************************


int io_read(resmgr_context_t *ctp, io_read_t *msg, RESMGR_OCB_T *ocb)
{
int nleft;
int nbytes;
int nparts;
int status;
int i;
char *output_data;

if ((status = iofunc_read_verify(ctp, msg, ocb, NULL)) != EOK)
return status;

if (msg->i.xtype & _IO_XTYPE_MASK != _IO_XTYPE_NONE)
return(ENOSYS);

// Fill buffer with data
sprintf(output_data, “%f\n”, (float) data);
ocb->attr->nbytes = strlen(output_data)+1;

nleft = ocb->attr->nbytes - ocb->offset;
nbytes = min (msg->i.nbytes, nleft);

if (nbytes > 0)
{
SETIOV (ctp->iov, output_data + ocb->offset, nbytes);

_IO_SET_READ_NBYTES (ctp, nbytes);

if (nbytes < strlen(output_data))
ocb->offset += nbytes;
else
ocb->offset = 0;

nparts = 1;
}
else
{
_IO_SET_READ_NBYTES (ctp, 0);

nparts = 0;
}

if (msg->i.nbytes > 0)
ocb->attr->flags |= IOFUNC_ATTR_ATIME;

return (_RESMGR_NPARTS (nparts));
}
//**************************************************************************



\

Paul.

Paul Jones <paul.jones@bnc.ox.ac.uk> wrote:
: // Fill buffer with data
: sprintf(output_data, “%f\n”, (float) data);

Your output_data variable needs to be an array of characters, not a char *,
for this line to work properly.


Steve Reid stever@qnx.com
TechPubs (Technical Publications)
QNX Software Systems

Or allocate some memory for this char * to point to, before you try write
date to it.
output_data = malloc(some_size * sizeof(char) );

See malloc in the helpviewer for more info.


Regards,

Joe

“Steve Reid” <stever@qnx.com> wrote in message
news:ak60mj$4mf$1@nntp.qnx.com

Paul Jones <> paul.jones@bnc.ox.ac.uk> > wrote:
: // Fill buffer with data
: sprintf(output_data, “%f\n”, (float) data);

Your output_data variable needs to be an array of characters, not a char
*,
for this line to work properly.


Steve Reid > stever@qnx.com
TechPubs (Technical Publications)
QNX Software Systems

Hardware Support <hw@qnx.com> wrote in message
news:ak623r$71j$1@nntp.qnx.com

Or allocate some memory for this char * to point to, before you try write
date to it.
output_data = malloc(some_size * sizeof(char) );

sizeof(char) is guaranteed to be 1. You’d be better of to generalize as:


#include <stdlib.h>

char* output_data;

output_data = malloc( units * sizeof(*output_data));

Cheers,
Adam

QNX Software Systems Ltd.
[ amallory@qnx.com ]

With a PC, I always felt limited by the software available.
On Unix, I am limited only by my knowledge.
–Peter J. Schoenster <pschon@baste.magibox.net>

I’m sure you’ve found out by now that the following lines were using
output_data while it was uninitialized.

char *output_data;
sprintf(output_data, “%f\n”, (float) data);

If you call gcc with ‘-O2 -Wall’ switches enabled it will catch this sort of
mistake. Don’t ignore your compiler warnings!

Cheers,
Shaun


In the resource manager io_read() below it works fine if I leave the “int
i”
declaration at the start, which is an unused variable, but exits (i.e.
returns -1 to the read() call) if the declaration is removed. The code is
basically an exact copy of what is in the docs/Rob Krten’s book but I
cannot
see what I have done wrong. I suspect that the output_data pointer is
being
used to overwrite the stack variables and the “int i” provides 4 bytes of
protection. Can anyone tell me what my newbie mistake is?