提交 3530cc2d 编写于 作者: B Behdad Esfahbod

[util] Fix option-parsing leaks

上级 642135f3
......@@ -349,10 +349,13 @@ helper_cairo_create_context (double w, double h,
unsigned int fr, fg, fb, fa, br, bg, bb, ba;
const char *color;
br = bg = bb = 0; ba = 255;
sscanf (view_opts->back + (*view_opts->back=='#'), "%2x%2x%2x%2x", &br, &bg, &bb, &ba);
color = view_opts->back ? view_opts->back : DEFAULT_BACK;
sscanf (color + (*color=='#'), "%2x%2x%2x%2x", &br, &bg, &bb, &ba);
fr = fg = fb = 0; fa = 255;
sscanf (view_opts->fore + (*view_opts->fore=='#'), "%2x%2x%2x%2x", &fr, &fg, &fb, &fa);
color = view_opts->fore ? view_opts->fore : DEFAULT_FORE;
sscanf (color + (*color=='#'), "%2x%2x%2x%2x", &fr, &fg, &fb, &fa);
cairo_content_t content;
if (!view_opts->annotate && ba == 255 && br == bg && bg == bb && fr == fg && fg == fb)
......
......@@ -31,6 +31,22 @@
/* main() body for utilities taking font and processing text.*/
static char *
locale_to_utf8 (char *s)
{
char *t;
GError *error = NULL;
t = g_locale_to_utf8 (s, -1, NULL, NULL, &error);
if (!t)
{
fail (true, "Failed converting text to UTF-8");
}
return t;
}
template <typename consumer_t, int default_font_size, int subpixel_bits>
struct main_font_text_t
{
......@@ -46,14 +62,14 @@ struct main_font_text_t
options.parse (&argc, &argv);
argc--, argv++;
if (argc && !font_opts.font_file) font_opts.font_file = argv[0], argc--, argv++;
if (argc && !input.text && !input.text_file) input.text = argv[0], argc--, argv++;
if (argc && !font_opts.font_file) font_opts.font_file = locale_to_utf8 (argv[0]), argc--, argv++;
if (argc && !input.text && !input.text_file) input.text = locale_to_utf8 (argv[0]), argc--, argv++;
if (argc)
fail (true, "Too many arguments on the command line");
if (!font_opts.font_file)
options.usage ();
if (!input.text && !input.text_file)
input.text_file = "-";
input.text_file = g_strdup ("-");
consumer.init (&font_opts);
......
......@@ -601,25 +601,26 @@ const char *
text_options_t::get_line (unsigned int *len)
{
if (text) {
if (text_len == (unsigned int) -1)
text_len = strlen (text);
if (!line) line = text;
if (line_len == (unsigned int) -1)
line_len = strlen (line);
if (!text_len) {
if (!line_len) {
*len = 0;
return NULL;
}
const char *ret = text;
const char *p = (const char *) memchr (text, '\n', text_len);
const char *ret = line;
const char *p = (const char *) memchr (line, '\n', line_len);
unsigned int ret_len;
if (!p) {
ret_len = text_len;
text += ret_len;
text_len = 0;
ret_len = line_len;
line += ret_len;
line_len = 0;
} else {
ret_len = p - ret;
text += ret_len + 1;
text_len -= ret_len + 1;
line += ret_len + 1;
line_len -= ret_len + 1;
}
*len = ret_len;
......
......@@ -150,19 +150,24 @@ struct view_options_t : option_group_t
{
view_options_t (option_parser_t *parser) {
annotate = false;
fore = DEFAULT_FORE;
back = DEFAULT_BACK;
fore = NULL;
back = NULL;
line_space = 0;
margin.t = margin.r = margin.b = margin.l = DEFAULT_MARGIN;
add_options (parser);
}
~view_options_t (void)
{
g_free (fore);
g_free (back);
}
void add_options (option_parser_t *parser);
hb_bool_t annotate;
const char *fore;
const char *back;
char *fore;
char *back;
double line_space;
struct margin_t {
double t, r, b, l;
......@@ -188,6 +193,9 @@ struct shape_options_t : option_group_t
}
~shape_options_t (void)
{
g_free (direction);
g_free (language);
g_free (script);
free (features);
g_strfreev (shapers);
}
......@@ -254,9 +262,9 @@ struct shape_options_t : option_group_t
}
/* Buffer properties */
const char *direction;
const char *language;
const char *script;
char *direction;
char *language;
char *script;
/* Buffer flags */
hb_bool_t bot;
......@@ -290,6 +298,8 @@ struct font_options_t : option_group_t
add_options (parser);
}
~font_options_t (void) {
g_free (font_file);
g_free (font_funcs);
hb_font_destroy (font);
}
......@@ -297,13 +307,13 @@ struct font_options_t : option_group_t
hb_font_t *get_font (void) const;
const char *font_file;
char *font_file;
int face_index;
int default_font_size;
unsigned int subpixel_bits;
mutable double font_size_x;
mutable double font_size_y;
const char *font_funcs;
char *font_funcs;
private:
mutable hb_font_t *font;
......@@ -321,11 +331,16 @@ struct text_options_t : option_group_t
fp = NULL;
gs = NULL;
text_len = (unsigned int) -1;
line = NULL;
line_len = (unsigned int) -1;
add_options (parser);
}
~text_options_t (void) {
g_free (text_before);
g_free (text_after);
g_free (text);
g_free (text_file);
if (gs)
g_string_free (gs, true);
if (fp)
......@@ -339,21 +354,21 @@ struct text_options_t : option_group_t
g_set_error (error,
G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
"Only one of text and text-file can be set");
};
const char *get_line (unsigned int *len);
const char *text_before;
const char *text_after;
char *text_before;
char *text_after;
const char *text;
const char *text_file;
char *text;
char *text_file;
private:
FILE *fp;
GString *gs;
unsigned int text_len;
char *line;
unsigned int line_len;
};
struct output_options_t : option_group_t
......@@ -370,6 +385,8 @@ struct output_options_t : option_group_t
add_options (parser);
}
~output_options_t (void) {
g_free (output_file);
g_free (output_format);
if (fp)
fclose (fp);
}
......@@ -393,8 +410,8 @@ struct output_options_t : option_group_t
FILE *get_file_handle (void);
const char *output_file;
const char *output_format;
char *output_file;
char *output_format;
const char **supported_formats;
bool explicit_output_format;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册