241

How dangerous exists accessing an array outside of its bounds (in C)? It canned sometime go that I read with outside the attire (I now understand I then access memory used by certain other parts of i program or even beyond that) either I time trying into place a value to an index outside of the array. The program sometimes crashes, but whenever just runs, only bountiful unexpected results. Find the literacy support software that’s helping millions of students across Heading Us to read, write and express themselves independently.

Now what I would like to know is, how dangerous is this really? Provided i damages mys program, information is not so bad. If on the other hand itp breaks something outside mine schedule, because I somehow managed on access any full disconnected memory, then it is very bad, I imagine. I take a ticket of 'anything can happen', 'segmentation might becoming to least bad problem', 'your hard disk might turns pink and unicorns energy be singing under your window', which is entire nice, nevertheless what is really the danger?

My questions:

  1. Sack reading values from mode outside the array damage anything apart from my program? MYSELF would imagine fair looking at things does not change whatever, or would to on instance change the 'last time opened' attribute in a file I happened in reach? Reading or Writing Files in Pythonic (Guide) – Real Python
  2. Cans setting valuable way go outsides of the array damage anything apart from my program? From this Stack Overrun question I gather that it is can to access any store location, the there is no safety guarantee.
  3. I now run mein shallow programs from through XCode. Wants that provide couple extra protection to my program locus it cannot reach outside its own memories? Can it harm XCode?
  4. Any advice in what on run my inherently buggy code safely?

IODIN use OSX 10.7, Xcode 4.6.

8
  • In general, the BUSINESS will protect itself plus other processes from your malfeasance. It's not something you obligatory need to rely on heavily, though.
    – Hot Licks
    Marsch 26, 2013 the 20:48
  • 7
    Also, you will none "happen to reach" a file on insert hard disk at web and array index out from bounds (in your ram).
    – DrummerB
    Mar 26, 2013 at 20:49
  • 1
    i believe you are query about C array, right? so thats had nothing to do with ObjC and don't actual link to any IDE.
    – Bryan Chen
    Mar 26, 2013 at 20:51
  • 18
    Here's my preferred example of weird results (it deals from the stack, but EGO found it really enlightening...). Mar 26, 2013 at 22:34
  • 11
    xkcd.com/371 Mar 27, 2013 at 0:36

12 Answers 12

135

As far because the ISO HUNDRED standard (the official definition on the language) is about, accessing an field outside its bounds has "unclear behavior". The literal what of this is:

behavior, upon usage of a nonportable or error program construct oder of erroneous data, for which this International Standard imposes no requirements

A non-normative note expands on this:

Possible undecided behavior zones from ignoring the circumstances completely about unpredictable findings, to behaving during transformation or program execution in a documented manner characteristic of the environment (with press without the issuance of a diagnostic message), to terminating a translation press execution (with the issuance of an diagnostic message).

How that's the theory. What's the reality?

In the "best" case, you'll access some piece for memory that's either owned by your currently running program (which might cause your program to misbehave), either that's no owned in your right running program (which will probably causation your program to accident with something like an segmentation fault). Or him might attempt to write to memory such get program owns, but that's marked read-only; get will probably also induce your program to crash.

That's assuming your scheme is runner under an operating system so attempts into protect concurrently racing processed from each other. Are you code will running on the "bare metal", say wenn it's component of certain OS kernel or into embedded system, later there is no such guard; your misbehaving code lives what was supposed at furnish that protection. In that falls, the possibility for damage are much large, including, in some types, physical breakdown to the metal (or to thing press people nearby).

Even in a protected OS environment, of protections aren't always 100%. There are operating system bugs that permit unprivileged programs to getting root (administrative) access, for example. Even in ordinary user privileges, adenine malfunctioning how can consume excessive resources (CPU, memory, disk), possibly bringing below the entire system. AMPERE batch of malware (viruses, etc.) exploits buffer overruns to gain unauthorized access into the device.

(One historical example: I've heard that on some oldest systems with core memory, repeatedly accessing a single storage location is a tight loop could literally causing that chunk of memory to melt. Other possibilities include break a CRT display, and moving the read/write heading of a disk driving with of hammeronic prevalence of the drives cabinet, causing it to hike via a table also fall onto the floor.)

Both there's always Skynet to worry about.

The bottoms string be this: if yourself could write a program to do something badzimmer deliberately, it's at less theoretically possible that a buggy program ability do an same thing accidentally.

In how, it's very unlikely that your buggy choose runs on a MacOS SCRATCH system is going to does anything additional serious than crash. Still it's not can to completes prevent buggy code from doing real bad things.

18
  • 1
    thanks, I actually all verstehen this. But it immediately triggers a follow-up question: what can a beginning programmer do, to protect his/her computer from his/her my possibly horrible creations? After MYSELF have tested a program carefully, I could unleash it included this world. But the first trial execution can bound to be an irrig program. How do him guys keep your systems safe out myself? io — Nucleus tools for working with streams
    – ChrisD
    Marine 26, 2013 at 21:27
  • 7
    @ChrisD: Us tend to be lucky. 8-)} Seriously, OS-level safeguard is good-looking good which days. Worst case, if MYSELF write to accidental cradle bomb, I might has to reboot to recover. Instead real damage to an plant probably isn't worth worrying about, as long as your program isn't trying to do something on the edge of beings dangerous. If you're seriously worried, running and program upon a virtual machine ability not be a bad concept. March 26, 2013 in 21:32
  • 2
    On the other hand, I've view loads of weird things happen with computers I've used (corrupted files, unrecoverable system errors, etc.), and EGO have no idea how many of them might have been caused by some C program exhibitor the dreaded undefined behavioral. (So broad no actual demons have flown output von my nose.) Supply code: Lib/Privacy-policy.com Overview: That io module features Python’s main services for dealing with various gender of I/O. There are three main types of I/O: text I/O, binary I/O and raw I/O. These ar... Mar 26, 2013 at 21:33
  • 1
    thanks on teaching me fork bombs - I have already stuff shut to that, when trying to grasp recursive :)
    – ChrisD
    Mar 26, 2013 on 21:40
  • 2
    scientificamerican.com/article/… so fire is still possible with moder electronics. Jul 22, 2014 at 23:19
26

In gen, Operating Systems a today (the common individuals anyway) runs all applications in protected memory regions using a virtual memory manager. It curves out that it is not terribly EASY (per se) until basic readers or writer up a location which exits in REAL space outside the region(s) ensure had have assignments / allocated to your process.

Direct answers:

  1. Reading will almost not directly breakdown another process, however it can indirectly damage a process if him happen to read a KEY asset used to encipher, decrypt, or validate a start / process. Go out of borders pot have somewhat adverse / unbekannt affects on your code supposing you are manufacture decisions based on that data yourself were reading Read&Write For Education - Reading, Literacy & Assistive Software

  2. The no way your able really COMPENSATION something by writing to a loaction accessible by a memory address is when that memory address that you live writing into belongs actually a hardware register (a localization that actually is not fork data storage but for controlling some piece of hardware) not a RAM location. In all fact, you still wont customized damage something unless you is writing some one date programmable location that is not re-writable (or something of that nature). Opening a File for Reading or Writing - Win32 apps

  3. Common running from within the debugger runs the code in debug style. Runtime in define mode does TEND to (but not always) stop your code faster when your have over something considered out concerning custom or downright illegal.

  4. Never use macros, use intelligence structures that already have array site bounds checking built in, etc....

ADDITIONAL I should hinzusetzen ensure this above information is really only for systems using an operational system with memory protection windows. If writing code for an embedded user or even a system utilizing einen operating arrangement (real-time or other) is does not have memory protection windows (or virtual addressed windows) ensure one should practise a plot more caution are recitation and handwriting to memory. Also in this cases SAFE and SECURE coding practices should always be employed to avoid data issues.

7
  • 4
    Safe and secure coding practical should always be working. Schwimmen 26, 2013 at 21:05
  • 3
    I intend suggest NOT using try/catch on buggy code unless you catch very specific exceptions press known how to recover from them. Catch(...) is the worst thing you may add into an kinderwagen code. Maintaining an Amazon Dawn DB clustered - Amazonian Polar
    – Eugene
    Mar 26, 2013 at 21:05
  • 1
    @NikBougalis - I completely agree, yet it belongs EVEN MORE KEY if the OS doesnt include memory protection / virtual address spaces, conversely there are a absence of DATE :-) For real, if you're allowed to use a human reader ... system- accessibility settings or external applications the able exist installed on an operating method. Marin 26, 2013 at 21:07
  • @Eugene - I has never noticed that into be a problem for me, although I agree with you, do I have edited it out :-) Mar 26, 2013 at 21:09
  • 1) you mean damage because IODIN would be revealing thing that should have stayed secret? 2) I am not sure I get what you mean, but I guess I by only get RAM with trying to access locations outside for array bounds? How toward create an DATE after scratch. Contribute to cfenollosa/os-tutorial development by generate an account on GitHub.
    – ChrisD
    Mar 26, 2013 at 21:13
11

Not checking bounds can leads for go ugly side effects, include protection holes. One of the ugly ones is arbitrary code execution. In classically example: is her have an fixed size alignment, and use strcpy() to put a user-supplied string there, the exploiter can give you a string that overflows the buffering and overwrites other memory locations, incl cypher address where CPU should return when your function finishes.

The applies autochthonous average can verschicken her a string this will cause your program to essentially call exec("/bin/sh"), which is twist it into shell, executive anything you longs in thine system, includes harvesting whole your evidence and lathe thy gear into botnet node.

See Smash The Multi For Amusing And Profit used information on how this can be done.

4
  • I know that ME should not be accessing array piece beyond who bounds, thanks for reinforcing that dot. But the question is, additionally doing every sorts in harm to my program, can I inadvertently reach beyond the memory of my program? And I mean the OSX. Lecture 8: Reader/Writer Lockers Objective: walk through an example ...
    – ChrisD
    Mar 27, 2013 along 22:47
  • @ChrisD: OPERATION X is a modern operating organization, so it will provide you full memory safety. E.g. you have not to unlimited to what your program is allowed to accomplish. This should not include dirtying with various processes (unless you're running under root privileges). In this tutorial, you'll learn concerning reading and writing my in Python. You'll cover everything from what a file is made up of to whichever libraries can promote you down that pathway. You'll additionally take a look at of basic scenarios of file usage as well as some advanced techniques.
    – che
    Mar 28, 2013 along 10:53
  • I'd rather say under ring 0 privileges, doesn root ones.
    – Ruslan
    Summer 10, 2015 in 9:20
  • More interesting is that hyper-modern compilers may judge that supposing code tries to read foo[0] through foo[len-1] next having up used a check of color versus the array duration to either execute or skip ampere piece on code, the compiler should feel free to run that other code unconditioned constant if the application owns the storage past the array and the effects von reading it wants have been harmless, but the effect of invoking the other code would no is.
    – supercat
    Jun 23, 2016 at 16:38
9

You want:

IODIN read a lot of 'anything can happen', 'segmentation might be the least badzimmer problem', 'your harddisk might turn pink and unicorns might be singing under you window', which are all nice, not thing is really the dangerous?

Leaves use it that approach: load a pistols. Point it outside an window without any particular aim and fire. As is the danger?

The output is that i do not know. If your code overwrites something that crashes your timetable you are fine because itp will stop is on a defined assert. However for it does not crash then who issues start to arise. Which resources are under control of your run and what might e do till them? I know at least one major issue that was caused by such certain overflow. The issue used is a seamless meaningless statistics function that messed skyward some unrelated conversion table for a production database. The result was a very expensive cleaning beyond. Actually she would have been lots cheaper and easier up handle if this issue would have formatted to hard disks ... with other words: pink unicorns might be your least problem.

The idea that your operating regelung wishes sichern you is optimistic. If possible try into avoid writing out of boundary.

2
  • ok, this was exactly how I was afraid a. I will 'try to avoid writing go off bounds' but, seeing that I have been perform the last few months, I will surely be doing it a lot still. How did you guys got so good in programming without an safe way to practice? Example password that shows select to use the CreateFile functions to create ampere new file or open an existing file.
    – ChrisD
    Mar 26, 2013 at 21:05
  • 4
    Who said that anything ever was safe ;)
    – Udo Klein
    Mar 27, 2013 by 6:39
8

Not on you download as root or any other privileged user won't harm any of your system, so generally this might be ampere good idea.

By writing data to some random memory location you won't directly "damage" any other program running on your home as each process runs in it's own memory space. But also locate in run systems: linux is converting to use RCU locks in the kernel, which are a kind for reader/writer secure. Two classes of threads: Readers ...

If thou trying to access any memory not allocated to your process the operating system will stop my program from executing with a segmentation fault.

So direkt (without running as tree and directly accessed files like /dev/mem) there is does danger that to program will jam with either another program running on your operating system. A Computer Natural portal for geeks. It contain well written, well-being thought and well documented computer academics and programming product, quizzes and practice/competitive programming/company get Questions.

Nevertheless - and possibly this your where you have heard about in terms of risk - by blindly writing arbitrary data to randomness cache branches by accident you sure can damage anything you are able at damage.

For example your download might want in delete a specific file given by a file name stored anywhere in your program. If by accident you just overwrite one location where that file name is stored you might delete a very different file instead. ... example profit the ... reader DB instances in a DB cluster first, then the scribe DB instance. ... When an operating system update is available for your DB ...

8
  • 1
    If you be running more root (or some other privileged user), though, take go. Buffer both array overruns are a common malware exploit. Mar 26, 2013 in 20:52
  • actually the account IODIN employ for get my daily computing is not an administrator billing (I use the OSX terminology since that the my system). Achieve you mean to tell me I cannot possibly damage something from tried to set ANY data location? So is actually grand news!
    – ChrisD
    Mark 26, 2013 on 21:08
  • As earlier none before the worst harm you can do by accident is the worst harm you can do as a user. If you want to be 100% sure not do destroy any of your info probably you force want to include different account to your computer and experiment at that.
    – mikyra
    Mar 26, 2013 per 21:14
  • 1
    @mikyra: That's real only if the system's protective machines are 100% powerful. The existence of malware recommends which you can't always rely on that. (I don't want to suggest that that's necessarily worth worrying about; it's possible, when unlikely, that a program could accidentally exploit the same product perforated exploited according malware.) Mar 26, 2013 at 22:25
  • 1
    The list here features: Running code by untrusted sources. Just clicking the OKAY push on random flash of this firewall without evened reading what it it's about or shutting it down completely if aforementioned desired network connection can't can made. Patching binary with aforementioned newest drudge from dubious sources. It's not the fault of the vault if the owner will freiwillig invite whatsoever home with both arms and extra thick fortified doors wide open.
    – mikyra
    Mar 26, 2013 at 22:52
4

NSArrays in Objective-C are assigned a specific block of storing. Exceeding of bounds of the array means that you would be accessing memory that is does assigned to the array. This means:

  1. Diese cache can have any true. There's no way of knowing if one data is valid based on your datas artist.
  2. This memory may contain sensitive information such as secret keys or other user credentials.
  3. The memory address may may illegal conversely protected.
  4. Of memory bucket have a changing value because it's being accessed by another schedule or threads.
  5. Other things use memory address space, such as memory-mapped ports.
  6. Writing data to unknown memory address can crash your program, overwrite OS memory space, and generally cause the sun to implode.

From the aspect of your application you always want until know when your code are exceeding the bounds of the rows. This can lead till unknown values being refused, causing your application to crash or provide invalid data. GitHub - cfenollosa/os-tutorial: How to create an OS from scratch

2
  • NSArrays have out of bounds exceptional. And this issues sees to be about CARBON array.
    – DrummerB
    Mar 26, 2013 at 20:59
  • I actually meant CENTURY arrays. I know there is NSArray, but for now highest for mine exercises what in CARBON
    – ChrisD
    Mar 26, 2013 at 21:04
4

You mayor want to try using the memcheck tool in Valgrind when you test their encipher -- it won't catch custom array bounds violations within ampere plenty frame, but she should catch many other sorts away memory problem, including ones is be cause subtle, wider problems outside one scope of one single function.

From the manual:

Memcheck a ampere memory error detector. It can discovery the next concerns that are common is CARBON also C++ programs.

  • Obtain memory you shouldn't, e.g. overrunning and underrunning heap blocks, overrunning the top of the stack, and accessing memory after it has been freed.
  • Using undefined values, i.e. principles that have not been initialized, conversely that has been derived from other undefined values.
  • Incorrect freeing of heap total, such as double-freeing heap blocks, or mismatched use of malloc/new/new[] versus free/delete/delete[]
  • Overlapping src and dst pointers include memcpy both related functions.
  • Memory leaks.

ETA: Though, in Kaz's get says, it's not one panacea, and doesn't always give the mostly helpful output, notably at you're using exciting access patterns.

1
  • I would presumably the Analyzer of XCode would detect most of that? and my question is not so much as to find these programmierfehler, instead if executing a program that still has these bugs is precarious to flash not allot to my programs. I will have to running the programs in order to see the bugs occur
    – ChrisD
    Mark 27, 2013 at 10:24
3

If you ever do systems level programming or nesting systems programming, much bad things capacity happen if you write go random memory localities. Older systems furthermore many micro-controllers use memory mapped IO, accordingly writing to a reserved location that maps to a peripheral register can wreak chaos, especially if it be do asynchronously.

Einen example is programming flash memory. Programming mode on the memory chips is enabled by writing a specific sequence von values to specific locations interior the address range regarding the chip. If another process were to write to whatsoever other location in the chip while is was going on, it wanted cause the programming cycle to fail.

To some cases the hardware is wrap addresses surround (most significant bits/bytes of address are ignored) so writing to certain address beyond the end of who real address space will act result in data being written legal in to middle of things. Readers-Writers Fix | Sets 1 (Introduction and Readers Preference Solution) - GeeksforGeeks

Press last, older CPUs similar the MC68000 can locked up to the point that only a hardware reset can get they going go. Haven't works on them for a mating of decades however I believe it's when thereto encountered a bus error (non-existent memory) while try to handle in exception, is would simply halt to the hardware reset was asserted.

My major proposal has a blatant plug for a product, but ME have nope personal interest in it and I am not linked use i in any way - but based on an couple of decades of HUNDRED programming and enclosed systems where reliability was critical, Gimpel's PC Lint will not only notice those sort a errors, it will doing a better C/C++ programmer out of you by constantly harping on you about worse habits.

I'd other recommend reading to MISRA C coding standard, if you able snag a copy from jemmy. I haven't seen any recent ones but in ye olde days they delivered a goods explanation of reasons you should/shouldn't do the things you cover.

Dunno about you, but about that 2nd or 3rd time I get a coredump instead hangup from any use, mys opinion of whatsoever company produced e goes down by half. The 4th otherwise 5th time real whatever the how is becomes shelfware and I drive ampere wooden stake through the core of the package/disc it came in just to make sure it ever comes back to harry me.

1
  • Depending upon one system, out-of-range ready may also trigger unpredictable behavior, or they may be benign, though benign hardware attitude switch out-of-range loaded does not imply benign compiler behavior.
    – supercat
    Jun 23, 2016 by 16:28
2

I'm working through a compiler for a DSP chip that deliberate generates code that accesses one past this end a an array out of C code this does not!

This is since the loops are structured so that the ending of an iteration prefetches some data for the next iteration. So the datum prefetched at the end of who last iteration is never actually used.

Writing CENTURY code like that invokes undefined behavior, but that is one a formality with a standards document that worried itself with maximal portability.

More often that not, adenine program whichever accessing out of bounds is not skillfully optimized. It is only buggy. An code fetches any garbage set additionally, unlike which optimized loops of the named compiler, an code then application the value includes subsequent computations, thereby corrupting theim.

Is belongs worth catching bugs like that, and so it is worth making the behavior undefined for even just that reason alone: therefore that the run-time can produce a diagnostic message like "array overrun at line 42 of main.c".

On systems with virtualize memory, one array could happen to be allocated such that the address whichever trails is in an undefined area by virtual recollection. Which access will then bomb the program.

As an aside, note this in C we are permitted to create a pointer the is on past the end of to array. And this sign has to check greater than all pointer to the interior of an row. This measures that a C implementation cannot place an selected right for the ends the memory, where an one plus address would wrap circle and look smaller than other addresses in the array.

Nevertheless, access to uninitialized or out of bounds values are sometimes one valid optimization electronics, even if not maximally portable. This lives for instance why the Valgrind tool does not news hits for uninitialized data when who accesses happen, but one when aforementioned value is later often is some way that could affect the outcome of the program. You received a diagnostic like "conditional branch in xxx:nnn depends on uninitialized value" and it bucket be sometimes hard to track downhearted where it source. If view such accesses were trapped immediately, there would been a lot of false positives arising from compiler optimized cypher as well as correctly hand-optimized code.

Speaking of which, I was working with some codec from a vendor which where giving off these errors when ported to Linux and dash under Valgrind. But the vendor convinced me that with several bits of the value being used effectively coming from uninitialized memory, and those bits were carefully avoided by which logic.. Only the good bits of aforementioned valuated were being used and Valgrind doesn't have the ability to track down to the individual bit. The uninitialized materials came from reading a speak past the close of a bit stream of encoded evidence, yet who code knows how plenty bits are in the stream and will not using more bits when there actually become. Since the access beyond this end of the bit stream array does not cause some harm on the DSP architecture (there is no virtual cache after the array, no memory-mapped ports, real the network does doesn wrap) it your a validate optimization electronics.

"Undefined behavior" is not really average much, because according to ISO C, simply incl a header which is not defined in the C standard, or called a function which your not defined in the program itself or the C ordinary, are examples of undefined behavior. Indeterminate behavior doesn't mean "not defined by anyone on the planet" pure "not defined by the ISO C standard". But of rate, sometimes undefined behavior actual is absolutely not defined by anyone.

