Whats the proper way to handle strcpy's return type
I created a program which at some point turned to be a buggy one and I can not find a good way to handle situations like in the following program:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int get_user_name( char *const dest, char *const src );
char *foo( char *variable ); /// foo() returns NULL
int main( void )
{
char arr[256] = { 0 };
char buffer[ 256 ] = { 0 };
char *const ptr = foo( arr ); /// foo() returned NULL here
if ( get_user_name( buffer, ptr ) == 0 ) /// get_user_name, should here return 0
{
printf("NULL returned from get_user_name()n" );
exit( EXIT_FAILURE );
}else
{
printf( "Everithing is OK" );
}
}
int get_user_name( char *const dest, char *const src )
{
char *ret = strcpy( dest, src ); /// Here Segfault happens
/// There is no return here because of above Segfault
if ( ret == NULL )
{
return 0;
}else
{
return 1;
}
}
char *foo( char *variable )
{
if ( strlen( variable) < 1 )
{
return NULL; /// Here will return NULL because there is no Length
}
/// Some code here ...
return variable;
}
Here is use a demo function which returns NULL
to explain my problem.
I was thinking to check src
before past it to strcpy
but I really cannot understand why strcpy
returns dest
without check if src
is NULL
or not.
Why does this happens because I see that strcpy
manual say only about the return of dest
and nothing if fails:
RETURN VALUE
The strcpy() and strncpy() functions return a pointer to the destination string dest.
c strcpy
add a comment |
I created a program which at some point turned to be a buggy one and I can not find a good way to handle situations like in the following program:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int get_user_name( char *const dest, char *const src );
char *foo( char *variable ); /// foo() returns NULL
int main( void )
{
char arr[256] = { 0 };
char buffer[ 256 ] = { 0 };
char *const ptr = foo( arr ); /// foo() returned NULL here
if ( get_user_name( buffer, ptr ) == 0 ) /// get_user_name, should here return 0
{
printf("NULL returned from get_user_name()n" );
exit( EXIT_FAILURE );
}else
{
printf( "Everithing is OK" );
}
}
int get_user_name( char *const dest, char *const src )
{
char *ret = strcpy( dest, src ); /// Here Segfault happens
/// There is no return here because of above Segfault
if ( ret == NULL )
{
return 0;
}else
{
return 1;
}
}
char *foo( char *variable )
{
if ( strlen( variable) < 1 )
{
return NULL; /// Here will return NULL because there is no Length
}
/// Some code here ...
return variable;
}
Here is use a demo function which returns NULL
to explain my problem.
I was thinking to check src
before past it to strcpy
but I really cannot understand why strcpy
returns dest
without check if src
is NULL
or not.
Why does this happens because I see that strcpy
manual say only about the return of dest
and nothing if fails:
RETURN VALUE
The strcpy() and strncpy() functions return a pointer to the destination string dest.
c strcpy
but I really cannot understand why strcpy returns dest without check if src is NULL or not. - because it says in the standard that it expects a c-string, not aNULL
. And I betstrcpy()
most times won't return anything but crash almost instantly.
– Swordfish
Nov 24 '18 at 17:34
strcpy
causes the abort of your program because one of the parameters is null. Check before calling.
– Paul Ogilvie
Nov 24 '18 at 17:34
@Swordfish I all ready pointed to segfault in the Question. And I also know what the standard say. Expecting something which is a string and relying on that that there will be no NULL without checking, from my side it does not make it the best function. I do know that asc
programmer I need to check everything againstNULL
but this is not my Question.
– user10533142
Nov 24 '18 at 17:36
add a comment |
I created a program which at some point turned to be a buggy one and I can not find a good way to handle situations like in the following program:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int get_user_name( char *const dest, char *const src );
char *foo( char *variable ); /// foo() returns NULL
int main( void )
{
char arr[256] = { 0 };
char buffer[ 256 ] = { 0 };
char *const ptr = foo( arr ); /// foo() returned NULL here
if ( get_user_name( buffer, ptr ) == 0 ) /// get_user_name, should here return 0
{
printf("NULL returned from get_user_name()n" );
exit( EXIT_FAILURE );
}else
{
printf( "Everithing is OK" );
}
}
int get_user_name( char *const dest, char *const src )
{
char *ret = strcpy( dest, src ); /// Here Segfault happens
/// There is no return here because of above Segfault
if ( ret == NULL )
{
return 0;
}else
{
return 1;
}
}
char *foo( char *variable )
{
if ( strlen( variable) < 1 )
{
return NULL; /// Here will return NULL because there is no Length
}
/// Some code here ...
return variable;
}
Here is use a demo function which returns NULL
to explain my problem.
I was thinking to check src
before past it to strcpy
but I really cannot understand why strcpy
returns dest
without check if src
is NULL
or not.
Why does this happens because I see that strcpy
manual say only about the return of dest
and nothing if fails:
RETURN VALUE
The strcpy() and strncpy() functions return a pointer to the destination string dest.
c strcpy
I created a program which at some point turned to be a buggy one and I can not find a good way to handle situations like in the following program:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int get_user_name( char *const dest, char *const src );
char *foo( char *variable ); /// foo() returns NULL
int main( void )
{
char arr[256] = { 0 };
char buffer[ 256 ] = { 0 };
char *const ptr = foo( arr ); /// foo() returned NULL here
if ( get_user_name( buffer, ptr ) == 0 ) /// get_user_name, should here return 0
{
printf("NULL returned from get_user_name()n" );
exit( EXIT_FAILURE );
}else
{
printf( "Everithing is OK" );
}
}
int get_user_name( char *const dest, char *const src )
{
char *ret = strcpy( dest, src ); /// Here Segfault happens
/// There is no return here because of above Segfault
if ( ret == NULL )
{
return 0;
}else
{
return 1;
}
}
char *foo( char *variable )
{
if ( strlen( variable) < 1 )
{
return NULL; /// Here will return NULL because there is no Length
}
/// Some code here ...
return variable;
}
Here is use a demo function which returns NULL
to explain my problem.
I was thinking to check src
before past it to strcpy
but I really cannot understand why strcpy
returns dest
without check if src
is NULL
or not.
Why does this happens because I see that strcpy
manual say only about the return of dest
and nothing if fails:
RETURN VALUE
The strcpy() and strncpy() functions return a pointer to the destination string dest.
c strcpy
c strcpy
edited Nov 24 '18 at 17:50
asked Nov 24 '18 at 17:29
user10533142
but I really cannot understand why strcpy returns dest without check if src is NULL or not. - because it says in the standard that it expects a c-string, not aNULL
. And I betstrcpy()
most times won't return anything but crash almost instantly.
– Swordfish
Nov 24 '18 at 17:34
strcpy
causes the abort of your program because one of the parameters is null. Check before calling.
– Paul Ogilvie
Nov 24 '18 at 17:34
@Swordfish I all ready pointed to segfault in the Question. And I also know what the standard say. Expecting something which is a string and relying on that that there will be no NULL without checking, from my side it does not make it the best function. I do know that asc
programmer I need to check everything againstNULL
but this is not my Question.
– user10533142
Nov 24 '18 at 17:36
add a comment |
but I really cannot understand why strcpy returns dest without check if src is NULL or not. - because it says in the standard that it expects a c-string, not aNULL
. And I betstrcpy()
most times won't return anything but crash almost instantly.
– Swordfish
Nov 24 '18 at 17:34
strcpy
causes the abort of your program because one of the parameters is null. Check before calling.
– Paul Ogilvie
Nov 24 '18 at 17:34
@Swordfish I all ready pointed to segfault in the Question. And I also know what the standard say. Expecting something which is a string and relying on that that there will be no NULL without checking, from my side it does not make it the best function. I do know that asc
programmer I need to check everything againstNULL
but this is not my Question.
– user10533142
Nov 24 '18 at 17:36
but I really cannot understand why strcpy returns dest without check if src is NULL or not. - because it says in the standard that it expects a c-string, not a
NULL
. And I bet strcpy()
most times won't return anything but crash almost instantly.– Swordfish
Nov 24 '18 at 17:34
but I really cannot understand why strcpy returns dest without check if src is NULL or not. - because it says in the standard that it expects a c-string, not a
NULL
. And I bet strcpy()
most times won't return anything but crash almost instantly.– Swordfish
Nov 24 '18 at 17:34
strcpy
causes the abort of your program because one of the parameters is null. Check before calling.– Paul Ogilvie
Nov 24 '18 at 17:34
strcpy
causes the abort of your program because one of the parameters is null. Check before calling.– Paul Ogilvie
Nov 24 '18 at 17:34
@Swordfish I all ready pointed to segfault in the Question. And I also know what the standard say. Expecting something which is a string and relying on that that there will be no NULL without checking, from my side it does not make it the best function. I do know that as
c
programmer I need to check everything against NULL
but this is not my Question.– user10533142
Nov 24 '18 at 17:36
@Swordfish I all ready pointed to segfault in the Question. And I also know what the standard say. Expecting something which is a string and relying on that that there will be no NULL without checking, from my side it does not make it the best function. I do know that as
c
programmer I need to check everything against NULL
but this is not my Question.– user10533142
Nov 24 '18 at 17:36
add a comment |
1 Answer
1
active
oldest
votes
You handle the situation by first checking the parameters are not null. For example:
int get_user_name( char *dest, char *const src )
{
char *ret;
if (dest==0 || src==0) return 0;
strcpy( dest, src );
return 1;
}
Note also that dest
cannot be const
because it is written to.
Well, that means thatstrcpy
works after I know 100% thatdest
andsrc
are OK. Thank you
– user10533142
Nov 24 '18 at 17:40
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53460686%2fwhats-the-proper-way-to-handle-strcpys-return-type%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You handle the situation by first checking the parameters are not null. For example:
int get_user_name( char *dest, char *const src )
{
char *ret;
if (dest==0 || src==0) return 0;
strcpy( dest, src );
return 1;
}
Note also that dest
cannot be const
because it is written to.
Well, that means thatstrcpy
works after I know 100% thatdest
andsrc
are OK. Thank you
– user10533142
Nov 24 '18 at 17:40
add a comment |
You handle the situation by first checking the parameters are not null. For example:
int get_user_name( char *dest, char *const src )
{
char *ret;
if (dest==0 || src==0) return 0;
strcpy( dest, src );
return 1;
}
Note also that dest
cannot be const
because it is written to.
Well, that means thatstrcpy
works after I know 100% thatdest
andsrc
are OK. Thank you
– user10533142
Nov 24 '18 at 17:40
add a comment |
You handle the situation by first checking the parameters are not null. For example:
int get_user_name( char *dest, char *const src )
{
char *ret;
if (dest==0 || src==0) return 0;
strcpy( dest, src );
return 1;
}
Note also that dest
cannot be const
because it is written to.
You handle the situation by first checking the parameters are not null. For example:
int get_user_name( char *dest, char *const src )
{
char *ret;
if (dest==0 || src==0) return 0;
strcpy( dest, src );
return 1;
}
Note also that dest
cannot be const
because it is written to.
answered Nov 24 '18 at 17:39
Paul OgilviePaul Ogilvie
18.3k21235
18.3k21235
Well, that means thatstrcpy
works after I know 100% thatdest
andsrc
are OK. Thank you
– user10533142
Nov 24 '18 at 17:40
add a comment |
Well, that means thatstrcpy
works after I know 100% thatdest
andsrc
are OK. Thank you
– user10533142
Nov 24 '18 at 17:40
Well, that means that
strcpy
works after I know 100% that dest
and src
are OK. Thank you– user10533142
Nov 24 '18 at 17:40
Well, that means that
strcpy
works after I know 100% that dest
and src
are OK. Thank you– user10533142
Nov 24 '18 at 17:40
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53460686%2fwhats-the-proper-way-to-handle-strcpys-return-type%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
but I really cannot understand why strcpy returns dest without check if src is NULL or not. - because it says in the standard that it expects a c-string, not a
NULL
. And I betstrcpy()
most times won't return anything but crash almost instantly.– Swordfish
Nov 24 '18 at 17:34
strcpy
causes the abort of your program because one of the parameters is null. Check before calling.– Paul Ogilvie
Nov 24 '18 at 17:34
@Swordfish I all ready pointed to segfault in the Question. And I also know what the standard say. Expecting something which is a string and relying on that that there will be no NULL without checking, from my side it does not make it the best function. I do know that as
c
programmer I need to check everything againstNULL
but this is not my Question.– user10533142
Nov 24 '18 at 17:36