Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > memcpy junk at beginning of buffer

Reply
Thread Tools

memcpy junk at beginning of buffer

 
 
Jeff
Guest
Posts: n/a
 
      12-29-2005
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?

 
Reply With Quote
 
 
 
 
Christopher Benson-Manica
Guest
Posts: n/a
 
      12-29-2005
Jeff <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
 
pemo
Guest
Posts: n/a
 
      12-29-2005

"Jeff" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
> 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.




 
Reply With Quote
 
Christopher Benson-Manica
Guest
Posts: n/a
 
      12-29-2005
Christopher Benson-Manica <(E-Mail Removed)> 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.
 
Reply With Quote
 
usenet@zevv.nl
Guest
Posts: n/a
 
      12-29-2005
> 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
 
Reply With Quote
 
Artie Gold
Guest
Posts: n/a
 
      12-29-2005
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!"
 
Reply With Quote
 
Jack Klein
Guest
Posts: n/a
 
      12-29-2005
On 29 Dec 2005 12:10:54 -0800, "Jeff" <(E-Mail Removed)> 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/~a...FAQ-acllc.html
 
Reply With Quote
 
Chuck F.
Guest
Posts: n/a
 
      12-29-2005
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/>
 
Reply With Quote
 
Jeff
Guest
Posts: n/a
 
      12-29-2005
Test program w/malloc this time produces similiar results.
compiler g++ gcc version 4.0.2 20051125 (Red Hat 4.0.2-
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;
}

 
Reply With Quote
 
Jeff
Guest
Posts: n/a
 
      12-29-2005
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;
}

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
index of string from beginning of line vs beginning of file Jesse B. Ruby 9 03-27-2010 04:04 PM
convert M bit buffer to N bit buffer runcyclexcski@yahoo.com C++ 2 03-26-2007 09:43 AM
memcpy a int to a char buffer ? Juergen Wohnich C++ 6 01-24-2006 06:24 PM
memcpy : maximum buffer size codefixer@gmail.com C++ 8 03-21-2005 05:35 PM
How to know the buffer size and increase buffer size in c++ Raja C++ 12 06-21-2004 06:21 PM



Advertisments