Compare memcpy to string literal? C

Multi tool use
Multi tool use












-1















net_buffer_t  netBuffer = nb_create(fd, MAX_LINE_LENGTH);
char byteArray[MAX_LINE_LENGTH] = "";
char commandString[COMMAND_LENGTH] = "";

nb_read_line(netBuffer, byteArray);
memcpy(commandString, byteArray, COMMAND_LENGTH);
commandString[COMMAND_LENGTH] = '';

printf("%dn", strcmp(commandString, "HELO"));

if(strcmp(commandString, "HELO") == 0){
printf("%sn", "got HELO!");
}else if(strcmp(commandString, "MAIL") == 0){
printf("%sn", "You got mail");
}else if(strcmp(commandString, "RCPT") == 0){

}else if(strcmp(commandString, "DATA") == 0){

}else if(strcmp(commandString, "NOOP") == 0){

}else if(strcmp(commandString, "QUIT") == 0){

}


So what my code does is it's supposed to read from a stream then compare it, but for some reason when I type in "HELO" and printf("%sn", commandString) it prints out "HELO" as well but I get 10 from the strcmp print line.










share|improve this question























  • You're writing to an out of bounds index in commandString, btw. And what is nb_read_line()?

    – Shawn
    Nov 22 '18 at 22:10











  • The problem may also be in nb_read_line which you didn't post….

    – Jabberwocky
    Nov 22 '18 at 22:11













  • The only thing that is obvious about nb_read_line() is that it does not provide you with a properly zero-terminated C string. Trying to turn it into one did not help. The printf statement doesn't really prove anything, it might well spit out unprintable control characters. Look at the docs or definition of nb_read_line, some odds that it returns the number of bytes that were read. That tells you where to put the 0.

    – Hans Passant
    Nov 22 '18 at 23:32













  • turned out to be the out of range null value

    – DolanTheMFWizard
    Nov 23 '18 at 2:19
















-1















net_buffer_t  netBuffer = nb_create(fd, MAX_LINE_LENGTH);
char byteArray[MAX_LINE_LENGTH] = "";
char commandString[COMMAND_LENGTH] = "";

nb_read_line(netBuffer, byteArray);
memcpy(commandString, byteArray, COMMAND_LENGTH);
commandString[COMMAND_LENGTH] = '';

printf("%dn", strcmp(commandString, "HELO"));

if(strcmp(commandString, "HELO") == 0){
printf("%sn", "got HELO!");
}else if(strcmp(commandString, "MAIL") == 0){
printf("%sn", "You got mail");
}else if(strcmp(commandString, "RCPT") == 0){

}else if(strcmp(commandString, "DATA") == 0){

}else if(strcmp(commandString, "NOOP") == 0){

}else if(strcmp(commandString, "QUIT") == 0){

}


So what my code does is it's supposed to read from a stream then compare it, but for some reason when I type in "HELO" and printf("%sn", commandString) it prints out "HELO" as well but I get 10 from the strcmp print line.










share|improve this question























  • You're writing to an out of bounds index in commandString, btw. And what is nb_read_line()?

    – Shawn
    Nov 22 '18 at 22:10











  • The problem may also be in nb_read_line which you didn't post….

    – Jabberwocky
    Nov 22 '18 at 22:11













  • The only thing that is obvious about nb_read_line() is that it does not provide you with a properly zero-terminated C string. Trying to turn it into one did not help. The printf statement doesn't really prove anything, it might well spit out unprintable control characters. Look at the docs or definition of nb_read_line, some odds that it returns the number of bytes that were read. That tells you where to put the 0.

    – Hans Passant
    Nov 22 '18 at 23:32













  • turned out to be the out of range null value

    – DolanTheMFWizard
    Nov 23 '18 at 2:19














-1












-1








-1








net_buffer_t  netBuffer = nb_create(fd, MAX_LINE_LENGTH);
char byteArray[MAX_LINE_LENGTH] = "";
char commandString[COMMAND_LENGTH] = "";

nb_read_line(netBuffer, byteArray);
memcpy(commandString, byteArray, COMMAND_LENGTH);
commandString[COMMAND_LENGTH] = '';

printf("%dn", strcmp(commandString, "HELO"));

if(strcmp(commandString, "HELO") == 0){
printf("%sn", "got HELO!");
}else if(strcmp(commandString, "MAIL") == 0){
printf("%sn", "You got mail");
}else if(strcmp(commandString, "RCPT") == 0){

}else if(strcmp(commandString, "DATA") == 0){

}else if(strcmp(commandString, "NOOP") == 0){

}else if(strcmp(commandString, "QUIT") == 0){

}


So what my code does is it's supposed to read from a stream then compare it, but for some reason when I type in "HELO" and printf("%sn", commandString) it prints out "HELO" as well but I get 10 from the strcmp print line.










share|improve this question














net_buffer_t  netBuffer = nb_create(fd, MAX_LINE_LENGTH);
char byteArray[MAX_LINE_LENGTH] = "";
char commandString[COMMAND_LENGTH] = "";

nb_read_line(netBuffer, byteArray);
memcpy(commandString, byteArray, COMMAND_LENGTH);
commandString[COMMAND_LENGTH] = '';

printf("%dn", strcmp(commandString, "HELO"));

if(strcmp(commandString, "HELO") == 0){
printf("%sn", "got HELO!");
}else if(strcmp(commandString, "MAIL") == 0){
printf("%sn", "You got mail");
}else if(strcmp(commandString, "RCPT") == 0){

}else if(strcmp(commandString, "DATA") == 0){

}else if(strcmp(commandString, "NOOP") == 0){

}else if(strcmp(commandString, "QUIT") == 0){

}


So what my code does is it's supposed to read from a stream then compare it, but for some reason when I type in "HELO" and printf("%sn", commandString) it prints out "HELO" as well but I get 10 from the strcmp print line.







c memcpy strcmp






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 22 '18 at 22:03









DolanTheMFWizardDolanTheMFWizard

938




938













  • You're writing to an out of bounds index in commandString, btw. And what is nb_read_line()?

    – Shawn
    Nov 22 '18 at 22:10











  • The problem may also be in nb_read_line which you didn't post….

    – Jabberwocky
    Nov 22 '18 at 22:11













  • The only thing that is obvious about nb_read_line() is that it does not provide you with a properly zero-terminated C string. Trying to turn it into one did not help. The printf statement doesn't really prove anything, it might well spit out unprintable control characters. Look at the docs or definition of nb_read_line, some odds that it returns the number of bytes that were read. That tells you where to put the 0.

    – Hans Passant
    Nov 22 '18 at 23:32













  • turned out to be the out of range null value

    – DolanTheMFWizard
    Nov 23 '18 at 2:19



















  • You're writing to an out of bounds index in commandString, btw. And what is nb_read_line()?

    – Shawn
    Nov 22 '18 at 22:10











  • The problem may also be in nb_read_line which you didn't post….

    – Jabberwocky
    Nov 22 '18 at 22:11













  • The only thing that is obvious about nb_read_line() is that it does not provide you with a properly zero-terminated C string. Trying to turn it into one did not help. The printf statement doesn't really prove anything, it might well spit out unprintable control characters. Look at the docs or definition of nb_read_line, some odds that it returns the number of bytes that were read. That tells you where to put the 0.

    – Hans Passant
    Nov 22 '18 at 23:32













  • turned out to be the out of range null value

    – DolanTheMFWizard
    Nov 23 '18 at 2:19

















You're writing to an out of bounds index in commandString, btw. And what is nb_read_line()?

– Shawn
Nov 22 '18 at 22:10





You're writing to an out of bounds index in commandString, btw. And what is nb_read_line()?

– Shawn
Nov 22 '18 at 22:10













The problem may also be in nb_read_line which you didn't post….

– Jabberwocky
Nov 22 '18 at 22:11







The problem may also be in nb_read_line which you didn't post….

– Jabberwocky
Nov 22 '18 at 22:11















The only thing that is obvious about nb_read_line() is that it does not provide you with a properly zero-terminated C string. Trying to turn it into one did not help. The printf statement doesn't really prove anything, it might well spit out unprintable control characters. Look at the docs or definition of nb_read_line, some odds that it returns the number of bytes that were read. That tells you where to put the 0.

– Hans Passant
Nov 22 '18 at 23:32







The only thing that is obvious about nb_read_line() is that it does not provide you with a properly zero-terminated C string. Trying to turn it into one did not help. The printf statement doesn't really prove anything, it might well spit out unprintable control characters. Look at the docs or definition of nb_read_line, some odds that it returns the number of bytes that were read. That tells you where to put the 0.

– Hans Passant
Nov 22 '18 at 23:32















turned out to be the out of range null value

– DolanTheMFWizard
Nov 23 '18 at 2:19





turned out to be the out of range null value

– DolanTheMFWizard
Nov 23 '18 at 2:19












1 Answer
1






active

oldest

votes


















1














  char commandString[COMMAND_LENGTH] = "";
...
commandString[COMMAND_LENGTH] = '';


You are writing past the end of the commandString array, because C arrays use zero-based indices. You should allocate an extra element for the null termination character.



  char commandString[COMMAND_LENGTH + 1]; 
// no need to initialize if you are overwriting anyway





share|improve this answer





















  • 1





    strncpy() has too many peculiarities to make it a good suggestion (including that it might not add a trailing nul to the destination depending on the length of the source string).

    – Shawn
    Nov 22 '18 at 22:23













  • @Shawn wow didn't know that, always thought it wrote the null character regardless. Thanks for the heads-up

    – meowgoesthedog
    Nov 22 '18 at 22:32











  • It's a tricky function prone to biting folks who didn't read the fine print before using it (which is most people). Personally, I wish it wasn't in the standard.

    – Shawn
    Nov 22 '18 at 22:49











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
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53438449%2fcompare-memcpy-to-string-literal-c%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









1














  char commandString[COMMAND_LENGTH] = "";
...
commandString[COMMAND_LENGTH] = '';


You are writing past the end of the commandString array, because C arrays use zero-based indices. You should allocate an extra element for the null termination character.



  char commandString[COMMAND_LENGTH + 1]; 
// no need to initialize if you are overwriting anyway





share|improve this answer





















  • 1





    strncpy() has too many peculiarities to make it a good suggestion (including that it might not add a trailing nul to the destination depending on the length of the source string).

    – Shawn
    Nov 22 '18 at 22:23













  • @Shawn wow didn't know that, always thought it wrote the null character regardless. Thanks for the heads-up

    – meowgoesthedog
    Nov 22 '18 at 22:32











  • It's a tricky function prone to biting folks who didn't read the fine print before using it (which is most people). Personally, I wish it wasn't in the standard.

    – Shawn
    Nov 22 '18 at 22:49
















1














  char commandString[COMMAND_LENGTH] = "";
...
commandString[COMMAND_LENGTH] = '';


You are writing past the end of the commandString array, because C arrays use zero-based indices. You should allocate an extra element for the null termination character.



  char commandString[COMMAND_LENGTH + 1]; 
// no need to initialize if you are overwriting anyway





share|improve this answer





















  • 1





    strncpy() has too many peculiarities to make it a good suggestion (including that it might not add a trailing nul to the destination depending on the length of the source string).

    – Shawn
    Nov 22 '18 at 22:23













  • @Shawn wow didn't know that, always thought it wrote the null character regardless. Thanks for the heads-up

    – meowgoesthedog
    Nov 22 '18 at 22:32











  • It's a tricky function prone to biting folks who didn't read the fine print before using it (which is most people). Personally, I wish it wasn't in the standard.

    – Shawn
    Nov 22 '18 at 22:49














1












1








1







  char commandString[COMMAND_LENGTH] = "";
...
commandString[COMMAND_LENGTH] = '';


You are writing past the end of the commandString array, because C arrays use zero-based indices. You should allocate an extra element for the null termination character.



  char commandString[COMMAND_LENGTH + 1]; 
// no need to initialize if you are overwriting anyway





share|improve this answer















  char commandString[COMMAND_LENGTH] = "";
...
commandString[COMMAND_LENGTH] = '';


You are writing past the end of the commandString array, because C arrays use zero-based indices. You should allocate an extra element for the null termination character.



  char commandString[COMMAND_LENGTH + 1]; 
// no need to initialize if you are overwriting anyway






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 22 '18 at 22:32

























answered Nov 22 '18 at 22:09









meowgoesthedogmeowgoesthedog

9,51431426




9,51431426








  • 1





    strncpy() has too many peculiarities to make it a good suggestion (including that it might not add a trailing nul to the destination depending on the length of the source string).

    – Shawn
    Nov 22 '18 at 22:23













  • @Shawn wow didn't know that, always thought it wrote the null character regardless. Thanks for the heads-up

    – meowgoesthedog
    Nov 22 '18 at 22:32











  • It's a tricky function prone to biting folks who didn't read the fine print before using it (which is most people). Personally, I wish it wasn't in the standard.

    – Shawn
    Nov 22 '18 at 22:49














  • 1





    strncpy() has too many peculiarities to make it a good suggestion (including that it might not add a trailing nul to the destination depending on the length of the source string).

    – Shawn
    Nov 22 '18 at 22:23













  • @Shawn wow didn't know that, always thought it wrote the null character regardless. Thanks for the heads-up

    – meowgoesthedog
    Nov 22 '18 at 22:32











  • It's a tricky function prone to biting folks who didn't read the fine print before using it (which is most people). Personally, I wish it wasn't in the standard.

    – Shawn
    Nov 22 '18 at 22:49








1




1





strncpy() has too many peculiarities to make it a good suggestion (including that it might not add a trailing nul to the destination depending on the length of the source string).

– Shawn
Nov 22 '18 at 22:23







strncpy() has too many peculiarities to make it a good suggestion (including that it might not add a trailing nul to the destination depending on the length of the source string).

– Shawn
Nov 22 '18 at 22:23















@Shawn wow didn't know that, always thought it wrote the null character regardless. Thanks for the heads-up

– meowgoesthedog
Nov 22 '18 at 22:32





@Shawn wow didn't know that, always thought it wrote the null character regardless. Thanks for the heads-up

– meowgoesthedog
Nov 22 '18 at 22:32













It's a tricky function prone to biting folks who didn't read the fine print before using it (which is most people). Personally, I wish it wasn't in the standard.

– Shawn
Nov 22 '18 at 22:49





It's a tricky function prone to biting folks who didn't read the fine print before using it (which is most people). Personally, I wish it wasn't in the standard.

– Shawn
Nov 22 '18 at 22:49


















draft saved

draft discarded




















































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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53438449%2fcompare-memcpy-to-string-literal-c%23new-answer', 'question_page');
}
);

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







7DIohGC,hJF8
Bpctxtj3MUek083X VbX5ok24I,CJcyePu54UeZ4qRzy,89A9u

Popular posts from this blog

404 Error Contact Form 7 ajax form submitting

How to resolve this name issue having white space while installing the android Studio.?

C# WPF - Problem with Material Design Textbox