473,379 Members | 1,335 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

Strcpy_s and strcpy

Hi,

I have changed my strcpy to strcpy_s for 2005 project. It's fairly
big project and was using strycpy lot of places.
The program started corrupting the stack and in turn crashing the
application. We have realized that it is due to strcpy_s. We have
changes that to strpcy and then it was fine.
There are some places the destlength was more then whatever size of
deststr. I know it is a mistake but the copy string had character to
copy. So I was thinking it shouldn't crash the project. Isn't that
true?

Example:
deststr[128];
copystr[] = "Test String";
destlength = 256;

strcpy_s(deststr, destlength, copystr);
even strcpy_s(deststr, strlen(copystr), copystr); was crashing (I need
to look more for this)

But is there any known problem with strcpy_s?
Thanks
Trupti
Jul 2 '08 #1
7 26861
On Jul 2, 8:53 am, "Samant.Tru...@gmail.com" <Samant.Tru...@gmail.com>
wrote:
Hi,

I have changed my strcpy to strcpy_s for 2005 project. It's fairly
big project and was using strycpy lot of places.
The program started corrupting the stack and in turn crashing the
application. We have realized that it is due to strcpy_s. We have
changes that to strpcy and then it was fine.
There are some places the destlength was more then whatever size of
deststr. I know it is a mistake but the copy string had character to
copy. So I was thinking it shouldn't crash the project. Isn't that
true?

Example:
deststr[128];
copystr[] = "Test String";
destlength = 256;

strcpy_s(deststr, destlength, copystr);
even strcpy_s(deststr, strlen(copystr), copystr); was crashing (I need
to look more for this)

But is there any known problem with strcpy_s?
Thanks
Trupti
Hello,

Perhaps I misunderstand the question, but in any case, 'destlength'
should refer to the allocated size of the destination buffer, and
should not be larger, otherwise memory corruption may occur.

Also, I believe that 'strcpy_s' is not yet part of the standard, so a
Microsoft forum could be more appropriate for discussing this issue.

Regards.
Jul 2 '08 #2
<Sa***********@gmail.comwrote in message
news:5f**********************************@34g2000h sf.googlegroups.com...
Hi,

I have changed my strcpy to strcpy_s for 2005 project. It's fairly
big project and was using strycpy lot of places.
The program started corrupting the stack and in turn crashing the
application. We have realized that it is due to strcpy_s. We have
changes that to strpcy and then it was fine.
There are some places the destlength was more then whatever size of
deststr. I know it is a mistake but the copy string had character to
copy. So I was thinking it shouldn't crash the project. Isn't that
true?

Example:
deststr[128];
copystr[] = "Test String";
destlength = 256;

strcpy_s(deststr, destlength, copystr);
even strcpy_s(deststr, strlen(copystr), copystr); was crashing (I need
to look more for this)

But is there any known problem with strcpy_s?
A test program shows that strcpy_s is doing some nasty business at least in
debug. Output of the following program is:

XXXXXXXXXXX
58 58 58 58 58 58 58 58 58 58 58 0
Copy
43 6f 70 79 0 58 58 58 58 58 58 0
Copy
43 6f 70 79 0 fe fe fe fe fe fe fe

In Release it is differnent:

XXXXXXXXXXX
58 58 58 58 58 58 58 58 58 58 58 0
Copy
43 6f 70 79 0 58 58 58 58 58 58 0
Copy
43 6f 70 79 0 58 58 58 58 58 58 0

#include <iostream>

void ClearMemory( char* Memory, size_t Size )
{
memset( Memory, 'X', Size - 1 );
Memory[Size - 1] = '\0';
}

void DispMemory( char* Memory, size_t Size )
{
std::cout << Memory << "\n";
for ( size_t i = 0; i < Size; ++i )
std::cout << std::hex << (unsigned int)(unsigned char)Memory[i] << "
";
std::cout << "\n";
}

