Operating System - HP-UX
1829013 Members
2344 Online
109986 Solutions
New Discussion

very simple code(5 lines only), why coredump?

 
SOLVED
Go to solution
proad
Advisor

very simple code(5 lines only), why coredump?


it's ok under WINDOWS & LINUX, but coredump under HP-UNIX, why?

#include
#include
int main(int argc, char *argv[])
{
char s1[5],s2[8];
strcpy(s1,"WKF"); //if remove this line, no coredump
int *p=(int*)s2;
p[0]=1234; //this line cause coredump
printf("%d\n",p[0]);
}

HP-UX hpltest B.11.11 U 9000/800 (tc)

link with "aCC -g -o t t.cpp". why?
and how to avoid coredump without modify my code? thanks very much.
12 REPLIES 12
David Johns
Advisor

Re: very simple code(5 lines only), why coredump?

Hello:

Try this:
cp tt.cpp tt.c
cc -g tt.c

When I run ./a.out, I get
1234

As a guess I'd say s1 is allocated 5 bytes on the stack, s2 is right behind it with 8 bytes. I'd be really surprised if the compiler didn't align s2 properly, but you never know. s2 may be byte aligned and an int is bigger, so the dereference (p[0]) may cause a bus error. (s2 needs to be on a word boundary.)

The command line option -o tt.cpp is telling aCC you want to create an object file, not an executable. Have you tried aCC -AA -g tt.cpp and running ./a.out ?

I see nothing wrong with the strcpy command, but you should always check the return value. Also, this is a C program, why are you using the C++ compiler?

Regards,
Dave
Biswajit Tripathy
Honored Contributor

Re: very simple code(5 lines only), why coredump?

proad,

> #include
> #include
> int main(int argc, char *argv[])
> {
> char s1[5],s2[8];
> strcpy(s1,"WKF"); //if remove this line, no coredump
> int *p=(int*)s2;
> p[0]=1234; //this line cause coredump
> printf("%d\n",p[0]);
> }

It dumps core because of incorrect alignment.
If you change the size of char s1[5] to s1[8],
the core dump will go away.

- Biswajit
:-)
Biswajit Tripathy
Honored Contributor

Re: very simple code(5 lines only), why coredump?

proad,

Couple of more comments.

You are writing a C code with some C++
syntax and compiling in a C++ compiler.
You probably know that this is not really a
good idea in real life.

Another thing is using char s2[] to store
integer. That's not a good idea either.

- Biswajit
:-)
proad
Advisor

Re: very simple code(5 lines only), why coredump?

first, thanks very much for your answers.

I wrote it in C++ because this code is just a demo of one of my another complex C++ program, which meet the same coredump. so do not compile it with "cc".


I store a number in a char[] is to store number in a big continuous memory in binary,in "sizeof(int)" bytes, the continuous memory also store other data.


I have tried to change the s1[] and s2[] size, but still coredump!





#include
#include
int main(int argc, char *argv[])
{
char s1[77],s2[88];
strcpy(s1,"WKF"); //if remove this line, no coredump
int *p=(int*)s2;
p[0]=1234; //this line cause coredump
printf("%d\n",p[0]);
}




thanks.
Biswajit Tripathy
Honored Contributor
Solution

Re: very simple code(5 lines only), why coredump?

proad,

> I have tried to change the s1[] and s2[] size, but still coredump!

I don't think you understood my reply; so let me be
more explicit. Make sure that the size of the array
s1[] is 8 byte alligned (i.e divisible by 8). In your new
code, you have used the s1[] size as 77. If you
change it to 80, then you will not encounter the bus
error.

- Biswajit
:-)
proad
Advisor

Re: very simple code(5 lines only), why coredump?

thanks very very much,
Biswajit Tripathy's reply is very helpful.
thanks. :)
Biswajit Tripathy
Honored Contributor

Re: very simple code(5 lines only), why coredump?

proad,
before you close the thread, I just want you
to understand the problem with this solution.
The solution is working solely because you are
assigning a suitable size to s1 to make sure that
address of s2 is 8 byte aligned. If someone
inserts another char array s3 between s1 and s2,
the program might again start to fail. To write a
program with better coding standard, you probably
need to use int arrays instead of char arrays or
check the compiler manual to see if the
c-preprocessor "pragma" directive can help you
align the data in a more predictable way.

- Biswajit
:-)
dirk dierickx
Honored Contributor

Re: very simple code(5 lines only), why coredump?

strcpy is also insecure programming practise.
use strncpy instead to prevent possible security hacks of your code in the future.
proad
Advisor

Re: very simple code(5 lines only), why coredump?

Biswajit Tripathy:
it's a good idea for me to use int arrays instead of char arrays in my program.
on the other hand, how to use "pragma" directive to align the data? would you please give me a simple example code?
thanks.
Biswajit Tripathy
Honored Contributor

Re: very simple code(5 lines only), why coredump?

proad,
sorry, it's been a long time since I used pragma in
a user level code, so don't remember the exact
directives; but the following URL might help you:

http://docs.hp.com/en/B3901-90015/ch03.html

- Biswajit
:-)
proad
Advisor

Re: very simple code(5 lines only), why coredump?

thanks very much,
Biswajit Tripathy's reply is always very helpful.

so I can close the thread now,
but maybe somebody else want to add comments,
so I'll close it later.
proad
Advisor

Re: very simple code(5 lines only), why coredump?

thanks to everybody!:)