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>
* 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)
static int parse_matrix (const char *);
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. */
enum xlfd_field_index
......@@ -1019,9 +1019,8 @@ font_expand_wildcards (Lisp_Object *field, int n)
a fully specified XLFD. */
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;
char *f[XLFD_LAST_INDEX + 1];
Lisp_Object val;
......@@ -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. */
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 *size_beg = NULL, *size_end = NULL;
char *props_beg = NULL, *family_end = NULL;
ptrdiff_t len = strlen (name);
if (len == 0)
return -1;
......@@ -1694,11 +1692,11 @@ font_unparse_fcname (Lisp_Object font, int pixel_size, char *name, int nbytes)
0. Otherwise return -1. */
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, '?'))
return font_parse_xlfd (name, font);
return font_parse_fcname (name, font);
return font_parse_xlfd (name, namelen, font);
return font_parse_fcname (name, namelen, font);
}
......@@ -2987,7 +2985,7 @@ font_spec_from_name (Lisp_Object font_name)
Lisp_Object spec = Ffont_spec (0, NULL);
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;
font_put_extra (spec, QCname, font_name);
font_put_extra (spec, QCuser_spec, font_name);
......@@ -3359,13 +3357,13 @@ font_open_by_spec (FRAME_PTR f, Lisp_Object spec)
found, return Qnil. */
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 spec, ret;
args[0] = QCname;
args[1] = make_unibyte_string (name, strlen (name));
args[1] = make_unibyte_string (name, len);
spec = Ffont_spec (2, args);
ret = font_open_by_spec (f, spec);
/* Do not lose name originally put in. */
......@@ -3872,7 +3870,7 @@ usage: (font-spec ARGS...) */)
if (EQ (key, QCname))
{
CHECK_STRING (val);
font_parse_name (SSDATA (val), spec);
font_parse_name (SSDATA (val), SBYTES (val), spec);
font_put_extra (spec, key, val);
}
else
......@@ -4887,7 +4885,7 @@ If the named font is not yet loaded, return nil. */)
if (fontset >= 0)
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))
font_object = name;
......
......@@ -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 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,
int force_symbol);
......@@ -781,7 +781,7 @@ extern void font_parse_family_registry (Lisp_Object family,
Lisp_Object registry,
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,
char *name, int bytes);
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
char xlfd[256];
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");
short_name = AREF (font_spec, FONT_REGISTRY_INDEX);
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)
fontset = fs_query_fontset (arg, 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))
error ("Font `%s' is not defined", SSDATA (arg));
arg = AREF (font_object, FONT_NAME_INDEX);
......@@ -3176,7 +3176,7 @@ x_set_font (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
{
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))
error ("Font `%s' is not defined", SDATA (arg));
arg = AREF (font_object, FONT_NAME_INDEX);
......
......@@ -4036,7 +4036,7 @@ x_default_font_parameter (struct frame *f, Lisp_Object parms)
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))
break;
}
......
......@@ -2956,11 +2956,7 @@ x_default_font_parameter (struct frame *f, Lisp_Object parms)
read yet. */
const char *system_font = xsettings_get_system_font ();
if (system_font)
{
char *name = xstrdup (system_font);
font = font_open_by_name (f, name);
xfree (name);
}
font = font_open_by_name (f, system_font, strlen (system_font));
}
if (NILP (font))
......@@ -2990,7 +2986,7 @@ x_default_font_parameter (struct frame *f, Lisp_Object parms)
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))
break;
}
......
......@@ -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
least twice (plus 1) as large as XLFD. */
static int
static ptrdiff_t
xfont_decode_coding_xlfd (char *xlfd, int len, char *output)
{
char *p0 = xlfd, *p1 = output;
......@@ -397,13 +397,14 @@ xfont_list_pattern (Display *display, const char *pattern,
for (i = 0; i < num_fonts; i++)
{
ptrdiff_t len;
Lisp_Object entity;
if (i > 0 && xstrcasecmp (indices[i - 1], indices[i]) == 0)
continue;
entity = font_make_entity ();
xfont_decode_coding_xlfd (indices[i], -1, buf);
if (font_parse_xlfd (buf, entity) < 0)
len = xfont_decode_coding_xlfd (indices[i], -1, buf);
if (font_parse_xlfd (buf, len, entity) < 0)
continue;
ASET (entity, FONT_TYPE_INDEX, Qx);
/* Avoid auto-scaled fonts. */
......@@ -604,10 +605,11 @@ xfont_match (Lisp_Object frame, Lisp_Object spec)
string. We must avoid such a name. */
if (*s)
{
ptrdiff_t len;
entity = font_make_entity ();
ASET (entity, FONT_TYPE_INDEX, Qx);
xfont_decode_coding_xlfd (s, -1, name);
if (font_parse_xlfd (name, entity) < 0)
len = xfont_decode_coding_xlfd (s, -1, name);
if (font_parse_xlfd (name, len, entity) < 0)
entity = Qnil;
}
XFree (s);
......@@ -796,7 +798,7 @@ xfont_open (FRAME_PTR f, Lisp_Object entity, int pixel_size)
ASET (font_object, FONT_TYPE_INDEX, Qx);
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);
}
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