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