Re: "Bus Error" related to compiler option

From: Michael Mair (Michael.Mair_at_invalid.invalid)
Date: 02/22/05


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.


Relevant Pages

  • Re: Can u tell me the explanation reg this problem
    ... value although it's implicitly declared to return int and there isn't a ... proper prototype of printf in scope), ... understood as const char*, ... termination status returned by the program to the operating system ...
    (comp.lang.c)
  • Re: reply the answer
    ... need a prototype for putsbecause it returns int and has no ... const char *, not char *. ... prototype is in scope that says it is okay to pass an argument ... Whether the definition of puts includes a prototype is not ...
    (comp.lang.c)
  • Re: Function arguments and their size
    ... >void f(int a, char b); ... As long as there is a prototype in scope, ... would pass a double to a function that needs an int, ...
    (comp.lang.c)
  • Re: integer promotions
    ... The f's prototype is provided, then an one-byte copy of x is passed to ... No, the value of 'x' is passed as a char, which is one byte by ... The prototype is not provided, then a four-byte int, which contains the ... is passed to f(unix/intel platform). ...
    (comp.lang.c)
  • Re: Convert a binary value to unsigned decimal value
    ... sprintf("%u", (unsigned int) x); ... > I use memcpy into an unsigned char variable, ... It's almost certain that char on your machine is 8 bits. ...
    (comp.lang.c)