int main()
{
char Buffer[12];
char String[] = "Copy";

ClearMemory( Buffer, sizeof( Buffer ) );
DispMemory( Buffer, sizeof( Buffer ) );

strcpy( Buffer, String );
DispMemory( Buffer, sizeof( Buffer ) );

ClearMemory( Buffer, sizeof( Buffer ) );
strcpy_s( Buffer, sizeof( Buffer ), String );
DispMemory( Buffer, sizeof( Buffer ) );
}
Jul 2 '08 #3
Sa***********@gmail.com wrote:
I have changed my strcpy to strcpy_s for 2005 project. It's fairly
big project and was using strycpy lot of places.
The program started corrupting the stack and in turn crashing the
application. We have realized that it is due to strcpy_s. We have
changes that to strpcy and then it was fine.
It is "fine" as long as your source string is short enough not to
overwrite the falsely advertised target buffer. The day when you're
presenting the application to your biggest potential customer, the
string will happen be larger, overrun the buffer and lead to random
erratic behavior of your application. Or it will create a target vector
for the infamous "buffer overrun" types of security attacks. No longer
so fine.

strcpy_s has helped you in identifying the problem. That's what it's
supposed to do. In my book, that's a good thing.
Jul 2 '08 #4
On Jul 2, 9:51 am, alasham.s...@gmail.com wrote:
On Jul 2, 8:53 am, "Samant.Tru...@gmail.com" <Samant.Tru...@gmail.com>
wrote:
I have changed my strcpy to strcpy_s for 2005 project. It's
fairly big project and was using strycpy lot of places. The
program started corrupting the stack and in turn crashing
the application. We have realized that it is due to
strcpy_s. We have changes that to strpcy and then it was
fine. There are some places the destlength was more then
whatever size of deststr. I know it is a mistake but the
copy string had character to copy. So I was thinking it
shouldn't crash the project. Isn't that true?
Example:
deststr[128];
copystr[] = "Test String";
destlength = 256;
strcpy_s(deststr, destlength, copystr);
even strcpy_s(deststr, strlen(copystr), copystr); was crashing (I need
to look more for this)
But is there any known problem with strcpy_s?
Perhaps I misunderstand the question, but in any case,
'destlength' should refer to the allocated size of the
destination buffer, and should not be larger, otherwise memory
corruption may occur.
Also, I believe that 'strcpy_s' is not yet part of the
standard, so a Microsoft forum could be more appropriate for
discussing this issue.
It's part of a TR (or something similar) for C. I doubt that it
will ever be part of C++, since we already have much better and
safer tools (std::string).

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jul 2 '08 #5
On Jul 2, 10:41 am, "Jim Langston" <tazmas...@rocketmail.comwrote:
<Samant.Tru...@gmail.comwrote in message
news:5f**********************************@34g2000h sf.googlegroups.com...
I have changed my strcpy to strcpy_s for 2005 project. It's
fairly big project and was using strycpy lot of places. The
program started corrupting the stack and in turn crashing
the application. We have realized that it is due to
strcpy_s. We have changes that to strpcy and then it was
fine.
For the moment.
There are some places the destlength was more then whatever
size of deststr. I know it is a mistake but the copy string
had character to copy. So I was thinking it shouldn't crash
the project. Isn't that true?
Garbage in, garbage out. If you lie to the function, you can't
expect it to behave normally.
Example:
deststr[128];
copystr[] = "Test String";
destlength = 256;
strcpy_s(deststr, destlength, copystr);
even strcpy_s(deststr, strlen(copystr), copystr); was crashing (I need
to look more for this)
But is there any known problem with strcpy_s?
A test program shows that strcpy_s is doing some nasty
business at least in debug. Output of the following program
is:
XXXXXXXXXXX
58 58 58 58 58 58 58 58 58 58 58 0
Copy
43 6f 70 79 0 58 58 58 58 58 58 0
Copy
43 6f 70 79 0 fe fe fe fe fe fe fe
In Release it is differnent:
XXXXXXXXXXX
58 58 58 58 58 58 58 58 58 58 58 0
Copy
43 6f 70 79 0 58 58 58 58 58 58 0
Copy
43 6f 70 79 0 58 58 58 58 58 58 0
#include <iostream>

void ClearMemory( char* Memory, size_t Size )
{
memset( Memory, 'X', Size - 1 );
Memory[Size - 1] = '\0';
}

