Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alternatives to MS strncpy_s

Tags:

c++

visual-c++

What are some alternatives to the Microsoft security enhanced functions such as strncpy_s or _itoa_s? Although developing in MS environment the goal is to write code that could be ported easily to other platforms.

like image 281
CodeLizard Avatar asked May 13 '09 14:05

CodeLizard


5 Answers

If you really want to program in C:

Use the plain old standard strncpy.

If you're programming in C++:

Use the plain old standard string class std::string.

(hint: You probably want the latter. C strings are just bugs waiting to happen, even if you use the "secure" *_s functions. C++ added a string class for a reason)

like image 83
jalf Avatar answered Nov 03 '22 01:11

jalf


std::string s(Cstring, n);
like image 39
Mykola Golubyev Avatar answered Nov 02 '22 23:11

Mykola Golubyev


Try strlcpy. Though not standard C, it exists on a number of platforms and you can download implementations as the source code is available free of charge. See http://www.gratisoft.us/todd/papers/strlcpy.html

like image 37
dubnde Avatar answered Nov 03 '22 01:11

dubnde


Rageous is correct, there is no complex logic behind it.

I would just use Microsoft's version for now and if you decide to port to another OS later, THEN implement it yourself for the target platform and use preprocessor commands to specify your implementation on the non-Windows platform(s).

like image 32
17 of 26 Avatar answered Nov 03 '22 01:11

17 of 26


Just use strncpy and disable the warning in VC++.

like image 32
Nemanja Trifunovic Avatar answered Nov 03 '22 00:11

Nemanja Trifunovic