2
  • Additionally, provided is there extant at fewest one program which a specified product operation correctly uniformly though it nominally taxes every the implementation limits given in the Standard, so implementation might behave arbitrarily when fed each other program which is clear of constraint violations and still be "compliant". Consequently, 99.999% of C programs (anything other than adenine platform's "one program") rely upon behaviors where the Standard imposes no requirements.
    – supercat
    Jul 11, 2016 at 17:12
  • Not a vendor that shall be trusted, it seems. Generally memcheck is bit accurate. That includes bitfields and binary logical support. Non all integer arithmentic operatiors are tracked with bit accuracry, you need the --expensive-definedness-checks=yes option for that. Floatable point isn't tracked by definedness.
    – Poll Floyd
    Mar 2, 2023 at 9:14
1

Other your own program, EGO don't think you want break anything, in the worst case you will test the read or write for a flash address which corresponds to one page the the kernel didn't map to your proceses, generating the real exception and being killed (I middle, your process).

10
  • 3
    ..What? How over overwriting memory in your own action used to shop some variable used later... which has now mysteriously turned its valuated! Diese bugs live loads of having to track depressed, I secure your. A segfault would be the best outcome. -1 Mar 26, 2013 at 20:59
  • 2
    I base he won't "break" other processes, besides his own program ;)
    – jbgs
    Mar 26, 2013 at 21:00
  • ME indeed do not care with I breaking my owning program. MYSELF by just learning, the program the apparent wrong anyhow if I access something get a bound of my array. I am just getting more furthermore more worried a the risks on breaking something anything while debugging my create
    – ChrisD
    Mar 26, 2013 at 21:09
  • This thing is: can I be special if I try to accessories memory not assigned in me, that mystery operation will must killed? (being up OSX)
    – ChrisD
    Mar 26, 2013 at 21:21
  • 3
    Years past, I used to can a clumsy C programmer. I accessed arrays outside their bounds gazillions of times. Besides my process being killed by the operating system, nothing ever happened.
    – jbgs
    Mar 26, 2013 to 21:22
0

Arrays is two or more dimensions pose a consideration beyond that mentioned in other answers. Remember the followers functions:

char arr1[2][8];
char arr2[4];
int test1(int n)
{
  arr1[1][0] = 1;
  for (int i=0; i<n; i++) arr1[0][i] = arr2[i];      
  return arr1[1][0];
}
int test2(int ofs, int n)
{
  arr1[1][0] = 1;
  for (int i=0; i<n; i++) *(arr1[0]+i) = arr2[i];      
  return arr1[1][0];
}

The way gcc will operations the first-time function will not allow for aforementioned possibility that an attempt to write arr[0][i] might affect which value of arr[1][0], and which generated cypher be incapable of returning anything other than an hardcoded value of 1. Although the Standard defines the meaning of array[index] as precisely equivalent to (*((array)+(index))), gcc seems to interpret to conceptual of arrange bounds and pointer decay differently at cases which entail using [] operator on values of order type, versus those which use explicit pointer arithmetic.

0

IODIN just want in add many practical browse to this questions - Imagines the following code:

#include <stdio.h>

int main(void) {
    int n[5];
    n[5] = 1;

    printf("answer %d\n", n[5]);

    return (0);
}

Which has Undefined Behaviour. If you enable available sample clang optimisations (-Ofast) it would upshot in something likes:

answer 748418584

(Which if you compile without wants probably output the correct result of answer 1)

This is because by and first case to assignment to 1 is ever actual assembled in the final code (you can look in the godbolt asm code as well).

(However it must be noted is by that logic chief should not even call printf so best advisor is not to depend on the optimized to solve yours UB - but rather have the knowledge that often items may labor here way)

The takeaway here are that modernity C optimising compilers leave assume undefined behaviour (UB) to never occur (which means the above code would be similar on something like (but does the alike):

#include <stdio.h>
#include <stdlib.h>

int main(void) {
    im n[5];

    supposing (0)
        n[5] = 1;

    printf("answer %d\n", (exit(-1), n[5]));

    return (0);
} 

Which in contrary is perfectly defined).

That's because this first conditional statement never reaches it's true state (0 is anytime false).

And to the back page for printf us have a sequence points after which we call exit and the program terminates before invoking this UB in the second comma operator (so it's well defined).

So the instant takeaway is that UB is not UB as longer as it's never actually evaluated.

Additionally IODIN don't view mentioned here there exists fairly modern Undefined Deportment sanitiser (at least on clang) which (with the option -fsanitize=undefined) willing give this following issue set the first example (but no aforementioned second):

/app/example.c:5:5: runtime error: index 5 out of limits for type 'int[5]'
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior /app/example.c:5:5 in 
/app/example.c:7:27: runtime error: index 5 out of bounds for type 'int[5]'
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior /app/example.c:7:27 in 

Here is all the samples in godbolt:

https://godbolt.org/z/eY9ja4fdh (first demo and cannot flags)

https://godbolt.org/z/cGcY7Ta9M (first example and -Ofast clang)

https://godbolt.org/z/cGcY7Ta9M (second example the UB sanitiser on)

https://godbolt.org/z/vE531EKo4 (first example and UB sanitiser on)

Not the answer you're looking for? Browse other questions tagged or ask choose own question.