LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-06-2005, 08:42 AM   #1
rajendra.badapanda
LQ Newbie
 
Registered: Jul 2005
Posts: 6

Rep: Reputation: 0
Bus Error (core dumped) due to SIGBUS signal


Hi,

Please note the behaviour of this program. I know that SIGBUS comes because of address mis-alignments in read/write acesses.

could anybody please explain the behaviour of the following program:

void main()
{
short a;

scanf("%d",&a);
printf("\n a= %d",a);
}

case one:
chattan:~/junk]$ a.out
2 //input given
Bus Error (core dumped)
chattan:~/junk]$


case two:
chattan:~/junk]$ a.out
a //input given

a=
chattan:~/junk]$

Last edited by rajendra.badapanda; 07-14-2005 at 07:35 AM.
 
Old 07-06-2005, 08:52 AM   #2
vharishankar
Senior Member
 
Registered: Dec 2003
Distribution: Debian
Posts: 3,178
Blog Entries: 4

Rep: Reputation: 138Reputation: 138
The program has one problem.

Use a %d in the printf statement.

That's all. Otherwise it works well. And you need to return int in main not void.
This is my code and works well.
Code:
#include <stdio.h>
int main()
{
short a ;

scanf("%d",&a);
printf("\n a= %d\n",a);

return 0;
}

Last edited by vharishankar; 07-06-2005 at 08:53 AM.
 
Old 07-14-2005, 07:39 AM   #3
rajendra.badapanda
LQ Newbie
 
Registered: Jul 2005
Posts: 6

Original Poster
Rep: Reputation: 0
even that doesn't solve the problem

thanx Harishankar,

I modified the program as you suggested - actually i had missed that part.

anyway it didn't solve the problem

I'm using gcc 3.2 on a 64 bit SunOs Box.

follwing is the transcript of my session:

<< gurux001:~/junk $] cat manas.c
void main()
{
short a;

scanf("%d",&a);
printf("\n a= %d",a);
}
<< gurux001:~/junk $] gcc -v
Reading specs from /usr/local/lib/gcc-lib/sparc-sun-solaris2.8/3.2/specs
Configured with: ../configure --with-as=/usr/ccs/bin/as --with-ld=/usr/ccs/bin/ld --disable-nls
Thread model: posix
gcc version 3.2
<< gurux001:~/junk $] gcc manas.c
manas.c: In function `main':
manas.c:2: warning: return type of `main' is not `int'
<< gurux001:~/junk $] a.out
2
Bus Error (core dumped)
<< gurux001:~/junk $] a.out
a

a= 0<< gurux001:~/junk $]
 
Old 07-14-2005, 11:24 AM   #4
stumpster123
LQ Newbie
 
Registered: Jun 2005
Distribution: Slackware 10.1
Posts: 1

Rep: Reputation: 0
You probally forgot to return 0 at the end of your main function

Code:
#include <stdio.h>
int main()
{
short a ;

scanf("%d",&a);
printf("\n a= %d\n",a);

return 0; //Forgot to do this
}
Also if that still does not fix the problem you may want to use %h instead of %d, because %h is the flag for short. The bus error may be occuring cause it is trying to read and save a 4 byte integer into a 2 byte space and isnt converting right.

Last edited by stumpster123; 07-14-2005 at 11:33 AM.
 
Old 07-14-2005, 11:32 AM   #5
rstewart
Member
 
Registered: Feb 2005
Location: Sunnyvale, CA
Distribution: Ubuntu
Posts: 205

Rep: Reputation: 38
Hello,

The code:
Code:
void main()
{
    short a;

    scanf("%d",&a);
    printf("\n a= %d",a);
}
also works for me running Fedora Core 3 and gcc 3.4.3. Your post stated that you were using a 64 bit SunOs box. I wonder if the c library, or the gcc compiler for that port has a problem in either the scanf or the printf support functions when dealing with misaligned data types. For grins you could try either one (or both) of the following tests:

Code:
Test 1:

void main()
{
    int a;

    scanf("%d", &a);
    printf("\n a = %d\n", a);
}

Test 2:

void main()
{
    short a;

    scanf("%hd", &a);
    printf("\n a = %hd\n", a);
}
I would be interested in hearing of your results if you run those tests.
 
Old 07-16-2005, 02:55 AM   #6
rajendra.badapanda
LQ Newbie
 
Registered: Jul 2005
Posts: 6

Original Poster
Rep: Reputation: 0
Hi,

I'm sorry to reply late.

rstewart, regarding your queries both of your suggested changes work fine.

I was just wondering why the misaligned data types are giving bus error.

I have a doubt with stumpster's suggestion:

"The bus error may be occuring cause it is trying to read and save a 4 byte integer into a 2 byte space and isnt converting right."

c doesn't check for array bounds so it could have safely written over the 4 bytes starting from the address &a - then why it is giving this bus error ?

~Rajendra
 
