# Shortest code that raises a SIGSEGV

Write the shortest code that raises a Segmentation Fault (SIGSEGV) in any programming language.

• Wow. Possibly the shortest successful question. – Matthew Roh Feb 9 '17 at 11:42

# JavaScript Shell, 7 bytes

clear()


Clears absolutely everything, not just the current scope which obviously causes lots of borks which result in JS blowing up and segfaulting

• According to MDN (document.clear), this should only do something in really old versions of Mozilla, and even then, what really segfaults in your experience? – tomsmeding Dec 16 '16 at 19:55
• @tomsmeding this is window.clear, FF directly doesn't reveal it, it's a spidermonkey built-in – Downgoat Dec 16 '16 at 19:56

## Ruby, 15 bytes

eval a='eval a'


Segfaults (Ruby 2.3 on Ubuntu xenial)

# x86 and x86_64 machine language, 3 bytes

0:       50                      push   %eax
1:       eb fd                   jmp    0


This pushes the value of the EAX (or RAX in long mode) register to the stack in a loop until the stack overflows.

To try this, compile and run the following C program.

const char main[]="\x50\xeb\xfd";


To try it on Windows, prepend the following to mark main as executable.

#pragma section("foo", execute)
__declspec(allocate("foo"))

• Main is not loaded in at address 0. As such, that jmp alone is causing the segfault. (I'm pretty certain,at least) – moonheart08 Mar 6 '18 at 19:25
• @moonheart08 it isn't jumping to absolute address 0, it's jumping to the push %eax. Jumping to absolute address 0 would require something like xor %eax, %eax; jmp *%eax – ceilingcat Mar 6 '18 at 20:42
• A long time later, i learned that the hard way (bugggssss!) Thanks. – moonheart08 Sep 5 '18 at 15:15

# Java (OpenJDK 9), 311227 223 bytes

import sun.misc.*;import java.lang.reflect.*;class M{public static void main(String[]args) throws Exception{Constructor<Unsafe> c=Unsafe.class.getDeclaredConstructor();c.setAccessible(true);c.newInstance().getAddress(0);}}



Try it online!

Ungolfed:

import sun.misc.Unsafe;

import java.lang.reflect.Constructor;

public class SegFault {

public static void main(String[] args) throws Exception {
Constructor<Unsafe> unsafeConstructor = Unsafe.class.getDeclaredConstructor();
unsafeConstructor.setAccessible(true);
Unsafe unsafe = unsafeConstructor.newInstance();
}
}


Saved 84 Bytes thanks to Mistah Figg

• It does the same as the Dyvil Answer below – Serverfrog Feb 9 '17 at 14:51
• Ask the OP. After searching, I think Access Violation and SegFault are maybe two names for the same thing. – mbomb007 Feb 9 '17 at 14:58
• TIO throws a SIGSEGV on OpenJDK, so I think its Possible that Oracle and OpenJDK throws same things but under different names. – Serverfrog Feb 9 '17 at 15:07
• Why the long names and import x.y.asdf; instead of import x.y.*; ? – MildlyMilquetoast Feb 14 '17 at 4:36
• Using java.lang.reflect.Constructor instead of importing it saves 9 bytes – Poke May 1 '17 at 17:16

# Lua 5.3.2 PUC-RIO (the "official") interpreter - 57 bytes

local t={}t.__newindex=t local y=setmetatable({},t)y[1]=1


Note: does not work on all machines, and was fixed in Lua 5.3.3.

# Clean, 19 12 bytes

Start=code{}


Try it online!

Every function always returns something in Clean, including Start. Because we haven't specified the type of Start, the compiler assumes (and it can only assume when you inline ABC) that it takes no arguments. Since it takes no arguments, there's nothing on either stack when the function resolves, and so the runtime tries to evaluate the first node in the spine of a graph with... Zero nodes.

• Nice, but the explanation is not entirely correct. The compiler knows the arity by the number of arguments of Start; there is no way to write a function in ABC that takes a number of arguments other than the number of arguments of the Clean function. On startup, a Start node is created. When it is evaluated, it is overwritten by _cycle_in_spine. The ABC code is supposed to push a new node on the stack, with which the _cycle_in_spine is then filled. But because it doesn't, the fill_a instruction then attempts to fill an uninitialised node above _cycle_in_spine. – user42682 Jan 8 '19 at 10:51
• @Keelan I learn something new every time you comment :). I'll edit that in once I'm back at my computer. – Οurous Jan 8 '19 at 16:03

# Rust, 34 bytes

fn main(){unsafe{*(0 as*mut _)=3}}


Just a null pointer assignment, nothing special here.

As a bonus, 45 44 40 bytes solution not using unsafe. Arguably a bug in a compiler.

#![no_main]#[no_mangle]static main:i8=0;


Main is usually a function ;).

