Warn when constant name is used for unexpected object

This fixes the sort of weird things like `fc-match :size=rgb` done without any errors.
This might be annoyed but the error messages should helps to fix an application bug or
suggest more useful constant names to fontconfig.

Fixes https://gitlab.freedesktop.org/fontconfig/fontconfig/issues/137
This commit is contained in:
Akira TAGOH 2018-11-30 07:03:54 +00:00
parent 71c9c7892a
commit 65c7427c01
2 changed files with 27 additions and 6 deletions

View File

@ -1021,6 +1021,9 @@ enum {
#define FC_MAX_BASE_OBJECT (FC_ONE_AFTER_MAX_BASE_OBJECT - 1) #define FC_MAX_BASE_OBJECT (FC_ONE_AFTER_MAX_BASE_OBJECT - 1)
}; };
FcPrivate FcBool
FcNameConstantWithObjectCheck (const FcChar8 *string, const char *object, int *result);
FcPrivate FcBool FcPrivate FcBool
FcNameBool (const FcChar8 *v, FcBool *result); FcNameBool (const FcChar8 *v, FcBool *result);

View File

@ -241,6 +241,24 @@ FcNameConstant (const FcChar8 *string, int *result)
return FcFalse; return FcFalse;
} }
FcBool
FcNameConstantWithObjectCheck (const FcChar8 *string, const char *object, int *result)
{
const FcConstant *c;
if ((c = FcNameGetConstant(string)))
{
if (strcmp (c->object, object) != 0)
{
fprintf (stderr, "Fontconfig error: Unexpected constant name `%s' used for object `%s': should be `%s'\n", string, object, c->object);
return FcFalse;
}
*result = c->value;
return FcTrue;
}
return FcFalse;
}
FcBool FcBool
FcNameBool (const FcChar8 *v, FcBool *result) FcNameBool (const FcChar8 *v, FcBool *result)
{ {
@ -287,7 +305,7 @@ FcNameBool (const FcChar8 *v, FcBool *result)
} }
static FcValue static FcValue
FcNameConvert (FcType type, FcChar8 *string) FcNameConvert (FcType type, const char *object, FcChar8 *string)
{ {
FcValue v; FcValue v;
FcMatrix m; FcMatrix m;
@ -297,7 +315,7 @@ FcNameConvert (FcType type, FcChar8 *string)
v.type = type; v.type = type;
switch ((int) v.type) { switch ((int) v.type) {
case FcTypeInteger: case FcTypeInteger:
if (!FcNameConstant (string, &v.u.i)) if (!FcNameConstantWithObjectCheck (string, object, &v.u.i))
v.u.i = atoi ((char *) string); v.u.i = atoi ((char *) string);
break; break;
case FcTypeString: case FcTypeString:
@ -338,8 +356,8 @@ FcNameConvert (FcType type, FcChar8 *string)
ec = malloc (len + 1); ec = malloc (len + 1);
if (sc && ec && sscanf ((char *) string, "[%s %[^]]]", sc, ec) == 2) if (sc && ec && sscanf ((char *) string, "[%s %[^]]]", sc, ec) == 2)
{ {
if (FcNameConstant ((const FcChar8 *) sc, &si) && if (FcNameConstantWithObjectCheck ((const FcChar8 *) sc, object, &si) &&
FcNameConstant ((const FcChar8 *) ec, &ei)) FcNameConstantWithObjectCheck ((const FcChar8 *) ec, object, &ei))
v.u.r = FcRangeCreateDouble (si, ei); v.u.r = FcRangeCreateDouble (si, ei);
else else
goto bail1; goto bail1;
@ -348,7 +366,7 @@ FcNameConvert (FcType type, FcChar8 *string)
{ {
bail1: bail1:
v.type = FcTypeDouble; v.type = FcTypeDouble;
if (FcNameConstant (string, &si)) if (FcNameConstantWithObjectCheck (string, object, &si))
{ {
v.u.d = (double) si; v.u.d = (double) si;
} else { } else {
@ -461,7 +479,7 @@ FcNameParse (const FcChar8 *name)
name = FcNameFindNext (name, ":,", save, &delim); name = FcNameFindNext (name, ":,", save, &delim);
if (t) if (t)
{ {
v = FcNameConvert (t->type, save); v = FcNameConvert (t->type, t->object, save);
if (!FcPatternAdd (pat, t->object, v, FcTrue)) if (!FcPatternAdd (pat, t->object, v, FcTrue))
{ {
FcValueDestroy (v); FcValueDestroy (v);