Old 07-16-2005, 04:31 AM   #7
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Code:
short a ;
scanf("%d",&a);
is definitely buggy.
It should be either "int a;" or "scanf("%h",&a);"
Quote:
c doesn't check for array bounds so it could have safely written over the 4 bytes starting from the address &a - then why it is giving this bus error ?
You cannot write "safely" 4 bytes where only 2 are expected.
- first you are writing to undefined space
- alignment may matter depending on the architecture, what is yours ?
 
Old 07-18-2005, 03:01 AM   #8
rajendra.badapanda
LQ Newbie
 
Registered: Jul 2005
Posts: 6

Original Poster
Rep: Reputation: 0
Here are my arguments against the BUS error:

>You cannot write "safely" 4 bytes where only 2 are expected.
> - first you are writing to undefined space
R: So, at worst I should have got a segmentation fault, bt why SIGBus?

> - alignment may matter depending on the architecture, what is yours ?
R: mine is a 64 bit SunOs
 
Old 07-18-2005, 10:33 AM   #9
rstewart
Member
 
Registered: Feb 2005
Location: Sunnyvale, CA
Distribution: Ubuntu
Posts: 205

Rep: Reputation: 38
Hello,

Quote:
Here are my arguments against the BUS error:

>You cannot write "safely" 4 bytes where only 2 are expected.
> - first you are writing to undefined space
R: So, at worst I should have got a segmentation fault, bt why SIGBus?

> - alignment may matter depending on the architecture, what is yours ?
R: mine is a 64 bit SunOs
What CPU is running the 64 bit SunOS?

Alignment does matter depending upon the CPU and how misaligned data is handled. I remember that either the old Motorola 68030s or 68040s would throw a bus error exception if you tried to do a 32 bit data access and the memory address did not start on a "long word" boundary. My guess is that you are seeing the same sort of exception handling. I think that it is probably the scanf that was throwing the exception and not the printf, you could go back to your original code, remove the printf and try a test with just the scanf. You could also go back to your original code, compile it and have the compiler preserve the assembly source. Then recompile the code that works, having the compiler save the assembly code, and take a look at both versions of the assembly code to try and identify what the CPU is really trying to execute. Then take a look at the CPU's assembly code programmers manual and look up wha each of the instructions are supposed to do and what kind of exceptions they can throw. (Not sure if you want to take those steps, but it could go a long way to helping you understand the hardware architecture a little better )
 
Old 07-19-2005, 08:57 AM   #10
rajendra.badapanda
LQ Newbie
 
Registered: Jul 2005
Posts: 6

Original Poster
Rep: Reputation: 0
Thanks rstewart,

You have given quite a good number of pointers.

I got the following string running "uname -a" on my server.

"SunOS gurux001 5.8 Generic_117350-24 sun4u sparc SUNW,Sun-Fire-V490"

I used gdb to find out the addresses being allocated to a "short int" and an "int".

I saw that a short int is allocated memory at 2 byte boundary, and an int is allocated memory at 4-byte boundary.

some post I came across in the net said that to read a n-byte entity the starting address should be at n-byte boundary.

till now I have assumed the above statement to be true.

You have anything to add ...?

regards,
Rajendra
 
Old 07-19-2005, 10:18 AM   #11
rstewart
Member
 
Registered: Feb 2005
Location: Sunnyvale, CA
Distribution: Ubuntu
Posts: 205

Rep: Reputation: 38
Hello,

Quote:
I used gdb to find out the addresses being allocated to a "short int" and an "int".

I saw that a short int is allocated memory at 2 byte boundary, and an int is allocated memory at 4-byte boundary.

some post I came across in the net said that to read a n-byte entity the starting address should be at n-byte boundary.

till now I have assumed the above statement to be true.
Okay, that makes perfect sense. In your original failure case you had allocated a short int (thus being allocated on a 2 byte boundary), however your scanf was attempting to access an int (I presume that your ints are either going to be 4 or possibly even 8 bytes, but are definitely not 2 bytes) therefore that access should either require a 4 or an 8 byte address boundary. Since your data was defined using a 2 byte boundary, and your access to that memory was presuming a 4 or 8 byte boundary - bingo bus error exception.

Once you switched to either of the two examples I suggested, your access alignment matched your definition alignment and everything was okay.

I have programmed the 32 bit Sun Sparc CPUs, they are "quirky" when it comes to alignment problems. I think that the 64 bit versions are probably just as quirky if not more so.
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
squid stops due to signal 6 and signal 25 simplyrahul Linux - Software 3 05-28-2011 01:05 AM
Segementation Fault Error (Core Dumped) ??? Mistro116@yahoo.com Programming 8 10-17-2005 11:10 AM
signal Bus error (7) - apache/ httpd da_kidd_er Linux - Networking 1 07-08-2005 10:51 PM
Bus Error (core dumped) due to SIGBUS signal rajendra.badapanda Linux - Software 1 07-05-2005 12:10 PM
Guarddog won't run due to "Bus error" jonr Mandriva 0 08-06-2004 12:15 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:02 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration