From 507575fd820f5e24dba7064563e2a799d1687b60 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Fri, 30 Oct 2026 16:34:30 -0600 Subject: [PATCH] [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 --- src/hb-ot-layout.cc | 12 ++++++------ src/hb-ot-metrics.cc | 5 ++++- src/hb-ot-metrics.h | 2 +- test/api/test-base-minmax.c | 8 ++++++++ test/api/test-ot-metrics.c | 22 ++++++++++++++++++++++ 5 files changed, 41 insertions(+), 8 deletions(-) diff --git a/src/hb-ot-layout.cc b/src/hb-ot-layout.cc index 22e41e24a..45a47842b 100644 --- a/src/hb-ot-layout.cc +++ b/src/hb-ot-layout.cc @@ -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; } diff --git a/src/hb-ot-metrics.cc b/src/hb-ot-metrics.cc index d2bfa0294..69196e3c8 100644 --- a/src/hb-ot-metrics.cc +++ b/src/hb-ot-metrics.cc @@ -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 && diff --git a/src/hb-ot-metrics.h b/src/hb-ot-metrics.h index 30de50008..9c938b67d 100644 --- a/src/hb-ot-metrics.h +++ b/src/hb-ot-metrics.h @@ -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); diff --git a/test/api/test-base-minmax.c b/test/api/test-base-minmax.c index c5632ca73..d9651a50f 100644 --- a/test/api/test-base-minmax.c +++ b/test/api/test-base-minmax.c @@ -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); } diff --git a/test/api/test-ot-metrics.c b/test/api/test-ot-metrics.c index 675f588de..0df8ead94 100644 --- a/test/api/test-ot-metrics.c +++ b/test/api/test-ot-metrics.c @@ -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 (); }