$bts main.bts (segfaults)  # Whispers, 5 bytes >> 1!  Try it online! ## How it works >> 1 - Call this line repeated ! - Prevent tail call  # Common Lisp (SBCL), 79 bytes. SBCL captures pretty much every exception and signal, but we can cause an "Unhandled memory exception" which is the result of a SIGSEGV. We must tell SBCL to not consider type safety and just add a fixnum to a float, which ends up disastrous. (defun f(x)(declare (optimize (safety 0))(fixnum x))(the fixnum (1+ x)))(f 0.0)  My SBCL image errors with: Unhandled memory fault at #x14. [Condition of type SB-SYS:MEMORY-FAULT-ERROR]  Evaluating (f '(1 5)) returned a garbage object, then (gc) threw Lisp into the low-level debugger after it tried to GC that object presumably. I don't see the difference in results since it is possible to jump back into Lisp from this state, and I imagine this is 100% platform dependent behavior. # Malbolge, 3046 bytes Behold, an answer in Malbolge that you can embed inside an answer! bP&A@?>=<;:9876543210/.-,+*)('&%$T"!~}|;]yxwvutslUSRQ.yx+i)J9edFb4_^]\yxwRQ)(TSRQ]m!G0KJIyxFvDa%_@?"=<5:98765.-2+*/.-,+*)('&%$#"!~}|utyrqvutsrqjonmPkjihgfedc\DDYAA\>>Y;;V886L5322G//D,,G))>&&A##!7~5:{y7xvuu,10/.-,+*)('&%$#"yb}|{zyxwvutmVqSohmOOjihafeHcEaYAA\[ZYRW:U7SLKP3NMLK-I,GFED&%%@?>=6;|9y70/4u210/o-n+k)"!gg$#"!x}{zyxZvYtsrqSoRmlkjLhKfedcEaD_^]\>Z=XWVU7S6QPON0LKDI,GFEDCBA#?"=};438y6543s1r/o-&%*k('&%e#d!~}|^z]xwvuWsVqponPlOjihgIeHcbaB^A\[ZY;W:UTSR4PI2MLKJ,,AFE(&B;:?"~<}{zz165v3s+*/pn,mk)jh&ge#db~a_{^\xwvoXsrqpRnmfkjMKg_GG\aDB^A?[><X;9U86R53ONM0KJC,+FEDC&A@?!!6||3876w4-tr*/.-&+*)('&%$e"!~}|utyxwvutWlkponmlOjchgedGba_XW\?ZYRQVOT7RQPINML/JIHAFEDC&A@?>!<;{98yw5.-ss*/pn,+lj(!~ff{"ca}^z][wZXtWUqTRnQOkNLhgfIdcFaZ_^A\[Z<XW:U8SRQPOHML/JIHG*ED=%%:?>=~;:{876w43210/(-,+*)('h%$d"ca}|_z\rqYYnsVTpoRPledLLafIGcbEBXW??TY<:V97S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intel syntax), 12 chars

lgdt [0x123]


# How it works

You force the cpu to load a GDT table at a random address(0x123 in this case) , and of course it'll segfault. And if that location is actually a valid gdt, you should still trigger a GPF. Also, this might triple fault if you use it on msdos.

# Io, 25 bytes

Recursively executing the current file raises a segmentation fault. (You know, the system running Io obviously doesn't implement tail-call optimization.)

doFile(System args at(0))


Try it online!

# C++, 19 bytes.

int main(){main();}


Not very interesting, just stack overflow. (pun not intended)

# Swift 5, 29 characters:

func x(){y()}
let y={x()}
x()


# Go, 29 characters

package x;func x(y*int){*y=4}


# WebAssembly (WaWrapper), 12 bytes

(func \$main)


Try it online!

F90 - 25 chars

REAL A(1)
n=2
A(n)=0
END


Compiled with Intel compiler 12.0 which actually refuses to compile without the intermediate n as it detects the bound error. Does this count as a segfault?:

ifort -C sigsegv.F90
./a.out
forrtl: severe (408): fort: (2): Subscript #1 of the array A has value 2 which is greater than the upper bound of 1

• This isn't a segmentation fault unless it actually causes an OS-defined signal representing an access violation or similar. That error message looks like it was printed by the implementation, meaning that this answer is invalid. – user62131 Feb 14 '17 at 15:08

# TI-BASIC, 5 bytes

Archive A:A


Archives the variable A and then tries to get the value of A.
Throws the ERR:ARCHIVED error when used.

This is the closest thing you'd get to a segmentation fault in TI-BASIC, since archived data cannot be accessed directly.

In other words, the software has attempted to access a restricted area of memory.

Note: TI-BASIC is a tokenized language. Character count does not equal byte count.

• It's not a segmentation fault. – Sapphire_Brick Jun 11 at 14:45
• @Sapphire_Brick It most definitely is. "A segmentation fault occurs when a program attempts to access a memory location that it is not allowed to access, or attempts to access a memory location in a way that is not allowed (for example, attempting to write to a read-only location, or to overwrite part of the operating system)." (source) Archived data can't be accessed in TI-BASIC, simple as that. Do your research before commenting next time. – Tau Jun 17 at 0:53
• You seem to be saying "Fish can swim; I can swim, so I am a fish.". A segmentation fault is more specific than the definition you gave. It has to be the signal SIGSEGV, not just any memory permission error. Suppose you try to use a bash command that you don't have permission to use. Is that a segmentation fault? Of course not. – Sapphire_Brick Jun 17 at 1:06
• Fair enough. Though, you could've explained that in a way without trying to use a strawman like "Fish can swim; I can swim, so I am a fish." – Tau Jun 17 at 3:13