void DispMemory( char* Memory, size_t Size )
{
std::cout << Memory << "\n";
for ( size_t i = 0; i < Size; ++i )
std::cout << std::hex << (unsigned int)(unsigned char)Memory[i] << "
";
std::cout << "\n";
}
int main()
{
char Buffer[12];
char String[] = "Copy";
ClearMemory( Buffer, sizeof( Buffer ) );
DispMemory( Buffer, sizeof( Buffer ) );
strcpy( Buffer, String );
DispMemory( Buffer, sizeof( Buffer ) );
ClearMemory( Buffer, sizeof( Buffer ) );
strcpy_s( Buffer, sizeof( Buffer ), String );
DispMemory( Buffer, sizeof( Buffer ) );
}
Looks fine to me. I'd consider this a feature, if it helps
detect errors like passing the wrong length.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jul 2 '08 #6
This indeed a feature. Wanted to understand how it's done.
I got my doubt clear
Thanks
Trupti

Jul 2 '08 #7
On 2 Jul, 07:53, "Samant.Tru...@gmail.com" <Samant.Tru...@gmail.com>
wrote:
Hi,

* I have changed my strcpy to strcpy_s for 2005 project. *It's fairly
big project and was using strycpy lot of places.
The program started corrupting the stack and in turn crashing the
application. *We have realized that it is due to strcpy_s. *We have
changes that to strpcy and then it was fine.
There are some places the destlength was more then whatever size of
deststr. *I know it is a mistake but the copy string had character to
copy. *So I was thinking it shouldn't crash the project. Isn't that
true?

Example:
deststr[128];
copystr[] = "Test String";
destlength = 256;

strcpy_s(deststr, destlength, copystr);
even strcpy_s(deststr, strlen(copystr), copystr); was crashing (I need
to look more for this)

But is there any known problem with strcpy_s?
from MSDN: "The strcpy_s function copies the contents in the address
of strSource, including the terminating null character, to the
location specified by strDestination. The destination string must be
large enough to hold the source string, including the terminating null
character. The behavior of strcpy_s is undefined if the source and
destination strings overlap."

I assume you are supposed to know the size of the destination buffer
(which is not unreasonable).
--
Nick Keighley
Jul 2 '08 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Paul Sheer | last post by:
I need to automatically search and replace all fixed size buffer strcpy's with strncpy's (or better yet, strlcpy's) as a security and stability audit. The code base is large and it is not feasable...
9
by: Ape Ricket | last post by:
Hi. During my program's set-up phase where it reads in the arguments it was invoked with, I programmed this: if (strcmp(argv,"-G") ==0) { geom_scaling = ON; if (i < argc-1)...
81
by: Matt | last post by:
I have 2 questions: 1. strlen returns an unsigned (size_t) quantity. Why is an unsigned value more approprate than a signed value? Why is unsighned value less appropriate? 2. Would there...
302
by: Lee | last post by:
Hi Whenever I use the gets() function, the gnu c compiler gives a warning that it is dangerous to use gets(). Is this due to the possibility of array overflow? Is it correct that the program...
55
by: Jake Thompson | last post by:
I need to copy a value into a char * field. I am currently doing this strcpy(cm8link.type,"13"); but I get an error of error C2664: 'strcpy' : cannot convert parameter 1 from 'const char'...
9
by: jim | last post by:
i want to make a c file that i can 'scanf ' students scores of 2 classes and their names , and i want it to get the sum of the 2 scores and make them in order .at last 'printf' /*am sorry,my...
38
by: edu.mvk | last post by:
Hi I am using strcpy() in my code for copying a string to another string. i am using static char arrays. for the first time it is exected correctly but the second time the control reaches...
3
by: RavindraB | last post by:
I am migrating C++ code to VS2005, for this i have to replace the some Deprecated CRT Functions like “strcpy” by strcpy_s I have written template to replace the function call. For strcpy_s I need...
6
by: boba | last post by:
Hi, I am a newbie to C++ (and programming in general) I have the following: char* fOutMsg; strcpy(fOutMsg, "03DS2"); --------------------------------------------------------------...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.