From: nani on
Using secured version of 'strcpy()' I need to use 'strcpy_s()' version
and there lies my problem. In my function I need to copy the result in
a char type which is passed to function. I pass a 'char addr[256]' but
on my final step function:
'strcpy_s(addr, sizeof(addr), result)'
the sizeof operator returns '4' and function chrases. The value of
result is lets say an IP address (11 characters). What can I use
instead 'sizeof' operater. I cant just use 256 because I use
'myfunction' in few places and passing different kind of char arrays.
Sorry for english and thanks in advance
From: David Wilkinson on
nani wrote:
> Using secured version of 'strcpy()' I need to use 'strcpy_s()' version
> and there lies my problem. In my function I need to copy the result in
> a char type which is passed to function. I pass a 'char addr[256]' but
> on my final step function:
> 'strcpy_s(addr, sizeof(addr), result)'
> the sizeof operator returns '4' and function chrases. The value of
> result is lets say an IP address (11 characters). What can I use
> instead 'sizeof' operater. I cant just use 256 because I use
> 'myfunction' in few places and passing different kind of char arrays.
> Sorry for english and thanks in advance

An array decays to a char* pointer when passed to a function. That is why you
see the size as 4.

Your 'my function' needs to receive the size as a parameter. Or use std::string&
as parameter instead of char*.

--
David Wilkinson
Visual C++ MVP
From: Joseph M. Newcomer on
See below...
On Fri, 5 Mar 2010 01:32:03 -0800 (PST), nani <cagalj.josip(a)gmail.com> wrote:

>Using secured version of 'strcpy()' I need to use 'strcpy_s()' version
>and there lies my problem. In my function I need to copy the result in
>a char type which is passed to function. I pass a 'char addr[256]' but
>on my final step function:
>'strcpy_s(addr, sizeof(addr), result)'
>the sizeof operator returns '4' and function chrases. The value of
>result is lets say an IP address (11 characters). What can I use
>instead 'sizeof' operater. I cant just use 256 because I use
>'myfunction' in few places and passing different kind of char arrays.
****
You must *always* pass in the size of the array, explicitly, as a parameter to the
function! In fact, failure to do so can get you fired in certain companies today, because
you would be violating secure coding practice. More below.
****
>Sorry for english and thanks in advance
****
Well, it did exactly what you asked, in exactly the correct way. If you pass a pointer to
a buffer into a function, you must pass in the length explicitly (in fact, the implication
that you "know" the length is a source of many errors). In modern practice, the ONLY
correct way to pass a pointer to something to a subroutine that can fill in using strcpy,
memcpy, etc. is to EXPLICITLY pass in the length:

void DoCopy(char * p, size_t n, const char * value)
{
... bunch of stuff
strcpy_s(p, n, value);
...other stuff
}

You would call it as
char addr[82];

DoCopy(addr, sizeof(addr), result);

Your use of "sizeof" in *your* code says, explicitly, "the size of the object passed in is
the size of a pointer". The fact that the pointer you used for the sizeof just happens to
be the name of the parameter is irrelevant; you could have the same effect by having
written
strcpy_s(addr, sizeof(LPVOID), result);

You can only use sizeof in contexts in which the compiler can know the size. It cannot
know the size of a value pointed to by a parameter; it can only know the size of a
variable based on its declaration. A pointer is implicitly declared to have a certain
size. There isn't even a way for the compiler to determine if the parameter points to a
character array, or something else, so there is no way it can figure out the size.
sizeof() is evaluated entirely at compile time. So it did exactly what you asked it to
do, it copy no more than sizeof(addr) characters, and sizeof(addr), given that addr is a
char *, is 4 in Win32 (and 8 in Win64).

Now, all that said, why are you
(a) using fixed-sized buffers to do a copy of a string?
(b) using the obsolete data type 'char'?
(c) not writing Unicode-aware code?

You should not use the 'char' data type except in rare and exotic circumstances, or when
you are first bringing data into a program (such as using UTF-8 encoding) or writing it
out. All code should be written as if it is in Unicode, or could be. So the data type
should be TCHAR, and the function should be _tcscpy_s.

In addition, using a fixed-size buffer is probably a mistake, nearly all the time. You
should be using a CString. If I had to copy a value into a buffer, I would have written
the code as
void DoCopy(CString & dest, const CString & src)
{
... stuff
dest = src;
...
}

I am at the point where if I write a strcpy more than once a year or so, I'm doing
something really odd and exotic. You should never think of char addr[256] as a natural
way to express anything unless there is a *really* compelling reason for doing so (and
there rarely is!). So why do you think you need a char array at all?
joe
****
Joseph M. Newcomer [MVP]
email: newcomer(a)flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
From: David Ching on
"nani" <cagalj.josip(a)gmail.com> wrote in message
news:6c60489f-7e30-49d2-a1a7-741a0b5e83ee(a)q16g2000yqq.googlegroups.com...
> Using secured version of 'strcpy()' I need to use 'strcpy_s()' version
> and there lies my problem. In my function I need to copy the result in
> I pass a 'char addr[256]' but
> on my final step function:
> 'strcpy_s(addr, sizeof(addr), result)'
> the sizeof operator returns '4'

Are you sure? Last I checked:

char addr[256];
assert (sizeof(addr) == 256);

would not assert.

-- David

From: Joseph M. Newcomer on
But consider:

char addr[256];
DoSomething(addr);

void DoSomething(char * addr)
{
ASSERT(sizeof(addr) == 256); // FAILS
ASSERT(sizeof(addr) == sizeof(DWORD_PTR)); // succeeds
ASSERT(sizeof(addr) == sizeof(LPVOID)); // succeeds
}

Note that the example tries to use sizeof in the called function as if it had meaning to
deliver the size of the buffer being pointed to, which, of course, it cannot possibly do!

The only correct solution is

char addr[256];

DoSomething(addr, sizeof(addr));

void DoSomething(char * addr, size_t len)
{
...
}

joe

On Fri, 5 Mar 2010 11:12:50 -0800, "David Ching" <dc(a)remove-this.dcsoft.com> wrote:

>"nani" <cagalj.josip(a)gmail.com> wrote in message
>news:6c60489f-7e30-49d2-a1a7-741a0b5e83ee(a)q16g2000yqq.googlegroups.com...
>> Using secured version of 'strcpy()' I need to use 'strcpy_s()' version
>> and there lies my problem. In my function I need to copy the result in
>> I pass a 'char addr[256]' but
>> on my final step function:
>> 'strcpy_s(addr, sizeof(addr), result)'
>> the sizeof operator returns '4'
>
>Are you sure? Last I checked:
>
> char addr[256];
> assert (sizeof(addr) == 256);
>
>would not assert.
>
>-- David
Joseph M. Newcomer [MVP]
email: newcomer(a)flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm