Re: memcpy junk at beginning of buffer



On 29 Dec 2005 12:10:54 -0800, "Jeff" <jeep@xxxxxxxxx> 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
.



Relevant Pages

  • Re: memcpy junk at beginning of buffer
    ... >> Im trying to memcpy a buffer from a filled in simple structure. ... > This is why we say POST REAL CODE. ... Make sure you're using a C compiler, ...
    (comp.lang.c)
  • Re: Why is it dangerous?
    ... When I compile a program from our C course with a windows compiler ... Is linux more dangerous than windows? ... to the first character in a buffer, and stores an entire line from stdin ...
    (comp.lang.c)
  • Re: cast-as-lvalue (Thank You)
    ... other type -- pointing into the buffer, ... Of course, this fails to compile cleanly in any correct C compiler, ... the resulting machine code (on the SPARC or MIPS ... Of course, even this shorter version still generally fails at runtime, ...
    (comp.lang.c)
  • Re: VC 2003, WinXP and Win2000
    ... issue does not seen to occur at all under Win2k SP4. ... I am working with the wincrypt, and allocating a buffer to hold some ... first-chance exception under WinXP. ... memcpy() line to the following: ...
    (microsoft.public.vc.language)
  • Re: Efficient scather-gather-copy
    ... memcpythan external programs. ... I can't see that memcpy() would buy you much here, ... passes through the buffer: one to count the newlines and one to ... char *xlate (char *src, size_t len) ...
    (comp.lang.asm.x86)