Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 26th, 2006, 11:24 PM   #1
cCj
Newbie
 
Join Date: Mar 2006
Posts: 11
Rep Power: 0 cCj is on a distinguished road
Safe string copy?

Hi,
strcpy is a one way to copy strings but what if I need to make sure that the dest string (buffer) is large enough to hold the source string, is there a function for that?
I could just check the length of the source string can compare that to the predefined size of destination buffer but I assume there is already function for this.

Thank you.
cCj is offline   Reply With Quote
Old Apr 27th, 2006, 12:02 AM   #2
Mad_guy
Hobbyist Programmer
 
Mad_guy's Avatar
 
Join Date: Oct 2004
Location: Sandstorm, Techno Club
Posts: 239
Rep Power: 5 Mad_guy is on a distinguished road
Send a message via AIM to Mad_guy Send a message via MSN to Mad_guy
IIRC, there's no function to do that, if you're dealing with a buffer obtained from malloc you can just realloc it to the appropriate size, though. If you're not afraid of just truncating the value you can do:

strncpy(destination,source,sizeof(source)-1);
source[sizeof(source)-1] = 0;

That'll make sure that the string is copied safely and that the string is null terminated (many str functions like strncat and strncpy don't ensure null termination which can still lead to arbitrary code execution or nothing based on adjacent buffers.)
__________________
os: mac os 10.5.4
revision control: git
editor: emacs

site
Mad_guy is offline   Reply With Quote
Old Apr 27th, 2006, 12:32 AM   #3
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 893
Rep Power: 4 The Dark is on a distinguished road
Quote:
Originally Posted by Mad_guy
IIRC, there's no function to do that, if you're dealing with a buffer obtained from malloc you can just realloc it to the appropriate size, though. If you're not afraid of just truncating the value you can do:

strncpy(destination,source,sizeof(source)-1);
source[sizeof(source)-1] = 0;

That'll make sure that the string is copied safely and that the string is null terminated (many str functions like strncat and strncpy don't ensure null termination which can still lead to arbitrary code execution or nothing based on adjacent buffers.)
I think you mean
strncpy(destination,source,sizeof(destination)-1);
destination[sizeof(destination)-1] = 0;

Also, this will only work if destination is an array, not a pointer. If it is a pointer, sizeof will return 4 (for a 32 bit system).
The Dark is offline   Reply With Quote
Old Apr 27th, 2006, 7:32 AM   #4
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
You can use 'strdup' if you accept responsibility for freeing the memory when you're through.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Apr 27th, 2006, 7:57 AM   #5
cCj
Newbie
 
Join Date: Mar 2006
Posts: 11
Rep Power: 0 cCj is on a distinguished road
Thanks for the replies but I know that option but you see what I'm doing is receiving IRC messages from clients but if the client sends very long messages I don't want to just allocate more space. I could just reject the over sized (flood) messages.

But anyway, thanks again, I can continue from my own from here.
cCj is offline   Reply With Quote
Old Apr 27th, 2006, 8:20 PM   #6
Narue
Professional Programmer
 
Narue's Avatar
 
Join Date: Sep 2005
Posts: 419
Rep Power: 4 Narue is on a distinguished road
>but if the client sends very long messages
It's generally a bad idea to have built-in limitations.

>I could just reject the over sized (flood) messages.
You could terminate the program too, and get about the same reaction from your user base.
__________________
Even if the voices aren't real, they have some pretty good ideas.
Narue is offline   Reply With Quote
Old Apr 28th, 2006, 10:40 PM   #7
cCj
Newbie
 
Join Date: Mar 2006
Posts: 11
Rep Power: 0 cCj is on a distinguished road
Quote:
Originally Posted by Narue
>I could just reject the over sized (flood) messages.
You could terminate the program too, and get about the same reaction from your user base.
But then if one client sends an oversized message all the client apps would be terminated since none could handle it.
cCj is offline   Reply With Quote
Old Apr 29th, 2006, 8:38 AM   #8
Narue
Professional Programmer
 
Narue's Avatar
 
Join Date: Sep 2005
Posts: 419
Rep Power: 4 Narue is on a distinguished road
>But then if one client sends an oversized message all the client apps would be
>terminated since none could handle it.
Methinks you missed the point entirely.
__________________
Even if the voices aren't real, they have some pretty good ideas.
Narue is offline   Reply With Quote
Old Apr 30th, 2006, 9:08 AM   #9
cCj
Newbie
 
Join Date: Mar 2006
Posts: 11
Rep Power: 0 cCj is on a distinguished road
Smile

Quote:
Originally Posted by Narue
>But then if one client sends an oversized message all the client apps would be
>terminated since none could handle it.
Methinks you missed the point entirely.
Lol and I still don't know. Any hints?
cCj is offline   Reply With Quote
Old Apr 30th, 2006, 10:21 AM   #10
jayme
Professional Programmer
 
jayme's Avatar
 
Join Date: Nov 2005
Location: Canada
Posts: 495
Rep Power: 0 jayme is an unknown quantity at this point
Send a message via MSN to jayme
I think he means rejecting data would be the same as removing the client from the server, so why keep the client in the server?
__________________

Quote:
Originally Posted by Mohamed Jihad
Durka durka!
Due to incorrect calculations during the middle ages, our calendar actually begins a few years after Jesus' birth. Thus the real 6/6/6 happened a few years back. The world already ended and you missed it.

Download Code::Blocks now!
jayme is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:44 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC