memcpy junk at beginning of buffer

Discussion in 'C Programming' started by Jeff, Dec 29, 2005.

  1. Jeff

    Jeff Guest

    Im trying to memcpy a buffer from a filled in simple structure.
    When I memcpy and then print the resulting buffer, I see 7 locations
    that have junk before my data starts. My data structure is:

    struct command_pkt {
    char command_num[3];
    char command[100];
    };

    typedef command_pkt COMMAND;

    The relevant portion is the following:

    sprintf(tmp,"%s %s",ip,host);

    COMMAND *com;
    strcpy(com->command_num,"1");
    strcpy(com->command,tmp);

    int len = sizeof(COMMAND);
    unsigned char buf[200];
    if (len > 200) {
    printf("ERROR - len > buf\n");
    return -1;
    }

    memset(&buf[0],0,200);
    memcpy(&buf[1],(unsigned char *)&com,len);

    printf("COM buf: <%s>\n",buf);

    Any ideas why there is junk at the beginning of my buffer?
    Jeff, Dec 29, 2005
    #1
    1. Advertising

  2. Jeff <> wrote:

    > The relevant portion is the following:


    > COMMAND *com;
    > strcpy(com->command_num,"1");
    > strcpy(com->command,tmp);


    > Any ideas why there is junk at the beginning of my buffer?


    I imagine the fact that com does not point at allocated space is a
    part of your problem.

    com=malloc( sizeof(COMMAND) ); /* with error checking as appropriate */

    --
    Christopher Benson-Manica | I *should* know what I'm talking about - if I
    ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
    Christopher Benson-Manica, Dec 29, 2005
    #2
    1. Advertising

  3. Jeff

    pemo Guest

    "Jeff" <> wrote in message
    news:...
    > Im trying to memcpy a buffer from a filled in simple structure.
    > When I memcpy and then print the resulting buffer, I see 7 locations
    > that have junk before my data starts. My data structure is:
    >
    > struct command_pkt {
    > char command_num[3];
    > char command[100];
    > };
    >
    > typedef command_pkt COMMAND;
    >
    > The relevant portion is the following:
    >
    > sprintf(tmp,"%s %s",ip,host);
    >
    > COMMAND *com;
    > strcpy(com->command_num,"1");
    > strcpy(com->command,tmp);
    >
    > int len = sizeof(COMMAND);
    > unsigned char buf[200];
    > if (len > 200) {
    > printf("ERROR - len > buf\n");
    > return -1;
    > }
    >
    > memset(&buf[0],0,200);
    > memcpy(&buf[1],(unsigned char *)&com,len);
    >
    > printf("COM buf: <%s>\n",buf);
    >
    > Any ideas why there is junk at the beginning of my buffer?


    I got this far, then stopped.

    > sprintf(tmp,"%s %s",ip,host);
    >
    > COMMAND *com;
    > strcpy(com->command_num,"1");
    > strcpy(com->command,tmp);


    If this is really taken from your code, what's com actually pointing to,
    i.e., you not allocated memory for a COMMAND, and pointed com to that
    memory.
    pemo, Dec 29, 2005
    #3
  4. Christopher Benson-Manica <> wrote:

    > > COMMAND *com;


    > com=malloc( sizeof(COMMAND) ); /* with error checking as appropriate */


    Or rather,

    com=malloc( sizeof *com ); /* see the difference? */

    --
    Christopher Benson-Manica | I *should* know what I'm talking about - if I
    ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
    Christopher Benson-Manica, Dec 29, 2005
    #4
  5. Jeff

    Guest

    > Im trying to memcpy a buffer from a filled in simple structure.
    > When I memcpy and then print the resulting buffer, I see 7 locations
    > that have junk before my data starts.
    >
    > My data structure is:
    >
    > struct command_pkt {
    > char command_num[3];
    > char command[100];
    > };
    >
    > typedef command_pkt COMMAND;


    You might want to avoid typedefs in this case. There is nothing wrong with
    knowing you are dealing with a struct of the type command_pkt when you are
    declaring your variables.

    > The relevant portion is the following:


    If possible, please post a complete program that others can compile it and
    try running it, instead of only a snippet. There can be a lot of other
    things going on that we can't see from here.

    > sprintf(tmp,"%s %s",ip,host);
    >
    > COMMAND *com;


    Here you define *com as a pointer, but where does it point to ?

    > strcpy(com->command_num,"1");
    > strcpy(com->command,tmp);
    >
    > int len = sizeof(COMMAND);
    > unsigned char buf[200];
    > if (len > 200) {
    > printf("ERROR - len > buf\n");
    > return -1;
    > }
    >
    > memset(&buf[0],0,200);
    > memcpy(&buf[1],(unsigned char *)&com,len);


    A few things are wrong here, of which at least :

    - You have declared a pointer to your struct, but you have not allocated
    any memory for it. Use malloc() or one of its friends, or declare 'com' as

    COMMAND com;

    and change your code to

    strcpy(com.command_num,"1");
    strcpy(com.command,tmp);

    - You are copying the *pointer* to your 'com' to the buffer, instead of
    'com' itself.

    - The size of the pointer to 'com' is probably not 'len' bytes big, so
    changes are you are copying other memory as well. This memory might be
    yours, or it might not be. This might crash your system or do other nasty
    things. Or it might just cause junk in your buffer. (which is nasty
    enough)

    - You are copying up to 200 bytes to the address of buf[1] instead of
    buf[0], thus overflowing your buffer by one byte.

    > printf("COM buf: <%s>\n",buf);


    It surprises me that this printf() outputs anything at all, since the above
    code suggests that buf[0] should be zero. But since all kind of funny memory
    accesses have been done before that, anything could happen here.

    > Any ideas why there is junk at the beginning of my buffer?


    Some time ago I read a story on this newsgroup about somebody who had demons
    fly out of his nose once, when he ran a similar program. Quite painful. You
    are lucky to have just junk in your buffer ! :)

    _Ico

    --
    :wq
    ^X^Cy^K^X^C^C^C^C
    , Dec 29, 2005
    #5
  6. Jeff

    Artie Gold Guest

    Jeff wrote:
    > Im trying to memcpy a buffer from a filled in simple structure.
    > When I memcpy and then print the resulting buffer, I see 7 locations
    > that have junk before my data starts. My data structure is:
    >
    > struct command_pkt {
    > char command_num[3];
    > char command[100];
    > };
    >
    > typedef command_pkt COMMAND;


    Huh? There's a type called `struct command_pkt' but not one (that *we*
    can see, anyway) called `command_pkt'.

    This is why we say POST REAL CODE (cut'n'paste).

    [snip]

    HTH,
    --ag
    --
    Artie Gold -- Austin, Texas
    http://goldsays.blogspot.com (new post 8/5)
    http://www.cafepress.com/goldsays
    "If you have nothing to hide, you're not trying!"
    Artie Gold, Dec 29, 2005
    #6
  7. Jeff

    Jack Klein Guest

    On 29 Dec 2005 12:10:54 -0800, "Jeff" <> wrote in
    comp.lang.c:

    > Im trying to memcpy a buffer from a filled in simple structure.
    > When I memcpy and then print the resulting buffer, I see 7 locations
    > that have junk before my data starts. My data structure is:
    >
    > struct command_pkt {
    > char command_num[3];
    > char command[100];
    > };
    >
    > typedef command_pkt COMMAND;


    The line above is not legal C. There is no such thing as a
    'command_pkt'. Either your actual code has:

    typedef struct command_pkt COMMAND;

    ....or you are not compiling with a C compiler.

    In any case, it's not a particularly good idea to create aliases for
    structure types, and an extremely bad idea to define them with ALL
    UPPER CASE LETTERS, which should be reserved for macros and, possibly,
    enumeration constants.

    > The relevant portion is the following:
    >
    > sprintf(tmp,"%s %s",ip,host);


    Where are tmp, ip, and host defined and given values?

    > COMMAND *com;


    Here you create an uninitialized pointer, which yo do not have the
    right to dereference, let alone write through.

    > strcpy(com->command_num,"1");
    > strcpy(com->command,tmp);


    Undefined behavior, writing through an uninitialized pointer.

    > int len = sizeof(COMMAND);


    The sizeof operator yields a value of type size_t. Given your
    definition of the structure, this value will fit into an int, but why
    not use the actual type?

    > unsigned char buf[200];


    Are you using a C99 conforming compiler, or are you using a different
    language, as I suspected above. Both the definition of 'len' and of
    'buf' are not valid under any version of the C standard prior to 1999.

    > if (len > 200) {
    > printf("ERROR - len > buf\n");
    > return -1;
    > }


    > memset(&buf[0],0,200);


    This would be more gracefully written as:
    memset(buf, 0, 200);

    > memcpy(&buf[1],(unsigned char *)&com,len);


    This would be more gracefully written as:

    memset(buff + 1, com, len);

    ....note no cast is needed on 'com', any type of pointer to object may
    be automatically converted to a pointer to void. Even in the other,
    not-C, language that I expect you are using.

    > printf("COM buf: <%s>\n",buf);
    >
    > Any ideas why there is junk at the beginning of my buffer?


    There is something seriously wrong if the output has anything other
    than white space after the ':' and the newline. buf[0] contains the
    string terminator, '\0'.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://c-faq.com/
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
    Jack Klein, Dec 29, 2005
    #7
  8. Jeff

    Chuck F. Guest

    Jeff wrote:
    >
    > Im trying to memcpy a buffer from a filled in simple structure.
    > When I memcpy and then print the resulting buffer, I see 7
    > locations that have junk before my data starts. My data
    > structure is:
    >
    > struct command_pkt {
    > char command_num[3];
    > char command[100];
    > };
    >
    > typedef command_pkt COMMAND;
    >
    > The relevant portion is the following:
    >
    > sprintf(tmp,"%s %s",ip,host);
    >
    > COMMAND *com;
    >

    .... snip ...
    >
    > Any ideas why there is junk at the beginning of my buffer?


    I looked no further than here. The typedef is meaningless. The
    structure name is "struct command_pkt". You should have gotten an
    error from the "COMMAND *com;" line. The exception being with a
    C++ compiler, but you wouldn't do such a silly thing as compile a C
    program with such a compiler, would you? Nor would you be so crass
    as to post C++ code on a C newsgroup, I assume.

    --
    "If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers." - Keith Thompson
    More details at: <http://cfaj.freeshell.org/google/>
    Chuck F., Dec 29, 2005
    #8
  9. Jeff

    Jeff Guest

    Test program w/malloc this time produces similiar results.
    compiler g++ gcc version 4.0.2 20051125 (Red Hat 4.0.2-8)
    Fedora Core 4


    #include <string.h>
    #include <stdlib.h>
    #include <stdio.h>
    #include "Packet.h"

    int main()
    {
    char out[] = "buffer data";

    COMMAND *com;
    com = (COMMAND *)malloc( sizeof(COMMAND) );
    strcpy(com->command,out);

    printf("\tCom send: %s\n",com->command);

    int len = sizeof(COMMAND);
    unsigned char buf[300];

    memcpy(&buf[0],(unsigned char *)&com,len);
    printf("COM SENDING: <%s>\n",com->command);
    printf("COM buf: <%s>\n",buf);

    return 0;
    }
    Jeff, Dec 29, 2005
    #9
  10. Jeff

    Jeff Guest

    Sorry, i realized a couple things after I posted this.

    Here's the fully independent version, also fixed char out
    issue vs COMMAND struct:

    #include <string.h>
    #include <stdlib.h>
    #include <stdio.h>

    struct command_pkt {
    char command[100];
    };

    int main()
    {
    char out[20];
    strcpy(out,"buffer data");

    struct command_pkt *com;
    com = (struct command_pkt *)malloc( sizeof(struct command_pkt) );
    strcpy(com->command,out);

    printf("\tCom send: %s\n",com->command);

    int len = sizeof(struct command_pkt);
    unsigned char buf[300];

    memcpy(&buf[0],(unsigned char *)&com,len);
    printf("COM SENDING: <%s>\n",com->command);
    printf("COM buf: <%s>\n",buf);

    return 0;
    }
    Jeff, Dec 29, 2005
    #10
  11. Jeff

    JimS Guest

    On 29 Dec 2005 14:50:02 -0800, "Jeff" <> wrote:

    >Sorry, i realized a couple things after I posted this.
    >
    >Here's the fully independent version, also fixed char out
    >issue vs COMMAND struct:
    >
    >#include <string.h>
    >#include <stdlib.h>
    >#include <stdio.h>
    >
    >struct command_pkt {
    > char command[100];
    >};
    >
    >int main()


    int main(void)
    would be better.

    >{
    > char out[20];
    > strcpy(out,"buffer data");


    You had
    char out[20]="buffer data";
    before, which was fine.

    > struct command_pkt *com;
    > com = (struct command_pkt *)malloc( sizeof(struct command_pkt) );

    com = malloc(sizeof *com);
    would be much better. You shouldn't cast the result of malloc because
    it hides the error of not #including stdlib.h (which you didn't make).
    Much better to use sizeof *com because if the type of com changes
    later, the code will still be correct.

    > strcpy(com->command,out);
    >
    > printf("\tCom send: %s\n",com->command);
    >
    > int len = sizeof(struct command_pkt);

    size_t len = sizeof(*com);
    would be better.

    > unsigned char buf[300];
    >
    > memcpy(&buf[0],(unsigned char *)&com,len);


    Why cast to unsigned char * when memcpy takes void *? All pointers in
    C are compatible with void *.
    Your bug is here anyway, it should be com, not &com you pass.
    memcpy(&buf[0],com,len);

    > printf("COM SENDING: <%s>\n",com->command);
    > printf("COM buf: <%s>\n",buf);
    >
    > return 0;
    >}


    Jim
    JimS, Dec 29, 2005
    #11
  12. Jeff

    Eric Sosman Guest

    Jeff wrote:
    > Sorry, i realized a couple things after I posted this.
    > [...]
    > struct command_pkt *com;
    > [...]
    > memcpy(&buf[0],(unsigned char *)&com,len);


    ITYM

    memcpy(&buf[0], com, len);

    or simply

    memcpy(buf, com, len);

    .... and you need to return to your C textbook and make
    sure you understand the difference between the pointer
    and the "pointee."

    --
    Eric Sosman
    lid
    Eric Sosman, Dec 29, 2005
    #12
  13. Artie Gold <> writes:
    > Jeff wrote:
    >> Im trying to memcpy a buffer from a filled in simple structure.
    >> When I memcpy and then print the resulting buffer, I see 7 locations
    >> that have junk before my data starts. My data structure is:
    >>
    >> struct command_pkt {
    >> char command_num[3];
    >> char command[100];
    >> };
    >>
    >> typedef command_pkt COMMAND;

    >
    > Huh? There's a type called `struct command_pkt' but not one (that *we*
    > can see, anyway) called `command_pkt'.
    >
    > This is why we say POST REAL CODE (cut'n'paste).


    It's quite possible that he did post real cut'n'pasted code, but not
    code that he fed to a C compiler. (C++ allows "struct command_pkt" to
    be referred to as just "command_pkt".)

    To the OP: Make sure you're using a C compiler, not a C++ compiler.
    They're two different (though similar) languages, and the differences
    can be significant.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Dec 29, 2005
    #13
  14. Jeff

    Jeff Guest

    Yes, sorry, I was using g++ and not gcc. When using gcc Ive gotten the
    following
    error:

    /tmp/ccNcK5bg.o(.eh_frame+0x11): undefined reference to
    `__gxx_personality_v0'
    collect2: ld returned 1 exit status

    and the last time I checked (a very long time ago) didnt know what it
    was about
    and didnt care much about how it was compiled, as long as it ran
    correctly, plus I
    was using C++ more than C at that time.

    So what is this ld error and how does one get rid of it? Quite
    descriptive as I
    dont have this symbol in my source of course.

    someone said something about:
    memcpy(&buf[0],com,len);

    I had worked on a server at one time where data was not at location
    zero
    all the time when it was sent (it was a circle buffer) and so I just
    explicitely
    set it here, but yes 'buf' would do just fine.
    Jeff, Dec 30, 2005
    #14
  15. "Jeff" <> writes:
    > Yes, sorry, I was using g++ and not gcc. When using gcc Ive gotten the
    > following
    > error:
    >
    > /tmp/ccNcK5bg.o(.eh_frame+0x11): undefined reference to
    > `__gxx_personality_v0'
    > collect2: ld returned 1 exit status
    >
    > and the last time I checked (a very long time ago) didnt know what
    > it was about and didnt care much about how it was compiled, as long
    > as it ran correctly, plus I was using C++ more than C at that time.
    >
    > So what is this ld error and how does one get rid of it? Quite
    > descriptive as I dont have this symbol in my source of course.


    If you've been reading this newsgroup for any time, you must have seen
    the advice to read <http://cfaj.freeshell.org/google/>. Read it now
    if you expect us to know what you're talking about.

    I don't know what that error message means. It's almost certainly
    nothing to do with the C language, which is what we discuss here.

    The string "gxx" *might* refer to g++. If so, you may be mixing C and
    C++ code. Make sure you're compiling everything as C.

    If that doesn't help, try the gnu.gcc.help newsgroup. Show them the
    exact code that you're compiling, the command line you use to compile
    it, the version of gcc you're using, and the system you're using. (We
    don't need most of that information here, but the gcc folks deal with
    system-specific issues.)

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Dec 30, 2005
    #15
  16. Jeff

    Artie Gold Guest

    Keith Thompson wrote:
    > Artie Gold <> writes:
    >
    >>Jeff wrote:
    >>
    >>>Im trying to memcpy a buffer from a filled in simple structure.
    >>>When I memcpy and then print the resulting buffer, I see 7 locations
    >>>that have junk before my data starts. My data structure is:
    >>>
    >>>struct command_pkt {
    >>> char command_num[3];
    >>> char command[100];
    >>>};
    >>>
    >>>typedef command_pkt COMMAND;

    >>
    >>Huh? There's a type called `struct command_pkt' but not one (that *we*
    >>can see, anyway) called `command_pkt'.
    >>
    >>This is why we say POST REAL CODE (cut'n'paste).

    >
    >
    > It's quite possible that he did post real cut'n'pasted code, but not
    > code that he fed to a C compiler. (C++ allows "struct command_pkt" to
    > be referred to as just "command_pkt".)


    A poster here actually compiling code as if it were C++? Say it ain't
    so... I couldn't *imagine* such a thing...

    ;-)

    >
    > To the OP: Make sure you're using a C compiler, not a C++ compiler.
    > They're two different (though similar) languages, and the differences
    > can be significant.
    >

    --ag

    --
    Artie Gold -- Austin, Texas
    http://goldsays.blogspot.com
    http://www.cafepress.com/goldsays
    "If you have nothing to hide, you're not trying!"
    Artie Gold, Dec 30, 2005
    #16
  17. Jeff

    Jeff Guest

    The error was due to my files being named .cc rather than .c
    apparently.
    Compiled with gcc and worked fine.

    Thanks
    Jeff, Dec 30, 2005
    #17
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Raja
    Replies:
    12
    Views:
    24,395
    John Harrison
    Jun 21, 2004
  2. Replies:
    8
    Views:
    8,157
    modemer
    Mar 21, 2005
  3. Juergen Wohnich

    memcpy a int to a char buffer ?

    Juergen Wohnich, Jan 23, 2006, in forum: C++
    Replies:
    6
    Views:
    674
    Henryk
    Jan 24, 2006
  4. Replies:
    2
    Views:
    603
    sergejusz
    Mar 26, 2007
  5. Jesse B.
    Replies:
    9
    Views:
    232
    Jesse B.
    Mar 27, 2010
Loading...

Share This Page