473,394 Members | 1,679 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,394 software developers and data experts.

_tcscpy_s and avoiding buffer overruns

If I use the new secure _tcscpy_s, should I do:

#define DESTLEN somenumber e.g. 10 or 255
void copystring(const _TCHAR* src_of_unknown_length)
{
_TCHAR msg[DESTLEN];
_tcscpy_s(msg, src_of_unknown_length, (size_t)DESTLEN);
// or _tcscpy_s(msg, src_of_unknown_length, (size_t)(DESTLEN - 1));
// ??
}
i.e. say if DESTLEN is 10, does it know to only copy 9 characters of
src_of_unknown_length into the buffer, in order to leave room for the null
terminator, or do I have to take this into account, as in the commented-out
bit?
And if so, is this behaviour different to that of the old _tcsncpy ?
Nov 17 '05 #1
1 7595
"Bonj" wrote:
#define DESTLEN somenumber e.g. 10 or 255
void copystring(const _TCHAR* src_of_unknown_length)
{
_TCHAR msg[DESTLEN];
_tcscpy_s(msg, src_of_unknown_length, (size_t)DESTLEN); // YES
// or _tcscpy_s(msg, src_of_unknown_length, (size_t)(DESTLEN - 1)); // NO
}
i.e. say if DESTLEN is 10, does it know to only copy 9 characters of
src_of_unknown_length into the buffer, in order to leave room for the null
terminator, or do I have to take this into account, as in the commented-out
bit?
The length you provide is the *complete* length of the buffer, and it won't
ever go past that (for NUL or otherwise). Just name the number of bytes
available for the target, and _tcscpy_s() will do the right thing.
And if so, is this behaviour different to that of the old _tcsncpy ?


_tcsncpy() stops after N characters, but doesn't promise a terminating NUL
byte. _tcscpy_s() stops after N characters, and promises that there will be a
NUL among them

~~ Steve
Nov 17 '05 #2

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

Similar topics

3
by: David Sworder | last post by:
Hi there, I come from a Visual C++ background. When writing a service that's exposed to the Internet, I had to check the incoming data stream (from the client) VERY carefully. If a hacker was...
2
by: Ioannis Vranos | last post by:
If we want our programs to be protected against buffer overflows, must we check the size of the various containers explicitly? E.g. #include <iostream> #include <string> int main()
0
by: Christoph Bisping | last post by:
Hello! I'm not sure if this is the right place to ask my question... According to the docs, it is possible to receive udp datagrams asynchronous using the Socket-class and its .BeginRecive(From)...
7
by: mathieu | last post by:
Hello, Is there a way to construct a std::istringstream from a buffer of char and avoid copying the array of bytes ? const char s = "Hello, World"; std::string str(s); std::istringstream is;...
1
by: Nico | last post by:
Where can I get it? I am a new comer here, I need your help.
1
by: davis | last post by:
Hi, I'm writing an app in bluetooth & wi-fi. It is client/server on both. We have defined our own network protocol that should run over both mediums. There are packet definitions of variable...
14
by: mast2as | last post by:
Hi everyone, I am trying to implement some specs which specify that an array of parameter is passed to a function as a pointer to an array terminated by a NULL chatacter. That seemed fairly easy...
1
by: tourist.tam | last post by:
Hi, I am trying to read a file using a BufferedStream and avoiding at maximum the use of string manipulation. I need now some help understanding on how to use byte array to hold the data and...
70
by: junky_fellow | last post by:
Guys, If main() calls some function func() and that function returns the error (errno), then does it make sense to return that value (errno) from main. (in case main can't proceed further) ? ...
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:
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.