xref: /aosp_15_r20/external/ltp/lib/safe_stdio.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker /*
2*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) 2013 Cyril Hrubis <[email protected]>
3*49cdfc7eSAndroid Build Coastguard Worker  *
4*49cdfc7eSAndroid Build Coastguard Worker  * This program is free software; you can redistribute it and/or
5*49cdfc7eSAndroid Build Coastguard Worker  * modify it under the terms of the GNU General Public License as
6*49cdfc7eSAndroid Build Coastguard Worker  * published by the Free Software Foundation; either version 2 of
7*49cdfc7eSAndroid Build Coastguard Worker  * the License, or (at your option) any later version.
8*49cdfc7eSAndroid Build Coastguard Worker  *
9*49cdfc7eSAndroid Build Coastguard Worker  * This program is distributed in the hope that it would be useful,
10*49cdfc7eSAndroid Build Coastguard Worker  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11*49cdfc7eSAndroid Build Coastguard Worker  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12*49cdfc7eSAndroid Build Coastguard Worker  * GNU General Public License for more details.
13*49cdfc7eSAndroid Build Coastguard Worker  *
14*49cdfc7eSAndroid Build Coastguard Worker  * You should have received a copy of the GNU General Public License
15*49cdfc7eSAndroid Build Coastguard Worker  * along with this program; if not, write the Free Software Foundation,
16*49cdfc7eSAndroid Build Coastguard Worker  * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17*49cdfc7eSAndroid Build Coastguard Worker  */
18*49cdfc7eSAndroid Build Coastguard Worker 
19*49cdfc7eSAndroid Build Coastguard Worker #define _GNU_SOURCE
20*49cdfc7eSAndroid Build Coastguard Worker #include <stdarg.h>
21*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
22*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
23*49cdfc7eSAndroid Build Coastguard Worker #include "test.h"
24*49cdfc7eSAndroid Build Coastguard Worker #include "safe_stdio_fn.h"
25*49cdfc7eSAndroid Build Coastguard Worker 
safe_fopen(const char * file,const int lineno,void (cleanup_fn)(void),const char * path,const char * mode)26*49cdfc7eSAndroid Build Coastguard Worker FILE *safe_fopen(const char *file, const int lineno, void (cleanup_fn)(void),
27*49cdfc7eSAndroid Build Coastguard Worker                  const char *path, const char *mode)
28*49cdfc7eSAndroid Build Coastguard Worker {
29*49cdfc7eSAndroid Build Coastguard Worker 	FILE *f = fopen(path, mode);
30*49cdfc7eSAndroid Build Coastguard Worker 
31*49cdfc7eSAndroid Build Coastguard Worker 	if (f == NULL) {
32*49cdfc7eSAndroid Build Coastguard Worker 		tst_brkm_(file, lineno, TBROK | TERRNO, cleanup_fn,
33*49cdfc7eSAndroid Build Coastguard Worker 			"fopen(%s,%s) failed", path, mode);
34*49cdfc7eSAndroid Build Coastguard Worker 	}
35*49cdfc7eSAndroid Build Coastguard Worker 
36*49cdfc7eSAndroid Build Coastguard Worker 	return f;
37*49cdfc7eSAndroid Build Coastguard Worker }
38*49cdfc7eSAndroid Build Coastguard Worker 
safe_fclose(const char * file,const int lineno,void (cleanup_fn)(void),FILE * f)39*49cdfc7eSAndroid Build Coastguard Worker int safe_fclose(const char *file, const int lineno, void (cleanup_fn)(void),
40*49cdfc7eSAndroid Build Coastguard Worker                 FILE *f)
41*49cdfc7eSAndroid Build Coastguard Worker {
42*49cdfc7eSAndroid Build Coastguard Worker 	int ret;
43*49cdfc7eSAndroid Build Coastguard Worker 
44*49cdfc7eSAndroid Build Coastguard Worker 	ret = fclose(f);
45*49cdfc7eSAndroid Build Coastguard Worker 
46*49cdfc7eSAndroid Build Coastguard Worker 	if (ret == EOF) {
47*49cdfc7eSAndroid Build Coastguard Worker 		tst_brkm_(file, lineno, TBROK | TERRNO, cleanup_fn,
48*49cdfc7eSAndroid Build Coastguard Worker 			"fclose(%p) failed", f);
49*49cdfc7eSAndroid Build Coastguard Worker 	} else if (ret) {
50*49cdfc7eSAndroid Build Coastguard Worker 		tst_brkm_(file, lineno, TBROK | TERRNO, cleanup_fn,
51*49cdfc7eSAndroid Build Coastguard Worker 			"Invalid fclose(%p) return value %d", f, ret);
52*49cdfc7eSAndroid Build Coastguard Worker 	}
53*49cdfc7eSAndroid Build Coastguard Worker 
54*49cdfc7eSAndroid Build Coastguard Worker 	return ret;
55*49cdfc7eSAndroid Build Coastguard Worker }
56*49cdfc7eSAndroid Build Coastguard Worker 
safe_asprintf(const char * file,const int lineno,void (cleanup_fn)(void),char ** strp,const char * fmt,...)57*49cdfc7eSAndroid Build Coastguard Worker int safe_asprintf(const char *file, const int lineno, void (cleanup_fn)(void),
58*49cdfc7eSAndroid Build Coastguard Worker                   char **strp, const char *fmt, ...)
59*49cdfc7eSAndroid Build Coastguard Worker {
60*49cdfc7eSAndroid Build Coastguard Worker 	int ret;
61*49cdfc7eSAndroid Build Coastguard Worker 	va_list va;
62*49cdfc7eSAndroid Build Coastguard Worker 
63*49cdfc7eSAndroid Build Coastguard Worker 	va_start(va, fmt);
64*49cdfc7eSAndroid Build Coastguard Worker 	ret = vasprintf(strp, fmt, va);
65*49cdfc7eSAndroid Build Coastguard Worker 	va_end(va);
66*49cdfc7eSAndroid Build Coastguard Worker 
67*49cdfc7eSAndroid Build Coastguard Worker 	if (ret == -1) {
68*49cdfc7eSAndroid Build Coastguard Worker 		tst_brkm_(file, lineno, TBROK | TERRNO, cleanup_fn,
69*49cdfc7eSAndroid Build Coastguard Worker 			"asprintf(%s,...) failed", fmt);
70*49cdfc7eSAndroid Build Coastguard Worker 	} else if (ret < 0) {
71*49cdfc7eSAndroid Build Coastguard Worker 		tst_brkm_(file, lineno, TBROK | TERRNO, cleanup_fn,
72*49cdfc7eSAndroid Build Coastguard Worker 			"Invalid asprintf(%s,...) return value %d", fmt, ret);
73*49cdfc7eSAndroid Build Coastguard Worker 	}
74*49cdfc7eSAndroid Build Coastguard Worker 
75*49cdfc7eSAndroid Build Coastguard Worker 	return ret;
76*49cdfc7eSAndroid Build Coastguard Worker }
77*49cdfc7eSAndroid Build Coastguard Worker 
safe_popen(const char * file,const int lineno,void (cleanup_fn)(void),const char * command,const char * type)78*49cdfc7eSAndroid Build Coastguard Worker FILE *safe_popen(const char *file, const int lineno, void (cleanup_fn)(void),
79*49cdfc7eSAndroid Build Coastguard Worker 		 const char *command, const char *type)
80*49cdfc7eSAndroid Build Coastguard Worker {
81*49cdfc7eSAndroid Build Coastguard Worker 	FILE *stream;
82*49cdfc7eSAndroid Build Coastguard Worker 	const int saved_errno = errno;
83*49cdfc7eSAndroid Build Coastguard Worker 
84*49cdfc7eSAndroid Build Coastguard Worker 	errno = 0;
85*49cdfc7eSAndroid Build Coastguard Worker 	stream = popen(command, type);
86*49cdfc7eSAndroid Build Coastguard Worker 
87*49cdfc7eSAndroid Build Coastguard Worker 	if (stream == NULL) {
88*49cdfc7eSAndroid Build Coastguard Worker 		if (errno != 0) {
89*49cdfc7eSAndroid Build Coastguard Worker 			tst_brkm_(file, lineno, TBROK | TERRNO, cleanup_fn,
90*49cdfc7eSAndroid Build Coastguard Worker 				"popen(%s,%s) failed", command, type);
91*49cdfc7eSAndroid Build Coastguard Worker 		} else {
92*49cdfc7eSAndroid Build Coastguard Worker 			tst_brkm_(file, lineno, TBROK, cleanup_fn,
93*49cdfc7eSAndroid Build Coastguard Worker 				"popen(%s,%s) failed: Out of memory",
94*49cdfc7eSAndroid Build Coastguard Worker 				command, type);
95*49cdfc7eSAndroid Build Coastguard Worker 		}
96*49cdfc7eSAndroid Build Coastguard Worker 	}
97*49cdfc7eSAndroid Build Coastguard Worker 
98*49cdfc7eSAndroid Build Coastguard Worker 	errno = saved_errno;
99*49cdfc7eSAndroid Build Coastguard Worker 
100*49cdfc7eSAndroid Build Coastguard Worker 	return stream;
101*49cdfc7eSAndroid Build Coastguard Worker }
102