컴퓨팅에서 버스 오류(bus error)는 하드웨어에 의해 발생되는 폴트(fault)로서, 중앙 처리 장치가 물리적으로 주소 할당을 할 수 없는 메모리에 프로세스가 접근을 시도한다는 것을 운영 체제(OS)에 알린다. 주소 버스의 유효하지 않은 주소이기 때문에 이러한 이름이 붙여졌다. 현대의 대부분의 아키텍처에서는 주로 메모리 주소 위반(논리 주소 또는 권한의 문제)으로 인해 발생하는 세그멘테이션 폴트보다는 그 빈도가 훨씬 더 적다.

POSIX 준수 플랫폼에서 버스 오류는 보통 SIGBUS 신호를 발생시키며 이는 그 오류를 발생시킨 프로세스에 전달된다. SIGBUS는 또한 컴퓨터가 발견하는 일반적인 장치 실패에 의해 발생할 수도 있으나 버스 오류는 드물게는 컴퓨터 하드웨어가 물리적으로 망가졌음을 의미하기도 한다. 보통은 소프트웨어버그에 의해 발생한다. 버스 오류는 다른 페이징 오류로 인해 발생할 수도 있다.

예시 편집

AT&T 어셈블리 문법의 C 프로그래밍 언어로 작성된, 비정렬 메모리 접근의 한 예이다.

#include <stdlib.h>

int main(int argc, char **argv) 
{
    int *iptr;
    char *cptr;
    
#if defined(__GNUC__)
# if defined(__i386__)
    /* Enable Alignment Checking on x86 */
    __asm__("pushf\norl $0x40000,(%esp)\npopf");
# elif defined(__x86_64__) 
     /* Enable Alignment Checking on x86_64 */
    __asm__("pushf\norl $0x40000,(%rsp)\npopf");
# endif
#endif

    /* malloc() always provides memory which is aligned for all fundamental types */
    cptr = malloc(sizeof(int) + 1);
    
    /* Increment the pointer by one, making it misaligned */
    iptr = (int *) ++cptr;

    /* Dereference it as an int pointer, causing an unaligned access */
    *iptr = 42;

    /*
       Following accesses will also result in sigbus error.
       short *sptr;
       int    i;

       sptr = (short *)&i;
       // For all odd value increments, it will result in sigbus.
       sptr = (short *)(((char *)sptr) + 1);
       *sptr = 100;
    
    */

    return 0;
}