public inbox for gcc-prs@sourceware.org
help / color / mirror / Atom feed
* c/7334: SIGBUS in assignment and in memcpy()
@ 2002-07-17  1:26 glushko
  0 siblings, 0 replies; 2+ messages in thread
From: glushko @ 2002-07-17  1:26 UTC (permalink / raw)
  To: gcc-gnats


>Number:         7334
>Category:       c
>Synopsis:       SIGBUS in assignment and in memcpy()
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    unassigned
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Wed Jul 17 01:26:01 PDT 2002
>Closed-Date:
>Last-Modified:
>Originator:     Igor Glushko
>Release:        2.95.2 and 3.0.2
>Organization:
>Environment:
SunOS samson 5.6 Generic_105181-23 sun4d sparc SUNW,SPARCserver-1000
>Description:
Assigning a not-aligned field of a packed structure produces SIGBUS and a core dump. In more complex program than the one attached the same result got using memcpy() instead of assignment.
>How-To-Repeat:
compile the attached file
>Fix:
use byte-by-byte assignmet copying in a while loop without any optimization
>Release-Note:
>Audit-Trail:
>Unformatted:
----gnatsweb-attachment----
Content-Type: text/plain; name="sigbus_in_assignment.c"
Content-Disposition: inline; filename="sigbus_in_assignment.c"

#include <stdio.h>
#include <string.h>

static
void*
byte_memcpy(void* to, const void* from, size_t n)
{
  register char* t=(char*)to;
  register const char* fb=(const char*)from;
  register const char* fe=(const char*)from+n;
  while(fb<fe){
    *(t++)=*(fb++);
  }
}

static
void
copy(int* pi, int i)
{
  printf("copy(%p, %i): byte_memcpy(%p, %p, %u) ...\n", pi, i, pi, &i, sizeof(*pi));
  // this works always (only with -O0 compilation option):
  memcpy(pi, &i, sizeof(*pi));
  printf("copy(%p, %i): memcpy(%p, %p, %u) ...\n", pi, i, pi, &i, sizeof(*pi));
  // this works in this example, but does not in more complex cases:
  memcpy(pi, &i, sizeof(*pi));
  printf("copy(%p, %i): *(%p) = %i ...\n", pi, i, pi, i);
  // here SIGBUS are caught:
  *pi = i;
  printf("copy(%p, %i)!\n", pi, i);
}

// Make GCC to recognize the '#pragma pack' construction.
#define HANDLE_SYSV_PRAGMA 1

#pragma pack(1)
struct s // packed structure
{
  unsigned u;
  char c;
  int i;
};
#pragma pack()

int
main()
{
  struct s ss = {1, 'a', -1};

  printf("%u, %c, %i\n", ss.u, ss.c, ss.i);
  ss.u = 2;
  printf("%u, %c, %i\n", ss.u, ss.c, ss.i);
  ss.c = 'b';
  printf("%u, %c, %i\n", ss.u, ss.c, ss.i);
  // this works well:
  ss.i = -2;
  // does not return from copy():
  copy(&ss.i, -2);
  printf("%u, %c, %i\n", ss.u, ss.c, ss.i);

  return 0;
}


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: c/7334: SIGBUS in assignment and in memcpy()
@ 2002-07-17  2:08 nathan
  0 siblings, 0 replies; 2+ messages in thread
From: nathan @ 2002-07-17  2:08 UTC (permalink / raw)
  To: gcc-bugs, gcc-prs, glushko, nobody

Synopsis: SIGBUS in assignment and in memcpy()

State-Changed-From-To: open->closed
State-Changed-By: nathan
State-Changed-When: Wed Jul 17 02:08:32 2002
State-Changed-Why:
    not a bug. The compiler is allowed to presume that an
    int * points to a correctly aligned integer, and therefore
    use the most efficent access for that. sparc requires objects
    to be aligned, and gives a bus-error if they are not.
    you say your implementation of memcpy fails in some
    cases, but do not provide an example of that.

http://gcc.gnu.org/cgi-bin/gnatsweb.pl?cmd=view%20audit-trail&database=gcc&pr=7334


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2002-07-17  9:08 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-07-17  1:26 c/7334: SIGBUS in assignment and in memcpy() glushko
2002-07-17  2:08 nathan

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).