Commit 984e7f30 authored by Dmitry Antipov's avatar Dmitry Antipov

Avoid calls to strlen in font processing functions.

* font.c (font_parse_name, font_parse_xlfd, font_parse_fcname)
(font_open_by_name): Changed to use length argument.  Adjust
users accordingly.
* font.h (font_open_by_name, font_parse_xlfd): Adjust prototypes.
* xfont.c (xfont_decode_coding_xlfd): Changed to return ptrdiff_t.
(xfont_list_pattern, xfont_match): Use length returned by
xfont_decode_coding_xlfd.
* xfns.c (x_default_font_parameter): Omit useless xstrdup.
parent 9d596af3
2012-07-11 Dmitry Antipov <dmantipov@yandex.ru>
Avoid calls to strlen in font processing functions.
* font.c (font_parse_name, font_parse_xlfd, font_parse_fcname)
(font_open_by_name): Changed to use length argument. Adjust
users accordingly.
* font.h (font_open_by_name, font_parse_xlfd): Adjust prototypes.
* xfont.c (xfont_decode_coding_xlfd): Changed to return ptrdiff_t.
(xfont_list_pattern, xfont_match): Use length returned by
xfont_decode_coding_xlfd.
* xfns.c (x_default_font_parameter): Omit useless xstrdup.
2012-07-11 Glenn Morris <rgm@gnu.org> 2012-07-11 Glenn Morris <rgm@gnu.org>
* s/darwin.h, s/freebsd.h, s/netbsd.h: * s/darwin.h, s/freebsd.h, s/netbsd.h:
......
...@@ -739,7 +739,7 @@ font_put_extra (Lisp_Object font, Lisp_Object prop, Lisp_Object val) ...@@ -739,7 +739,7 @@ font_put_extra (Lisp_Object font, Lisp_Object prop, Lisp_Object val)
static int parse_matrix (const char *); static int parse_matrix (const char *);
static int font_expand_wildcards (Lisp_Object *, int); static int font_expand_wildcards (Lisp_Object *, int);
static int font_parse_name (char *, Lisp_Object); static int font_parse_name (char *, ptrdiff_t, Lisp_Object);
/* An enumerator for each field of an XLFD font name. */ /* An enumerator for each field of an XLFD font name. */
enum xlfd_field_index enum xlfd_field_index
...@@ -1019,9 +1019,8 @@ font_expand_wildcards (Lisp_Object *field, int n) ...@@ -1019,9 +1019,8 @@ font_expand_wildcards (Lisp_Object *field, int n)
a fully specified XLFD. */ a fully specified XLFD. */
int int
font_parse_xlfd (char *name, Lisp_Object font) font_parse_xlfd (char *name, ptrdiff_t len, Lisp_Object font)
{ {
ptrdiff_t len = strlen (name);
int i, j, n; int i, j, n;
char *f[XLFD_LAST_INDEX + 1]; char *f[XLFD_LAST_INDEX + 1];
Lisp_Object val; Lisp_Object val;
...@@ -1336,12 +1335,11 @@ font_unparse_xlfd (Lisp_Object font, int pixel_size, char *name, int nbytes) ...@@ -1336,12 +1335,11 @@ font_unparse_xlfd (Lisp_Object font, int pixel_size, char *name, int nbytes)
This function tries to guess which format it is. */ This function tries to guess which format it is. */
static int static int
font_parse_fcname (char *name, Lisp_Object font) font_parse_fcname (char *name, ptrdiff_t len, Lisp_Object font)
{ {
char *p, *q; char *p, *q;
char *size_beg = NULL, *size_end = NULL; char *size_beg = NULL, *size_end = NULL;
char *props_beg = NULL, *family_end = NULL; char *props_beg = NULL, *family_end = NULL;
ptrdiff_t len = strlen (name);
if (len == 0) if (len == 0)
return -1; return -1;
...@@ -1694,11 +1692,11 @@ font_unparse_fcname (Lisp_Object font, int pixel_size, char *name, int nbytes) ...@@ -1694,11 +1692,11 @@ font_unparse_fcname (Lisp_Object font, int pixel_size, char *name, int nbytes)
0. Otherwise return -1. */ 0. Otherwise return -1. */
static int static int
font_parse_name (char *name, Lisp_Object font) font_parse_name (char *name, ptrdiff_t namelen, Lisp_Object font)
{ {
if (name[0] == '-' || strchr (name, '*') || strchr (name, '?')) if (name[0] == '-' || strchr (name, '*') || strchr (name, '?'))
return font_parse_xlfd (name, font); return font_parse_xlfd (name, namelen, font);
return font_parse_fcname (name, font); return font_parse_fcname (name, namelen, font);
} }
...@@ -2987,7 +2985,7 @@ font_spec_from_name (Lisp_Object font_name) ...@@ -2987,7 +2985,7 @@ font_spec_from_name (Lisp_Object font_name)
Lisp_Object spec = Ffont_spec (0, NULL); Lisp_Object spec = Ffont_spec (0, NULL);
CHECK_STRING (font_name); CHECK_STRING (font_name);
if (font_parse_name (SSDATA (font_name), spec) == -1) if (font_parse_name (SSDATA (font_name), SBYTES (font_name), spec) == -1)
return Qnil; return Qnil;
font_put_extra (spec, QCname, font_name); font_put_extra (spec, QCname, font_name);
font_put_extra (spec, QCuser_spec, font_name); font_put_extra (spec, QCuser_spec, font_name);
...@@ -3359,13 +3357,13 @@ font_open_by_spec (FRAME_PTR f, Lisp_Object spec) ...@@ -3359,13 +3357,13 @@ font_open_by_spec (FRAME_PTR f, Lisp_Object spec)
found, return Qnil. */ found, return Qnil. */
Lisp_Object Lisp_Object
font_open_by_name (FRAME_PTR f, const char *name) font_open_by_name (FRAME_PTR f, const char *name, ptrdiff_t len)
{ {
Lisp_Object args[2]; Lisp_Object args[2];
Lisp_Object spec, ret; Lisp_Object spec, ret;
args[0] = QCname; args[0] = QCname;
args[1] = make_unibyte_string (name, strlen (name)); args[1] = make_unibyte_string (name, len);
spec = Ffont_spec (2, args); spec = Ffont_spec (2, args);
ret = font_open_by_spec (f, spec); ret = font_open_by_spec (f, spec);
/* Do not lose name originally put in. */ /* Do not lose name originally put in. */
...@@ -3872,7 +3870,7 @@ usage: (font-spec ARGS...) */) ...@@ -3872,7 +3870,7 @@ usage: (font-spec ARGS...) */)
if (EQ (key, QCname)) if (EQ (key, QCname))
{ {
CHECK_STRING (val); CHECK_STRING (val);
font_parse_name (SSDATA (val), spec); font_parse_name (SSDATA (val), SBYTES (val), spec);
font_put_extra (spec, key, val); font_put_extra (spec, key, val);
} }
else else
...@@ -4887,7 +4885,7 @@ If the named font is not yet loaded, return nil. */) ...@@ -4887,7 +4885,7 @@ If the named font is not yet loaded, return nil. */)
if (fontset >= 0) if (fontset >= 0)
name = fontset_ascii (fontset); name = fontset_ascii (fontset);
font_object = font_open_by_name (f, SSDATA (name)); font_object = font_open_by_name (f, SSDATA (name), SBYTES (name));
} }
else if (FONT_OBJECT_P (name)) else if (FONT_OBJECT_P (name))
font_object = name; font_object = name;
......
...@@ -771,7 +771,7 @@ extern void font_prepare_for_face (FRAME_PTR f, struct face *face); ...@@ -771,7 +771,7 @@ extern void font_prepare_for_face (FRAME_PTR f, struct face *face);
extern void font_done_for_face (FRAME_PTR f, struct face *face); extern void font_done_for_face (FRAME_PTR f, struct face *face);
extern Lisp_Object font_open_by_spec (FRAME_PTR f, Lisp_Object spec); extern Lisp_Object font_open_by_spec (FRAME_PTR f, Lisp_Object spec);
extern Lisp_Object font_open_by_name (FRAME_PTR f, const char *name); extern Lisp_Object font_open_by_name (FRAME_PTR f, const char *name, ptrdiff_t len);
extern Lisp_Object font_intern_prop (const char *str, ptrdiff_t len, extern Lisp_Object font_intern_prop (const char *str, ptrdiff_t len,
int force_symbol); int force_symbol);
...@@ -781,7 +781,7 @@ extern void font_parse_family_registry (Lisp_Object family, ...@@ -781,7 +781,7 @@ extern void font_parse_family_registry (Lisp_Object family,
Lisp_Object registry, Lisp_Object registry,
Lisp_Object spec); Lisp_Object spec);
extern int font_parse_xlfd (char *name, Lisp_Object font); extern int font_parse_xlfd (char *name, ptrdiff_t len, Lisp_Object font);
extern int font_unparse_xlfd (Lisp_Object font, int pixel_size, extern int font_unparse_xlfd (Lisp_Object font, int pixel_size,
char *name, int bytes); char *name, int bytes);
extern int font_unparse_fcname (Lisp_Object font, int pixel_size, extern int font_unparse_fcname (Lisp_Object font, int pixel_size,
......
...@@ -1646,7 +1646,7 @@ FONT-SPEC is a vector, a cons, or a string. See the documentation of ...@@ -1646,7 +1646,7 @@ FONT-SPEC is a vector, a cons, or a string. See the documentation of
char xlfd[256]; char xlfd[256];
int len; int len;
if (font_parse_xlfd (SSDATA (name), font_spec) < 0) if (font_parse_xlfd (SSDATA (name), SBYTES (name), font_spec) < 0)
error ("Fontset name must be in XLFD format"); error ("Fontset name must be in XLFD format");
short_name = AREF (font_spec, FONT_REGISTRY_INDEX); short_name = AREF (font_spec, FONT_REGISTRY_INDEX);
if (strncmp (SSDATA (SYMBOL_NAME (short_name)), "fontset-", 8) if (strncmp (SSDATA (SYMBOL_NAME (short_name)), "fontset-", 8)
......
...@@ -3167,7 +3167,7 @@ x_set_font (struct frame *f, Lisp_Object arg, Lisp_Object oldval) ...@@ -3167,7 +3167,7 @@ x_set_font (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
fontset = fs_query_fontset (arg, 0); fontset = fs_query_fontset (arg, 0);
if (fontset < 0) if (fontset < 0)
{ {
font_object = font_open_by_name (f, SSDATA (arg)); font_object = font_open_by_name (f, SSDATA (arg), SBYTES (arg));
if (NILP (font_object)) if (NILP (font_object))
error ("Font `%s' is not defined", SSDATA (arg)); error ("Font `%s' is not defined", SSDATA (arg));
arg = AREF (font_object, FONT_NAME_INDEX); arg = AREF (font_object, FONT_NAME_INDEX);
...@@ -3176,7 +3176,7 @@ x_set_font (struct frame *f, Lisp_Object arg, Lisp_Object oldval) ...@@ -3176,7 +3176,7 @@ x_set_font (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
{ {
Lisp_Object ascii_font = fontset_ascii (fontset); Lisp_Object ascii_font = fontset_ascii (fontset);
font_object = font_open_by_name (f, SSDATA (ascii_font)); font_object = font_open_by_name (f, SSDATA (ascii_font), SBYTES (ascii_font));
if (NILP (font_object)) if (NILP (font_object))
error ("Font `%s' is not defined", SDATA (arg)); error ("Font `%s' is not defined", SDATA (arg));
arg = AREF (font_object, FONT_NAME_INDEX); arg = AREF (font_object, FONT_NAME_INDEX);
......
...@@ -4036,7 +4036,7 @@ x_default_font_parameter (struct frame *f, Lisp_Object parms) ...@@ -4036,7 +4036,7 @@ x_default_font_parameter (struct frame *f, Lisp_Object parms)
for (i = 0; names[i]; i++) for (i = 0; names[i]; i++)
{ {
font = font_open_by_name (f, names[i]); font = font_open_by_name (f, names[i], strlen (names[i]));
if (! NILP (font)) if (! NILP (font))
break; break;
} }
......
...@@ -2956,11 +2956,7 @@ x_default_font_parameter (struct frame *f, Lisp_Object parms) ...@@ -2956,11 +2956,7 @@ x_default_font_parameter (struct frame *f, Lisp_Object parms)
read yet. */ read yet. */
const char *system_font = xsettings_get_system_font (); const char *system_font = xsettings_get_system_font ();
if (system_font) if (system_font)
{ font = font_open_by_name (f, system_font, strlen (system_font));
char *name = xstrdup (system_font);
font = font_open_by_name (f, name);
xfree (name);
}
} }
if (NILP (font)) if (NILP (font))
...@@ -2990,7 +2986,7 @@ x_default_font_parameter (struct frame *f, Lisp_Object parms) ...@@ -2990,7 +2986,7 @@ x_default_font_parameter (struct frame *f, Lisp_Object parms)
for (i = 0; names[i]; i++) for (i = 0; names[i]; i++)
{ {
font = font_open_by_name (f, names[i]); font = font_open_by_name (f, names[i], strlen (names[i]));
if (! NILP (font)) if (! NILP (font))
break; break;
} }
......
...@@ -174,7 +174,7 @@ compare_font_names (const void *name1, const void *name2) ...@@ -174,7 +174,7 @@ compare_font_names (const void *name1, const void *name2)
XLFD is NULL terminated. The caller must assure that OUTPUT is at XLFD is NULL terminated. The caller must assure that OUTPUT is at
least twice (plus 1) as large as XLFD. */ least twice (plus 1) as large as XLFD. */
static int static ptrdiff_t
xfont_decode_coding_xlfd (char *xlfd, int len, char *output) xfont_decode_coding_xlfd (char *xlfd, int len, char *output)
{ {
char *p0 = xlfd, *p1 = output; char *p0 = xlfd, *p1 = output;
...@@ -397,13 +397,14 @@ xfont_list_pattern (Display *display, const char *pattern, ...@@ -397,13 +397,14 @@ xfont_list_pattern (Display *display, const char *pattern,
for (i = 0; i < num_fonts; i++) for (i = 0; i < num_fonts; i++)
{ {
ptrdiff_t len;
Lisp_Object entity; Lisp_Object entity;
if (i > 0 && xstrcasecmp (indices[i - 1], indices[i]) == 0) if (i > 0 && xstrcasecmp (indices[i - 1], indices[i]) == 0)
continue; continue;
entity = font_make_entity (); entity = font_make_entity ();
xfont_decode_coding_xlfd (indices[i], -1, buf); len = xfont_decode_coding_xlfd (indices[i], -1, buf);
if (font_parse_xlfd (buf, entity) < 0) if (font_parse_xlfd (buf, len, entity) < 0)
continue; continue;
ASET (entity, FONT_TYPE_INDEX, Qx); ASET (entity, FONT_TYPE_INDEX, Qx);
/* Avoid auto-scaled fonts. */ /* Avoid auto-scaled fonts. */
...@@ -604,10 +605,11 @@ xfont_match (Lisp_Object frame, Lisp_Object spec) ...@@ -604,10 +605,11 @@ xfont_match (Lisp_Object frame, Lisp_Object spec)
string. We must avoid such a name. */ string. We must avoid such a name. */
if (*s) if (*s)
{ {
ptrdiff_t len;
entity = font_make_entity (); entity = font_make_entity ();
ASET (entity, FONT_TYPE_INDEX, Qx); ASET (entity, FONT_TYPE_INDEX, Qx);
xfont_decode_coding_xlfd (s, -1, name); len = xfont_decode_coding_xlfd (s, -1, name);
if (font_parse_xlfd (name, entity) < 0) if (font_parse_xlfd (name, len, entity) < 0)
entity = Qnil; entity = Qnil;
} }
XFree (s); XFree (s);
...@@ -796,7 +798,7 @@ xfont_open (FRAME_PTR f, Lisp_Object entity, int pixel_size) ...@@ -796,7 +798,7 @@ xfont_open (FRAME_PTR f, Lisp_Object entity, int pixel_size)
ASET (font_object, FONT_TYPE_INDEX, Qx); ASET (font_object, FONT_TYPE_INDEX, Qx);
if (STRINGP (fullname)) if (STRINGP (fullname))
{ {
font_parse_xlfd (SSDATA (fullname), font_object); font_parse_xlfd (SSDATA (fullname), SBYTES (fullname), font_object);
ASET (font_object, FONT_NAME_INDEX, fullname); ASET (font_object, FONT_NAME_INDEX, fullname);
} }
else else
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment