is this allowed in c ?

C

chip

char *getdata(char *str)
{
char buffer[200];
sprintf( buffer, "We are here %s", str );
return (str);
}

Can one call this function and use the buffer returned or is this illegal ?

Regards,
-chip
 
G

gabriel

chip said:
Can one call this function and use the buffer returned or is this
illegal ?

Very, very illegal. This is one of the many ways you can blow your leg off
in C/C++.
 
T

Tom St Denis

chip said:
char *getdata(char *str)
{
char buffer[200];
sprintf( buffer, "We are here %s", str );
return (str);
}

Can one call this function and use the buffer returned or is this illegal
?

This stores in "buffer" and you're code is horribly insecure too. You could
use

char *getdata(const char *str)
{
static char buffer[200];

memset(buffer, 0, sizeof(buffer));
strncpy(buffer, "We are here ", sizeof(buffer)-1);
strncat(buffer, str, sizeof(buffer)-1);

return buffer;
}

Except this is not thread safe [e.g. two threads that call this will munge
the function return value.

Tom
 
?

=?ISO-8859-1?Q?Bj=F8rn_Augestad?=

Tom said:
char *getdata(char *str)
{
char buffer[200];
sprintf( buffer, "We are here %s", str );
return (str);
}

Can one call this function and use the buffer returned or is this illegal

?

This stores in "buffer" and you're code is horribly insecure too. You could
use

char *getdata(const char *str)
{
static char buffer[200];

memset(buffer, 0, sizeof(buffer));
Why ?
strncpy(buffer, "We are here ", sizeof(buffer)-1); Why not just use strcpy() ?
strncat(buffer, str, sizeof(buffer)-1);

Bug. What if strlen(str) > sizeof(buffer) - 1 - strlen("We are here ") ?
return buffer;
}

Except this is not thread safe [e.g. two threads that call this will munge
the function return value.



Bjørn

PS: The original version was thread safe ;-)
 
G

gabriel

Tom said:
Except this is not thread safe [e.g. two threads that call this will
munge the function return value.

Nor will it work if he does something like:

char *a = GetData("Whatever1");
char *b = GetData("Whatever2");

Then a will lose its value on the second call.

Given that the OP is asking this question, I would guess that he would go
on to make teh above successino of calls and run into trouble there.

What the OP needs to do, IMHO, is read up and fully understand how C memory
management works.
 
T

Tom St Denis

gabriel said:
Tom said:
Except this is not thread safe [e.g. two threads that call this will
munge the function return value.

Nor will it work if he does something like:

char *a = GetData("Whatever1");
char *b = GetData("Whatever2");

Then a will lose its value on the second call.

Given that the OP is asking this question, I would guess that he would go
on to make teh above successino of calls and run into trouble there.

What the OP needs to do, IMHO, is read up and fully understand how C memory
management works.

That's a big 10-4.

Tom
 
G

gabriel

Richard said:
Returning str from this function is perfectly acceptable. I'm not sure
why "gabriel" thought otherwise. Returning buffer from this function
would very definitely not be acceptable, since you'd be returning a
pointer to an automatic object which would no longer exist by the time
that pointer value could be inspected.

I thoroughly apologize, I read too fast. I could have sworn I saw return
(buffer).
 
G

gabriel

gabriel said:
Very, very illegal. This is one of the many ways you can blow your
leg off in C/C++.

Disregard this reply from me. I'm an idiot and did not read your code
correctly.
 
D

Dan Pop

In said:
char *getdata(char *str)
{
char buffer[200];
sprintf( buffer, "We are here %s", str );
return (str);
}

Can one call this function and use the buffer returned or is this illegal ?

Nothing illegal here, provided that the input string is small enough not
to cause a buffer overrun. But your function is pefectly useless, because
the buffer goes aways as soon as you return from it.

If you intended to return buffer instead of str, the caller would get
an indeterminate and useless pointer value, because the lifetime of the
automatically allocated buffer expires at the end of the function.

A working version would be:

char *getdata(char *str)
{
static char buffer[200];
sprintf(buffer, "We are here %s", str);
return buffer;
}

but this has its specific caveats and has to be carefully used. Consider:

printf("%s ||| %s\n", getdata("foo"), getdata("bar"));

Dan
 
P

Peter Slootweg

chip said:
char *getdata(char *str)
{
char buffer[200];
sprintf( buffer, "We are here %s", str );
return (str);

you are returning str
}

Can one call this function and use the buffer returned or is this illegal ?

as is - yes it is legal to use the return value of the function - assuming
it was legal to use the value passed in as str

now, if you meant to return buffer, then yes it is 'illegal' to use the
return value - after returning from getdata.
 
R

Richard Heathfield

chip said:
char *getdata(char *str)
{
char buffer[200];
sprintf( buffer, "We are here %s", str );
return (str);
}

Can one call this function and use the buffer returned or is this illegal
?

Returning str from this function is perfectly acceptable. I'm not sure why
"gabriel" thought otherwise. Returning buffer from this function would very
definitely not be acceptable, since you'd be returning a pointer to an
automatic object which would no longer exist by the time that pointer value
could be inspected.

Incidentally, your code doesn't check that the data you provide will fit in
the buffer.
 
N

nrk

Tom said:
chip said:
char *getdata(char *str)
{
char buffer[200];
sprintf( buffer, "We are here %s", str );
return (str);
}

Can one call this function and use the buffer returned or is this illegal
?

This stores in "buffer" and you're code is horribly insecure too. You
could use

char *getdata(const char *str)
{
static char buffer[200];

memset(buffer, 0, sizeof(buffer));
strncpy(buffer, "We are here ", sizeof(buffer)-1);
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
strncat(buffer, str, sizeof(buffer)-1);
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

And your code is super-secure?
return buffer;
}

Except this is not thread safe [e.g. two threads that call this will munge
the function return value.

It is braindead and useless as well. (Taste of your own medicine. If it
doesn't kill you, it might make you stronger).

-nrk.
 
T

Tom St Denis

nrk said:
Tom said:
chip said:
char *getdata(char *str)
{
char buffer[200];
sprintf( buffer, "We are here %s", str );
return (str);
}

Can one call this function and use the buffer returned or is this
illegal
?

This stores in "buffer" and you're code is horribly insecure too. You
could use

char *getdata(const char *str)
{
static char buffer[200];

memset(buffer, 0, sizeof(buffer));
strncpy(buffer, "We are here ", sizeof(buffer)-1); ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
strncat(buffer, str, sizeof(buffer)-1);
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

And your code is super-secure?

What is wrong with my function calls? I only allow upto size-1 bytes and I
memset before [all nulls]. As far as I can tell you can't overflow buffer
unless you have a buggy libc.

However, if I did make a real mistake please share it.

Tom
 
T

Tom St Denis

Tom St Denis said:
nrk said:
Tom said:
char *getdata(char *str)
{
char buffer[200];
sprintf( buffer, "We are here %s", str );
return (str);
}

Can one call this function and use the buffer returned or is this illegal
?

This stores in "buffer" and you're code is horribly insecure too. You
could use

char *getdata(const char *str)
{
static char buffer[200];

memset(buffer, 0, sizeof(buffer));
strncpy(buffer, "We are here ", sizeof(buffer)-1); ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
strncat(buffer, str, sizeof(buffer)-1);
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

And your code is super-secure?

What is wrong with my function calls? I only allow upto size-1 bytes and I
memset before [all nulls]. As far as I can tell you can't overflow buffer
unless you have a buggy libc.

However, if I did make a real mistake please share it.

Foot mouth...

sizeof(buffer) - strlen(buffer) - 1

should be the right argument.

Teach me to post during an opsys class..

Tom

[P.S. Sorry peeps.]
 
C

CBFalconer

Richard said:
chip said:
char *getdata(char *str)
{
char buffer[200];
sprintf( buffer, "We are here %s", str );
return (str);
}

Returning str from this function is perfectly acceptable. I'm not
sure why "gabriel" thought otherwise. Returning buffer from this
function would very definitely not be acceptable, since you'd be
returning a pointer to an automatic object which would no longer
exist by the time that pointer value could be inspected.

Incidentally, your code doesn't check that the data you provide
will fit in the buffer.

AFAICS the code serves two purposes:

1. To excite Tom into a mouth frothing frenzy.
2. To function as a NOP until strlen(str) exceeds roughly 200,
and then to serve as an exploitable security leak. Notice the sly
way it returns the base address of the injected code. In normal
use it could be called "validate" and likely escape peer review in
usage. A macro in the source file of the form "#define getdata
validate" will look innocuous (sp?) and further obscure the
interactions.

Suggested usage:

if (!validate(somestring)) {
/* code only reached for somestring==NULL */
}

Color me Machiavellian.
 
K

Keith Thompson

chip said:
char *getdata(char *str)
{
char buffer[200];
sprintf( buffer, "We are here %s", str );
return (str);
}

Can one call this function and use the buffer returned or is this illegal ?

Note that "the buffer returned" is not the object declared as
"buffer". I think that's the wording that confused a lot of people.

It looks ok as long as str is a valid pointer to a string that isn't
too long.

I presume this is a fragment of a larger function that actually does
something useful. As it is, you copy characters into "buffer", but
you never refer to it. The declaration of "buffer" and the call to
sprintf could be deleted without affecting the visible behavior of the
function (other than avoiding undefined behavior in some cases).

(A very minor point: the parentheses on the return statement are
harmless but unnecessary.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,135
Messages
2,570,783
Members
47,341
Latest member
hanifree

Latest Threads

Top