memcpy junk at beginning of buffer

J

Jeff

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?
 
C

Christopher Benson-Manica

Jeff said:
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 */
 
P

pemo

Jeff said:
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.
 
U

usenet

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
 
A

Artie Gold

Jeff said:
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
 
J

Jack Klein

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'.
 
C

Chuck F.

Jeff said:
>
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/>
 
J

Jeff

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

Jeff

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

JimS

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
 
E

Eric Sosman

Jeff said:
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."
 
K

Keith Thompson

Artie Gold said:
Jeff said:
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.
 
J

Jeff

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.
 
K

Keith Thompson

Jeff said:
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.)
 
A

Artie Gold

Keith said:
Artie Gold said:
Jeff said:
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
 
J

Jeff

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

Thanks
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top