Rationalise g_htoi() / xrdp_wm_htoi()

xrdp contains two functions which do similar things:-
- g_htoi() converts a hex string to an integer, ignoring unrecognised
  characters
- xrdp_wm_htoi() converts a hex string to an integer, ignoring leading
  whitespace, but terminating on unrecognised characters

An analysis of the uses of g_htoi() shows that the only place where
unrecognised characters might be encountered is parsing lines from
xrdp_keyboard.ini, where all values have an '0x' prefix (i.e. the 'x'
is unrecognised)

An analysis of xrdp_wm_htoi() shows that the functionality to ignore
leading whitespace is not used.

Both functions are replaced with a re-written g_htoi() which is const-
correct and provided with test cases. This function behaves in
the same way as the atoi() library function, in that it terminates on
an unexpected character.

The use of g_htoi() in parsing lines from xrdp_keyboard.ini is replaced
with a call to g_atoix() which handles the '0x' prefix correctly.
This commit is contained in:
matt335672
2025-03-31 15:36:51 +01:00
parent 267443e90d
commit bc0e169451
7 changed files with 82 additions and 135 deletions
+27
View File
@@ -1141,6 +1141,28 @@ END_TEST
/******************************************************************************/
START_TEST(test_htoi__all)
{
// Invalid 1st character
ck_assert_int_eq(g_htoi(""), 0);
ck_assert_int_eq(g_htoi("z"), 0);
// Test border conditions (assumes ASCII) */
ck_assert_int_eq(g_htoi("99/"), 0x99); /* '/' = one-before '0' */
ck_assert_int_eq(g_htoi("99:"), 0x99); /* '/' = one-after '9' */
ck_assert_int_eq(g_htoi("99@"), 0x99); /* '#' = one-before 'A' */
ck_assert_int_eq(g_htoi("99G"), 0x99); /* 'G' = one-after 'F' */
ck_assert_int_eq(g_htoi("99`"), 0x99); /* '`' = one-before 'a' */
ck_assert_int_eq(g_htoi("99g"), 0x99); /* 'g' = one-after 'f' */
// Test all valid characters and shifting is OK */
ck_assert_int_eq(g_htoi("01234567"), 0x01234567);
ck_assert_int_eq(g_htoi("8ABCDEFa"), 0x8ABCDEFa);
ck_assert_int_eq(g_htoi("bcdef"), 0xbcdef);
}
END_TEST
/******************************************************************************/
Suite *
make_suite_test_string(void)
{
@@ -1152,6 +1174,7 @@ make_suite_test_string(void)
TCase *tc_char2bm;
TCase *tc_strtrim;
TCase *tc_sigs;
TCase *tc_htoi;
s = suite_create("String");
@@ -1230,5 +1253,9 @@ make_suite_test_string(void)
tcase_add_test(tc_sigs, test_sigs__common);
tcase_add_test(tc_sigs, test_sigs__bigint);
tc_htoi = tcase_create("g_htoi");
suite_add_tcase(s, tc_htoi);
tcase_add_test(tc_htoi, test_htoi__all);
return s;
}