mirror of
https://github.com/storytold/harfbuzz.git
synced 2026-11-19 14:01:10 +01:00
hb_shape_justify() copies the buffer text so it can reshape while solving for the axis value, but only the solver path at the end frees the copy. The three "nothing to solve" success returns (default advance already in range, maximum expansion still short of the target, maximum shrink still above it) and the three shaping failure returns leak it, so ordinary line justification leaks one text copy per call. Free it with a scope guard right after the allocation, as the rest of the tree does, and drop the explicit free at the end. Reproduced with leaks --atExit on a plain experimental-API build: four calls on Estedad-VF.ttf give four ROOT LEAKs at hb-shape.cc:319, none with the fix. Add test-shape-justify.c covering all five paths under the experimental-API test list so the asan-ubsan job runs it under LeakSanitizer. The full ASan+UBSan Meson suite passes (289 ok, 2 skipped). Assisted-by: Claude
115 lines
3.8 KiB
C
115 lines
3.8 KiB
C
/*
|
|
* Copyright © 2026 nussaiba shaikh
|
|
*
|
|
* This is part of HarfBuzz, a text shaping library.
|
|
*
|
|
* Permission is hereby granted, without written agreement and without
|
|
* license or royalty fees, to use, copy, modify, and distribute this
|
|
* software and its documentation for any purpose, provided that the
|
|
* above copyright notice and the following two paragraphs appear in
|
|
* all copies of this software.
|
|
*
|
|
* IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
|
|
* DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
|
|
* ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
|
|
* IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
|
|
* DAMAGE.
|
|
*
|
|
* THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
|
|
* BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
|
* FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
|
|
* ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
|
|
* PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
|
|
*/
|
|
|
|
#include "hb-test.h"
|
|
|
|
/* Unit tests for hb_shape_justify() */
|
|
|
|
static hb_buffer_t *
|
|
create_buffer (void)
|
|
{
|
|
hb_buffer_t *buffer = hb_buffer_create ();
|
|
hb_buffer_add_utf8 (buffer, "\xd8\xa7\xd8\xa8\xd8\xac", -1, 0, -1); /* ابج */
|
|
hb_buffer_guess_segment_properties (buffer);
|
|
return buffer;
|
|
}
|
|
|
|
/* Estedad-VF has a wdth axis (100..200, default 100). At the default
|
|
* instance the text advances 2565 units, at wdth=200 it advances 3565. */
|
|
static void
|
|
test_shape_justify_early_returns (void)
|
|
{
|
|
hb_face_t *face = hb_test_open_font_file ("fonts/Estedad-VF.ttf");
|
|
hb_font_t *font = hb_font_create (face);
|
|
const char *bad_shapers[] = {"nonexistent", NULL};
|
|
hb_buffer_t *buffer;
|
|
float advance;
|
|
hb_tag_t var_tag;
|
|
float var_value;
|
|
|
|
/* Default advance, computed internally, already inside the target range. */
|
|
buffer = create_buffer ();
|
|
advance = 0.f;
|
|
g_assert_true (hb_shape_justify (font, buffer, NULL, 0, NULL,
|
|
2500.f, 2600.f,
|
|
&advance, &var_tag, &var_value));
|
|
g_assert_cmpuint (var_tag, ==, HB_TAG_NONE);
|
|
g_assert_cmpfloat (advance, ==, 2565.f);
|
|
hb_buffer_destroy (buffer);
|
|
|
|
/* Target above the maximum expansion: the axis maximum is returned. */
|
|
buffer = create_buffer ();
|
|
advance = 0.f;
|
|
g_assert_true (hb_shape_justify (font, buffer, NULL, 0, NULL,
|
|
3600.f, 4000.f,
|
|
&advance, &var_tag, &var_value));
|
|
g_assert_cmpuint (var_tag, ==, HB_TAG ('w','d','t','h'));
|
|
g_assert_cmpfloat (var_value, ==, 200.f);
|
|
g_assert_cmpfloat (advance, ==, 3565.f);
|
|
hb_buffer_destroy (buffer);
|
|
|
|
/* Target below the maximum shrink: the axis minimum is returned. */
|
|
buffer = create_buffer ();
|
|
advance = 0.f;
|
|
g_assert_true (hb_shape_justify (font, buffer, NULL, 0, NULL,
|
|
2000.f, 2100.f,
|
|
&advance, &var_tag, &var_value));
|
|
g_assert_cmpuint (var_tag, ==, HB_TAG ('w','d','t','h'));
|
|
g_assert_cmpfloat (var_value, ==, 100.f);
|
|
g_assert_cmpfloat (advance, ==, 2565.f);
|
|
hb_buffer_destroy (buffer);
|
|
|
|
/* Shaping failure. */
|
|
buffer = create_buffer ();
|
|
advance = 0.f;
|
|
g_assert_false (hb_shape_justify (font, buffer, NULL, 0, bad_shapers,
|
|
3000.f, 3100.f,
|
|
&advance, &var_tag, &var_value));
|
|
hb_buffer_destroy (buffer);
|
|
|
|
/* Solver path. */
|
|
buffer = create_buffer ();
|
|
advance = 0.f;
|
|
g_assert_true (hb_shape_justify (font, buffer, NULL, 0, NULL,
|
|
3000.f, 3100.f,
|
|
&advance, &var_tag, &var_value));
|
|
g_assert_cmpuint (var_tag, ==, HB_TAG ('w','d','t','h'));
|
|
g_assert_cmpfloat (advance, >=, 3000.f);
|
|
g_assert_cmpfloat (advance, <=, 3100.f);
|
|
hb_buffer_destroy (buffer);
|
|
|
|
hb_font_destroy (font);
|
|
hb_face_destroy (face);
|
|
}
|
|
|
|
int
|
|
main (int argc, char **argv)
|
|
{
|
|
hb_test_init (&argc, &argv);
|
|
|
|
hb_test_add (test_shape_justify_early_returns);
|
|
|
|
return hb_test_run();
|
|
}
|