Re: "Bus Error" related to compiler option
From: Michael Mair (Michael.Mair_at_invalid.invalid)
Date: 02/22/05
- Next message: Yitzik: "My computer getting stuck when I debug in Visual C++"
- Previous message: Christian Kandeler: "Re: "Bus Error" related to compiler option"
- In reply to: Chul Min Kim: ""Bus Error" related to compiler option"
- Next in thread: Giorgos Keramidas: "Re: "Bus Error" related to compiler option"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Tue, 22 Feb 2005 09:33:27 +0100
Chul Min Kim wrote:
> Hi,
>
> I got a BUS ERROR from one of my company's program.
> Let me briefly tell our environment.
>
> Machine : Sun E3500 (Ultra Sparc II 400Mhz CPU 4EA)
> OS : Solaris7
> Compiler : Sun Workshop 5.0 cc complier
>
> I get "BUS ERROR" only when I execute the binary which builds
> with "-g" complier option.
>
> However, the binary builds with "-O" option is ok. No Bus Error.
>
> I found FAQ realted to this "Bus Error". I have read the articles
> realted to this issue already. "Bus Error" is related to misallignment.
>
> My question is "why" bus error is not occurred with the binary built
> by "-O" complier option.
>
> Here is source code.
Provide a minimal example that gives no excess warnings.
>
> /* SAMPLE CODE - BEGIN */
> typedef struct _SVC10336_ {
> int a;
> char b;
> char c;
> } svc10336_t;
>
> main()
int main ()
or
int main (void)
> {
> char reqbuf[9454];
This buffer may be arbitrarily aligned.
>
> memset((char *)&reqbuf, 0x00, sizeof(reqbuf));
No prototype for memset up to now. #include <string.h>
> {
> int a = 100;
>
> memcpy(reqbuf, &a, 4);
Error: You assume that sizeof a is 4.
You want: memcpy(reqbuf, &a, sizeof a);
Note: Make reqbuf an array of unsigned char rather than char
to be guaranteed that this works as intended
Once again: #include <string.h>
> reqbuf[4] = '3';
You mean: reqbuf[offsetof(struct _SVC10336_, b)]
(at least if you want to build the same structure).
Use memcpy() for everything to be on the safe side.
> reqbuf[5] = '4';
dito
> }
>
> svc_start(reqbuf);
No prototype for this function.
return 0;
> }
>
> svc_start(char *reqbuf)
> {
> tr_svc10336(reqbuf);
No prototype for this function.
> }
>
> tr_svc10336(char *reqbuf)
> {
> svc10336_t *svc10336;
> int a;
> char b;
> char c;
>
> svc10336 = (svc10336_t *)reqbuf;
>
> a = svc10336->a;
Stupid error: svc10336_t may have to be X-aligned and reqbuf
can be 1-aligned, X>1.
Rule: If you memcpy() it into your buffer, also memcpy() it out
of it.
> b = svc10336->b;
> c = svc10336->c;
>
> printf("a[%d] b[%c] c[%c]\n", a,b,c);
No prototype so far: #include <stdio.h>
> }
> /* SAMPLE CODE - END */
>
>
> Here is the command that I build the binary.
>
> (1) $ cc -g -o sample sample.c
>
> this binary goes to "Bus Error".
>
> (2) $ cc -O -o sample sample.c
>
> this binary OK.
If your compiler does not provide any warnings for the above code,
turn up the warning level. If this is not possible, use *lint
(I recommend splint).
Cheers
Michael
-- E-Mail: Mine is a gmx dot de address.
- Next message: Yitzik: "My computer getting stuck when I debug in Visual C++"
- Previous message: Christian Kandeler: "Re: "Bus Error" related to compiler option"
- In reply to: Chul Min Kim: ""Bus Error" related to compiler option"
- Next in thread: Giorgos Keramidas: "Re: "Bus Error" related to compiler option"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|
|