• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /** \file test-null.c
2  * \brief Pass NULL values into libexif APIs and ensure it doesn't crash.
3  *
4  * Copyright (C) 2019 Dan Fandrich <dan@coneharvesters.com>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19  * Boston, MA  02110-1301  USA.
20  *
21  * SPDX-License-Identifier: LGPL-2.0-or-later
22  */
23 
24 #include "libexif/exif-data.h"
25 #include "libexif/exif-entry.h"
26 #include "libexif/exif-loader.h"
27 #include "libexif/exif-mnote-data.h"
28 
29 #include <stdio.h>
30 #include <stdlib.h>
31 
loader_null_test(void)32 static void loader_null_test(void)
33 {
34 	ExifLoader *l;
35 	ExifData *d;
36 	unsigned char ret;
37 	const unsigned char *ccp;
38 	unsigned int i;
39 
40 	l = exif_loader_new_mem(NULL);
41 	if (l) {
42 		fprintf(stderr, "Unexpected success in %s\n", "exif_loader_new_mem");
43 		exit(13);
44 	}
45 
46 	exif_loader_ref(NULL);
47 
48 	exif_loader_unref(NULL);
49 
50 	exif_loader_write_file(NULL, "test");
51 
52 	exif_loader_write_file(NULL, NULL);
53 
54 	ret = exif_loader_write(NULL, (unsigned char *)"x", 1);
55 	if (ret) {
56 		fprintf(stderr, "Unexpected success in %s\n", "exif_loader_write");
57 		exit(13);
58 	}
59 
60 	exif_loader_write(NULL, NULL, 123);
61 
62 	exif_loader_reset(NULL);
63 
64 	d = exif_loader_get_data(NULL);
65 	if (d) {
66 		fprintf(stderr, "Unexpected success in %s\n", "exif_loader_get_data");
67 		exit(13);
68 	}
69 
70 	exif_loader_get_buf(NULL, NULL, NULL);
71 
72 	exif_loader_log(NULL, NULL);
73 
74 	l = exif_loader_new();
75 	if (!l) {
76 		fprintf(stderr, "Out of memory\n");
77 		exit(13);
78 	}
79 
80 	exif_loader_write_file(l, NULL);
81 
82 	exif_loader_write(l, NULL, 0);
83 	exif_loader_write(l, NULL, 1);
84 
85 	exif_loader_get_buf(l, NULL, NULL);
86 	exif_loader_get_buf(l, &ccp, NULL);
87 	exif_loader_get_buf(l, NULL, &i);
88 
89 	exif_loader_log(l, NULL);
90 
91 	exif_loader_unref(l);
92 }
93 
data_null_test(void)94 static void data_null_test(void)
95 {
96 	/* exif_data_new_from_file() is untested since it doesn't check path */
97 
98 	ExifData *d;
99 	ExifByteOrder bo;
100 	ExifMnoteData *m;
101 	ExifDataType dt;
102 	unsigned char *buf;
103 	unsigned int len;
104 
105 	d = exif_data_new_mem(NULL);
106 	if (d) {
107 		fprintf(stderr, "Unexpected success in %s\n", "exif_data_new_mem");
108 		exit(13);
109 	}
110 
111 	d = exif_data_new_from_data(NULL, 123);
112 	if (d) {
113 		exif_data_unref(d);
114 	}
115 
116 	bo = exif_data_get_byte_order(NULL);
117 	(void) bo;
118 
119 	exif_data_set_byte_order(NULL, EXIF_BYTE_ORDER_MOTOROLA);
120 
121 	m = exif_data_get_mnote_data(NULL);
122 	if (m) {
123 		fprintf(stderr, "Unexpected success in %s\n", "exif_data_get_mnote_data");
124 		exit(13);
125 	}
126 
127 	exif_data_fix(NULL);
128 
129 	exif_data_foreach_content(NULL, NULL, NULL);
130 
131 	exif_data_set_option(NULL, EXIF_DATA_OPTION_FOLLOW_SPECIFICATION);
132 
133 	exif_data_unset_option(NULL, EXIF_DATA_OPTION_FOLLOW_SPECIFICATION);
134 
135 	exif_data_set_data_type(NULL, EXIF_DATA_TYPE_UNCOMPRESSED_CHUNKY);
136 
137 	dt = exif_data_get_data_type(NULL);
138 	(void) dt;
139 
140 	exif_data_load_data(NULL, NULL, 123);
141 
142 	exif_data_save_data(NULL, NULL, NULL);
143 
144 	exif_data_log(NULL, NULL);
145 
146 	exif_data_dump(NULL);
147 
148 	exif_data_ref(NULL);
149 
150 	exif_data_unref(NULL);
151 
152 	d = exif_data_new();
153 	if (!d) {
154 		fprintf(stderr, "Out of memory\n");
155 		exit(13);
156 	}
157 
158 	exif_data_load_data(d, NULL, 123);
159 
160 	exif_data_save_data(d, NULL, &len);
161 	exif_data_save_data(d, &buf, NULL);
162 	exif_data_save_data(d, NULL, NULL);
163 
164 	exif_data_foreach_content(d, NULL, NULL);
165 
166 	exif_data_log(d, NULL);
167 
168 	exif_data_unref(d);
169 }
170 
content_null_test(void)171 static void content_null_test(void)
172 {
173 	ExifContent *c;
174 	ExifIfd i;
175 
176 	c = exif_content_new_mem(NULL);
177 	if (c) {
178 		fprintf(stderr, "Unexpected success in %s\n", "exif_content_new_mem");
179 		exit(13);
180 	}
181 
182 	exif_content_ref(NULL);
183 
184 	exif_content_unref(NULL);
185 
186 	exif_content_free(NULL);
187 
188 	exif_content_get_entry(NULL, EXIF_TAG_COMPRESSION);
189 
190 	exif_content_fix(NULL);
191 
192 	exif_content_foreach_entry(NULL, NULL, NULL);
193 
194 	i = exif_content_get_ifd(NULL);
195 	(void) i;
196 
197 	exif_content_dump(NULL, 0);
198 
199 	exif_content_add_entry(NULL, NULL);
200 
201 	exif_content_remove_entry(NULL, NULL);
202 
203 	exif_content_log(NULL, NULL);
204 
205 	c = exif_content_new();
206 	if (!c) {
207 		fprintf(stderr, "Out of memory\n");
208 		exit(13);
209 	}
210 
211 	exif_content_add_entry(c, NULL);
212 
213 	exif_content_remove_entry(c, NULL);
214 
215 	exif_content_log(c, NULL);
216 
217 	exif_content_foreach_entry(c, NULL, NULL);
218 
219 	exif_content_unref(c);
220 }
221 
entry_null_test(void)222 static void entry_null_test(void)
223 {
224 	ExifEntry *e;
225 	const char *v = NULL;
226 	char buf[] = {0};
227 	ExifData *d;
228 
229 	e = exif_entry_new_mem(NULL);
230 	if (e) {
231 		fprintf(stderr, "Unexpected success in %s\n", "exif_entry_new_mem");
232 		exit(13);
233 	}
234 
235 	exif_entry_ref(NULL);
236 
237 	exif_entry_unref(NULL);
238 
239 	exif_entry_free(NULL);
240 
241 	exif_entry_initialize(NULL, EXIF_TAG_COMPRESSION);
242 
243 	exif_entry_fix(NULL);
244 
245 	v = exif_entry_get_value(NULL, NULL, 123);
246 	if (v) {
247 		fprintf(stderr, "Unexpected success in %s\n", "exif_entry_get_value");
248 		exit(13);
249 	}
250 
251 	v = exif_entry_get_value(NULL, buf, 1);
252 	if (v != buf) {
253 		fprintf(stderr, "Unexpected value in %s\n", "exif_entry_get_value");
254 		exit(13);
255 	}
256 
257 	exif_entry_dump(NULL, 0);
258 
259 	/* Creating a plain ExifEntry isn't enough, since some functions require
260 	 * that it exists in an IFD.
261 	 */
262 	d = exif_data_new();
263 	if (!d) {
264 		fprintf(stderr, "Out of memory\n");
265 		exit(13);
266 	}
267 	/* Create the mandatory EXIF fields so we have something to work with */
268 	exif_data_fix(d);
269 	e = exif_content_get_entry (d->ifd[EXIF_IFD_0], EXIF_TAG_X_RESOLUTION);
270 
271 	(void) exif_entry_get_value(e, NULL, 0);
272 	(void) exif_entry_get_value(e, NULL, 123);
273 
274 	exif_data_unref(d);
275 }
276 
mnote_null_test(void)277 static void mnote_null_test(void)
278 {
279 	/* Note that these APIs aren't tested with a real ExifMnoteData pointer
280 	 * because it's impossible to create one without real data.
281 	 */
282 	exif_mnote_data_ref(NULL);
283 
284 	exif_mnote_data_unref(NULL);
285 
286 	exif_mnote_data_load(NULL, NULL, 123);
287 
288 	exif_mnote_data_load(NULL, (const unsigned char *)"x", 1);
289 
290 	exif_mnote_data_save(NULL, NULL, NULL);
291 
292 	exif_mnote_data_count(NULL);
293 
294 	exif_mnote_data_get_id(NULL, 0);
295 
296 	exif_mnote_data_get_name(NULL, 0);
297 
298 	exif_mnote_data_get_title(NULL, 0);
299 
300 	exif_mnote_data_get_description(NULL, 0);
301 
302 	exif_mnote_data_get_value(NULL, 0, NULL, 123);
303 
304 	exif_mnote_data_log(NULL, NULL);
305 }
306 
log_null_test(void)307 static void log_null_test(void)
308 {
309 	ExifLog *l;
310 	static va_list va;
311 
312 	l = exif_log_new_mem(NULL);
313 	if (l) {
314 		fprintf(stderr, "Unexpected success in %s\n", "exif_log_new_mem");
315 		exit(13);
316 	}
317 
318 	exif_log_ref(NULL);
319 
320 	exif_log_unref(NULL);
321 
322 	exif_log_free(NULL);
323 
324 	exif_log_set_func(NULL, NULL, NULL);
325 
326 	exif_log(NULL, EXIF_LOG_CODE_CORRUPT_DATA, "XXX", "YYY");
327 
328 	exif_logv(NULL, EXIF_LOG_CODE_CORRUPT_DATA, "XXX", "YYY", va);
329 
330 	l = exif_log_new();
331 	if (!l) {
332 		fprintf(stderr, "Out of memory\n");
333 		exit(13);
334 	}
335 
336 	exif_log_set_func(l, NULL, NULL);
337 
338 	exif_log_unref(l);
339 }
340 
main(void)341 int main(void)
342 {
343 	loader_null_test();
344 	data_null_test();
345 	content_null_test();
346 	entry_null_test();
347 	mnote_null_test();
348 	log_null_test();
349 
350 	/* If it gets here, we didn't get a SIGSEGV, so success! */
351 	return 0;
352 }
353