Bug Summary

File:fc-query/fc-query.c
Location:line 190, column 5
Description:Potential leak of memory pointed to by 'format'

Annotated Source Code

1/*
2 * fontconfig/fc-query/fc-query.c
3 *
4 * Copyright © 2003 Keith Packard
5 * Copyright © 2008 Red Hat, Inc.
6 * Red Hat Author(s): Behdad Esfahbod
7 *
8 * Permission to use, copy, modify, distribute, and sell this software and its
9 * documentation for any purpose is hereby granted without fee, provided that
10 * the above copyright notice appear in all copies and that both that
11 * copyright notice and this permission notice appear in supporting
12 * documentation, and that the name of the author(s) not be used in
13 * advertising or publicity pertaining to distribution of the software without
14 * specific, written prior permission. The authors make no
15 * representations about the suitability of this software for any purpose. It
16 * is provided "as is" without express or implied warranty.
17 *
18 * THE AUTHOR(S) DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
19 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
20 * EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY SPECIAL, INDIRECT OR
21 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
22 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
23 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
24 * PERFORMANCE OF THIS SOFTWARE.
25 */
26
27#ifdef HAVE_CONFIG_H1
28#include <config.h>
29#else
30#ifdef linux
31#define HAVE_GETOPT_LONG1 1
32#endif
33#define HAVE_GETOPT1 1
34#endif
35
36#include <fontconfig/fontconfig.h>
37#include <fontconfig/fcfreetype.h>
38#include <stdio.h>
39#include <unistd.h>
40#include <stdlib.h>
41#include <string.h>
42
43#ifndef HAVE_GETOPT1
44#define HAVE_GETOPT1 0
45#endif
46#ifndef HAVE_GETOPT_LONG1
47#define HAVE_GETOPT_LONG1 0
48#endif
49
50#if HAVE_GETOPT_LONG1
51#undef _GNU_SOURCE
52#define _GNU_SOURCE
53#include <getopt.h>
54static const struct option longopts[] = {
55 {"ignore-blanks", 0, 0, 'b'},
56 {"index", 1, 0, 'i'},
57 {"format", 1, 0, 'f'},
58 {"version", 0, 0, 'V'},
59 {"help", 0, 0, 'h'},
60 {NULL((void*)0),0,0,0},
61};
62#else
63#if HAVE_GETOPT1
64extern char *optarg;
65extern int optind, opterr, optopt;
66#endif
67#endif
68
69static void
70usage (char *program, int error)
71{
72 FILE *file = error ? stderr__stderrp : stdout__stdoutp;
73#if HAVE_GETOPT_LONG1
74 fprintf (file, "usage: %s [-Vbh] [-i index] [-f FORMAT] [--ignore-blanks] [--index index] [--format FORMAT] [--version] [--help] font-file...\n",
75 program);
76#else
77 fprintf (file, "usage: %s [-Vbh] [-i index] [-f FORMAT] font-file...\n",
78 program);
79#endif
80 fprintf (file, "Query font files and print resulting pattern(s)\n");
81 fprintf (file, "\n");
82#if HAVE_GETOPT_LONG1
83 fprintf (file, " -b, --ignore-blanks ignore blanks to compute langauges\n");
84 fprintf (file, " -i, --index INDEX display the INDEX face of each font file only\n");
85 fprintf (file, " -f, --format=FORMAT use the given output format\n");
86 fprintf (file, " -V, --version display font config version and exit\n");
87 fprintf (file, " -h, --help display this help and exit\n");
88#else
89 fprintf (file, " -b (ignore-blanks) ignore blanks to compute languages\n");
90 fprintf (file, " -i INDEX (index) display the INDEX face of each font file only\n");
91 fprintf (file, " -f FORMAT (format) use the given output format\n");
92 fprintf (file, " -V (version) display font config version and exit\n");
93 fprintf (file, " -h (help) display this help and exit\n");
94#endif
95 exit (error);
96}
97
98int
99main (int argc, char **argv)
100{
101 int index_set = 0;
102 int set_index = 0;
103 int ignore_blanks = 0;
104 FcChar8 *format = NULL((void*)0);
105 FcBlanks *blanks = NULL((void*)0);
106 int err = 0;
107 int i;
108#if HAVE_GETOPT_LONG1 || HAVE_GETOPT1
109 int c;
110
111#if HAVE_GETOPT_LONG1
112 while ((c = getopt_long (argc, argv, "bi:f:Vh", longopts, NULL((void*)0))) != -1)
1
Loop condition is true. Entering loop body
5
Loop condition is false. Execution continues on line 138
113#else
114 while ((c = getopt (argc, argv, "bi:f:Vh")) != -1)
115#endif
116 {
117 switch (c) {
2
Control jumps to 'case 102:' at line 125
118 case 'b':
119 ignore_blanks = 1;
120 break;
121 case 'i':
122 index_set = 1;
123 set_index = atoi (optarg);
124 break;
125 case 'f':
126 format = (FcChar8 *) strdup (optarg);
3
Memory is allocated
127 break;
4
Execution continues on line 112
128 case 'V':
129 fprintf (stderr__stderrp, "fontconfig version %d.%d.%d\n",
130 FC_MAJOR2, FC_MINOR11, FC_REVISION95);
131 exit (0);
132 case 'h':
133 usage (argv[0], 0);
134 default:
135 usage (argv[0], 1);
136 }
137 }
138 i = optind;
139#else
140 i = 1;
141#endif
142
143 if (i == argc)
6
Taking false branch
144 usage (argv[0], 1);
145
146 if (!ignore_blanks)
7
Taking true branch
147 blanks = FcConfigGetBlanks (NULL((void*)0));
148 for (; i < argc; i++)
8
Loop condition is false. Execution continues on line 190
149 {
150 int index;
151 int count = 0;
152
153 index = set_index;
154
155 do {
156 FcPattern *pat;
157
158 pat = FcFreeTypeQuery ((FcChar8 *) argv[i], index, blanks, &count);
159 if (pat)
160 {
161 if (format)
162 {
163 FcChar8 *s;
164
165 s = FcPatternFormat (pat, format);
166 if (s)
167 {
168 printf ("%s", s);
169 FcStrFree (s);
170 }
171 }
172 else
173 {
174 FcPatternPrint (pat);
175 }
176
177 FcPatternDestroy (pat);
178 }
179 else
180 {
181 fprintf (stderr__stderrp, "Can't query face %d of font file %s\n",
182 index, argv[i]);
183 err = 1;
184 }
185
186 index++;
187 } while (!index_set && index < count);
188 }
189
190 FcFini ();
9
Potential leak of memory pointed to by 'format'
191 return err;
192}