- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - OpenVMS
- >
- Re: Is this C language statement a problem?
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-25-2009 11:33 PM
тАО05-25-2009 11:33 PM
Is this C language statement a problem?
I have a C snippet that someone here at work says could be problematic. That the copying of memory locations should not overlap.
I've never had a problem with this style on DEC C, but maybe I've been missing something.
Can anyone offer a reliable opinion on whether it is 'dangerous' code?
The snippet is:
char currStlDate[8+1];
...
memset(currStlDate, 0, sizeof (currStlDate));
...
/* Here currStlDate is "20090526" */
sprintf(currStlDate, "%s", &currStlDate[2]);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-26-2009 12:52 AM
тАО05-26-2009 12:52 AM
Re: Is this C language statement a problem?
???
strcpy(dst,src): must not overlap
memmove(dst,src,size): may overlap
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-26-2009 02:46 AM
тАО05-26-2009 02:46 AM
Re: Is this C language statement a problem?
Both source and dest pointers in the sprintf statement point to memory locations where dest will overwrite source.
Isn't this what memory overlapping is?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-26-2009 03:23 AM
тАО05-26-2009 03:23 AM
Re: Is this C language statement a problem?
As far I see standards say nothing about the target of sprintf (but the question may be better placed in comp.lang.c !).
In practice I see nothing against doing it.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-26-2009 03:34 AM
тАО05-26-2009 03:34 AM
Re: Is this C language statement a problem?
- the char line should have a comment:
// zero terminated ccyymmdd or yymmdd format.
- the memset suggests that the coder wasn't sure when or how the date string will be used. It is likely to be entirely redundant, or should be replaced by: currStlDate[0] = 0
- the sprintf suggests that the coder was too lazy to make a clear 'ccyymmdd' versus 'yymmdd' position. It seems unacceptable to me to have a variable sometimes hold one flavor, sometimes the other. It should probably be replaced by:
char *currStlDate_yymmdd = &currStlDate[2];
fwiw
Hein.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-26-2009 03:43 AM
тАО05-26-2009 03:43 AM
Re: Is this C language statement a problem?
just curious I made a test on really overlapping (not just substring shuffling) sprintf :
/*----------------------------------------*/
#include
#include
int main(){
char test[50];
strcpy(test,"abcdefghijklmnopqrstuvwxyz1234567890");
sprintf(test,"%s",test);
printf("%s\n",test);
sprintf(test," %s",&test[2]);
printf("%s\n",test);
}
/*------------------------------------------*/
One would expect the first 3 or 4 characters blanked, but the rest of the string simply copied.
On VMS/DECC: yes, it is so:
run test_sprintf
abcdefghijklmnopqrstuvwxyz1234567890
defghijklmnopqrstuvwxyz1234567890
On RH Linux/GCC: what a surprise:
./a.out
abcdefghijklmnopqrstuvwxyz1234567890
ddfghhjkllnopprsttvwxxz1224566890
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-26-2009 03:44 AM
тАО05-26-2009 03:44 AM
Re: Is this C language statement a problem?
The documentation states:
int sprintf(char *s, const char *format, ...);
Equivalent to the fprintf function except that the argument s specifies
an array, rather than a stream, into which the generated output will be
written. A null character is written at the end of the characters written. If
copying takes place between objects that overlap, the behavior is undefined.
The sprintf function returns the number of characters written into the
array, not counting the terminating null character.
Thanks for your answers anyway.
James P.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-30-2009 08:06 AM
тАО05-30-2009 08:06 AM
Re: Is this C language statement a problem?
The C99 Standard has the "restrict" keyword for the target buffer in sprintf.
>Isn't this what memory overlapping is?
Exactly.
>If copying takes place between objects that overlap, the behavior is undefined.
C99 is even clearer:
int sprintf(restrict char *s, const char *format, ...);
>Joseph: As far I see standards say nothing about the target of sprintf
The Standard is quite clear.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-03-2009 11:12 PM
тАО06-03-2009 11:12 PM
Re: Is this C language statement a problem?
James P.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-03-2009 11:12 PM
тАО06-03-2009 11:12 PM