1 #define NULL ((void*)0)
2 typedef __SIZE_TYPE__ size_t;
3 
4 extern int errno;
5 
6 extern void free (void *);
7 char *strdup (const char *)
8   __attribute__((malloc (free)));
9 
10 extern size_t strlen (const char *__s)
11   __attribute__ ((__nothrow__ , __leaf__))
12   __attribute__ ((__pure__))
13   __attribute__ ((__nonnull__ (1)));
14 
15 extern void error (int __status, int __errnum, const char *__format, ...)
16      __attribute__ ((__format__ (__printf__, 3, 4)));
17 
18 extern void error_at_line (int __status, int __errnum, const char *__fname,
19 			   unsigned int __lineno, const char *__format, ...)
20      __attribute__ ((__format__ (__printf__, 5, 6)));
21 
22 /* PR analyzer/99196; extract taken from
23      https://github.com/libguestfs/libguestfs/blob/f19fd566f6387ce7e4d82409528c9dde374d25e0/daemon/tar.c#L108
24    (which is GPLv2 or later).  */
25 
26 extern char *read_whole_file (const char *error_file, size_t *out);
27 
28 #define EXIT_FAILURE 1
29 
read_error_file(const char * error_file)30 char *read_error_file (const char *error_file)
31 {
32   size_t len;
33   char *str;
34 
35   str = read_whole_file (error_file, &len);
36   if (str == NULL) {
37     str = strdup ("(no error)");
38     if (str == NULL)
39       error (EXIT_FAILURE, errno, "strdup");
40     len = strlen (str); /* { dg-bogus "NULL" } */
41   }
42 
43   /* Remove trailing \n character if any. */
44   if (len > 0 && str[len-1] == '\n')
45     str[--len] = '\0';
46 
47   return str;                   /* caller frees */
48 }
49