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

Alternative to strcpy

72
Was just wondering if there was an alternative to the strcpy function?
Have done up my assignment on buffer overrun than I realised that I cannot use the strcpy function as stated in the question paper.

Did some search around the internet and found things like strncpy but I dun think I can use that as it specifies the number of characters to copy which will not give me the same effect as strcpy.

Will still continue to search in the mean time.

Thanks in advance. :)
Aug 10 '07 #1
5 13039
weaknessforcats
9,208 Expert Mod 8TB
There is no alternative to strcpy(). Only strcpy() will copy characters until it reaches a \0 whether it has corrupted your memory or not. It doesn't care. Hackers love this thing.

All replacements a) require you know ahead of time how big the target buffer is (like MS strcpy_s()) or b) overload the assignment operator (C++ only) and not use strcpy() in the assignment of youer class members.

That is:
If you are using C++ you should not be using anything from the C string library. You should be using C++ string objects instead.
If you are using C, then you will need to know the size of the target buffer. A function like this can be written in five minutes.
Aug 10 '07 #2
JosAH
11,448 Expert 8TB
If you are using C, then you will need to know the size of the target buffer. A function like this can be written in five minutes.
If the function is just passed an address it'll be a hell of a job to determine
whether or not a certain string will fit in there; iow what the 'size' of the buffer
is located at that particular address.

kind regards,

Jos
Aug 10 '07 #3
weaknessforcats
9,208 Expert Mod 8TB
If the function is just passed an address it'll be a hell of a job to determine
whether or not a certain string will fit in there; iow what the 'size' of the buffer
is located at that particular address.
Yes indeed. So in C you would write a struct with a char* and an unisgned int member for the size of the buffer (not the size of the string). Then you would use these struct variables as your string and write a bevy of functions that use the struct. Eventually, you would replace the entire C string library.

You would do the same thing in C++ with appropriate access specifiers and member functions. That is, the basic_string<> template.
Aug 10 '07 #4
JosAH
11,448 Expert 8TB
Yes indeed. So in C you would write a struct with a char* and an unisgned int member for the size of the buffer (not the size of the string). Then you would use these struct variables as your string and write a bevy of functions that use the struct. Eventually, you would replace the entire C string library.

You would do the same thing in C++ with appropriate access specifiers and member functions. That is, the basic_string<> template.
C is just structured assembly language; we shouldn't expect utter safety from it
and we certainly shouldn't ask C to protect us from our own bloopers. If someone
wants a safe programming language s/he should program in an interpreted form
of some language; C just shoots us in the foot if we goof; C++ is a bit meaner:
it also stabs us in the back when we don't pay attention for a moment ;-)

kind regards,

Jos
Aug 10 '07 #5
KWSW
72
wow lots of stuff to digest here... thanks for the tips... :)
Aug 10 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

4
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...
3
by: naren | last post by:
Iam not getting the correct pros and cons of the strcpy() and memcpy() some where i read for short strings strcpy is faster and for large strings memcpy is faster.. in strcpy() there is a single...
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...
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"); --------------------------------------------------------------...
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
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...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.