-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathexceptions.html
181 lines (139 loc) · 4.83 KB
/
exceptions.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, minimum-scale=1.0, maximum-scale=1.0">
<link rel="stylesheet" type="text/css" href="css/core.css" />
<link rel="stylesheet" type="text/css" href="css/prism.css" />
<title>Exceptions in C</title>
</head>
<body>
<div class="page">
<div class="container">
<div class="header">
<a href="contact.html" class="header-element">
Contact
</a>
<a href="about.html" class="header-element">
About
</a>
<a href="articles.html" class="header-element">
Articles
</a>
<a href="index.html" class="header-element">
Home
</a>
</div>
<h1>Implementing exceptions in C</h1>
<span class="date">Initial publication: March 8th, 2015</span>
<hr>
<p>
After reading about the <a href="http://acez.re/ps-vita-level-1-webkitties-3/">PS Vita webkit exploit</a>, I discovered the standard C functions: <code>setjmp</code> and <code>longjmp</code>, found in <code>setjmp.h</code>.
</p>
<p>
To understand what these functions do, take a look at the following example:
</p>
<div class="quote">
<p>
The example below shows the basic idea of setjmp. There, <code>main()</code> calls <code>first()</code>, which in turn calls <code>second()</code>. Then, <code>second()</code> jumps back into <code>main()</code>, skipping <code>first()</code>'s call of <code>printf()</code>.
</p>
<pre><code class="language-c">#include <stdio.h>
#include <setjmp.h>
static jmp_buf buf;
void second(void) {
printf("second\n"); // prints
longjmp(buf,1); // jumps back to where setjmp was called - making setjmp now return 1
}
void first(void) {
second();
printf("first\n"); // does not print
}
int main() {
if ( ! setjmp(buf) ) {
first(); // when executed, setjmp returns 0
} else { // when longjmp jumps back, setjmp returns 1
printf("main\n"); // prints
}
return 0;
}</code></pre>
<br>
<p>
When executed, the above program will output:
</p>
<pre><code>second
main</code></pre>
<br>
<p>
Notice that although the <code>first()</code> subroutine gets called, "<code>first</code>" is never printed. "<code>main</code>" gets printed as the conditional statement <code>if ( ! setjmp(buf) )</code> is executed a second time.
</p>
</div>
<div class="quote-reference">
- <a href="http://en.wikipedia.org/wiki/Setjmp.h">Wikipedia</a>
</div>
<br>
<br>
<p>
As you can see, this is not very maintainable code. It is also common for there to be differences between different implementations of the functions, making it something that you should avoid using.
</p>
<p>
What practical use could these functions ever have? Well, Wikipedia states that:
</p>
<div class="quote">
A typical use of <code>setjmp/longjmp</code> is implementation of an <a href="http://en.wikipedia.org/wiki/Exception_mechanism">exception mechanism</a>
</div>
<div class="quote-reference">
- <a href="http://en.wikipedia.org/wiki/Setjmp.h">Wikipedia</a>
</div>
<br>
<br>
<p>
So, I decided to see if I could get an exception handling implementation of my own, in C.
</p>
<p>
The library I wrote can be found on <a href="https://github.com/CTurt/Exception">GitHub</a>, it is used like so:
</p>
<pre><code class="language-c">/*
Program output:
Doing something...
Process failed due to error 1!
End of program
*/
#include <stdio.h>
#include "exception.h"
void process(void) {
printf("Doing something...\n");
throw(1);
}
int main(void) {
// Create a new exception
exception(
try() {
process();
// this never happens because process always throws error 1
printf("Got to end of process!\n");
}
// is executed when throw(1) is called within the try
catch(1) {
printf("Process failed due to error 1!\n");
}
// is executed when a throw is called within the try and there
// is no catch specific to the exception
finally() {
// use e to get the value passed to throw
printf("Process failed due to error %d!\n", e);
}
);
printf("End of program\n");
return 0;
}
</code></pre>
<br>
<p>
As you can see, the syntax is at least a little bit cleaner than using <code>setjmp</code> and <code>longjmp</code> directly, but I would still not recommend using this in any serious projects.
</p>
</div>
</div>
<script src="js/prism.js" type="text/javascript"></script>
</body>
</html>