+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:
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
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;
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;
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);
}
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);
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. */
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
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;
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);
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,
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)
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);
{
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);
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;
}
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))
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;
}
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;
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. */
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);
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