[metrics][layout] Accept NULL out-pointer where the docs allow it

hb_ot_metrics_get_position_with_fallback() and
hb_ot_layout_get_font_extents() / ...2() document their result pointer
as optional ((out) (optional) / (out) (nullable)), and one path in each
honours that, but the others dereference it: the metrics fallback reads
*position for the strikeout/underline zero check and writes it in every
synthesized arm, and the layout miss path hands the pointer unchecked to
hb_font_get_extents_for_direction(), which memsets it.

Substitute a stack local at function entry with the existing
`if (!p) p = &local;` idiom (cf. hb-paint.cc), so every path below is
unchanged, and drop the now-redundant `if (extents)` guard.  ...2() is
covered through its callee.  The prototype comment becomes
`/* OUT.  May be NULL. */` to match hb_ot_metrics_get_position().  No
API/ABI change.

Regression cases are added to test/api/test-ot-metrics.c and
test/api/test-base-minmax.c.

Reported by Anthropic's OSS Scanner as ordinary bug ANT-2026-Y1S3T3RY.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
Behdad EsfahbodandClaude Opus 4.8 committed 2026-10-30 16:35:27 -06:00
1 parent 2f8fb68973
commit 507575fd82
5 files changed
+41 -8

No files matched your search

+6 -6
View File
@@ -2197,16 +2197,16 @@ hb_ot_layout_get_font_extents (hb_font_t *font,
hb_tag_t language_tag,
hb_font_extents_t *extents)
{
hb_font_extents_t unused;
if (!extents) extents = &unused;
hb_position_t min = 0, max = 0;
if (font->face->table.BASE->get_min_max (font, direction, script_tag, language_tag, HB_TAG_NONE,
&min, &max))
{
if (extents)
{
extents->ascender = max;
extents->descender = min;
extents->line_gap = 0;
}
extents->ascender = max;
extents->descender = min;
extents->line_gap = 0;
return true;
}
+4 -1
View File
@@ -266,12 +266,15 @@ hb_ot_metrics_get_position (hb_font_t *font,
void
hb_ot_metrics_get_position_with_fallback (hb_font_t *font,
hb_ot_metrics_tag_t metrics_tag,
hb_position_t *position /* OUT */)
hb_position_t *position /* OUT. May be NULL. */)
{
hb_font_extents_t font_extents;
hb_codepoint_t glyph;
hb_glyph_extents_t extents;
hb_position_t unused;
if (!position) position = &unused;
if (hb_ot_metrics_get_position (font, metrics_tag, position))
{
if ((metrics_tag != HB_OT_METRICS_TAG_STRIKEOUT_SIZE &&
+1 -1
View File
@@ -113,7 +113,7 @@ hb_ot_metrics_get_position (hb_font_t *font,
HB_EXTERN void
hb_ot_metrics_get_position_with_fallback (hb_font_t *font,
hb_ot_metrics_tag_t metrics_tag,
hb_position_t *position /* OUT */);
hb_position_t *position /* OUT. May be NULL. */);
HB_EXTERN float
hb_ot_metrics_get_variation (hb_font_t *font, hb_ot_metrics_tag_t metrics_tag);
+8
View File
@@ -51,6 +51,14 @@ test_ot_layout_font_extents (void)
&extents));
g_assert_cmpint (extents.ascender, ==, 3000);
/* NULL extents is allowed on both the hit and the miss path. */
g_assert_true (hb_ot_layout_get_font_extents2 (font, HB_DIRECTION_LTR,
HB_SCRIPT_LATIN, HB_LANGUAGE_INVALID,
NULL));
g_assert_true (!hb_ot_layout_get_font_extents2 (font, HB_DIRECTION_LTR,
HB_SCRIPT_ARABIC, HB_LANGUAGE_INVALID,
NULL));
hb_font_destroy (font);
hb_face_destroy (face);
}
+22
View File
@@ -94,6 +94,27 @@ test_ot_metrics_get_with_fallback (void)
hb_face_destroy (face);
}
static void
test_ot_metrics_get_position_with_fallback_null (void)
{
hb_face_t *face = hb_test_open_font_file ("fonts/cpal-v0.ttf");
hb_font_t *font = hb_font_create (face);
hb_position_t value;
/* position may be NULL whether the metric is found, found but zero, or synthesized. */
g_assert_true (hb_ot_metrics_get_position (font, HB_OT_METRICS_TAG_HORIZONTAL_ASCENDER, NULL));
hb_ot_metrics_get_position_with_fallback (font, HB_OT_METRICS_TAG_HORIZONTAL_ASCENDER, NULL);
hb_ot_metrics_get_position_with_fallback (font, HB_OT_METRICS_TAG_UNDERLINE_SIZE, NULL);
hb_ot_metrics_get_position_with_fallback (font, HB_OT_METRICS_TAG_VERTICAL_ASCENDER, NULL);
hb_ot_metrics_get_position_with_fallback (font, HB_OT_METRICS_TAG_X_HEIGHT, NULL);
hb_ot_metrics_get_position_with_fallback (font, HB_OT_METRICS_TAG_HORIZONTAL_ASCENDER, &value);
g_assert_cmpint (value, ==, 1000);
hb_font_destroy (font);
hb_face_destroy (face);
}
int
main (int argc, char **argv)
{
@@ -101,5 +122,6 @@ main (int argc, char **argv)
hb_test_add (test_ot_metrics_get_no_var);
hb_test_add (test_ot_metrics_get_var);
hb_test_add (test_ot_metrics_get_with_fallback);
hb_test_add (test_ot_metrics_get_position_with_fallback_null);
return hb_test_run ();
}