From c31e3cbb08cd96cf5362b4a8eab4a486e0eac017 Mon Sep 17 00:00:00 2001 From: tinzhu Date: Wed, 3 Jan 2024 13:50:15 +0800 Subject: [PATCH] Fixed string length calculation in thermostat sample. --- .../demo_guix_thermostat.c | 23 ++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/samples/demo_guix_thermostat/demo_guix_thermostat.c b/samples/demo_guix_thermostat/demo_guix_thermostat.c index 298e851f..9d9073af 100644 --- a/samples/demo_guix_thermostat/demo_guix_thermostat.c +++ b/samples/demo_guix_thermostat/demo_guix_thermostat.c @@ -19,6 +19,7 @@ INT i_temperature = 79; VOID start_guix(VOID); UINT win32_graphics_driver_setup_565rgb(GX_DISPLAY *display); +UINT string_length_get(GX_CONST GX_CHAR* input_string, UINT max_string_length); int main(int argc, char ** argv) { @@ -122,7 +123,7 @@ GX_STRING string; gx_utility_ltoa(i_temperature, str_value, 10); string.gx_string_ptr = str_value; - string.gx_string_length = sizeof(str_value) - 1; + string.gx_string_length = string_length_get(str_value, sizeof(str_value) - 1); gx_prompt_text_set_ext(prompt, &string); } @@ -163,4 +164,24 @@ INT angle; return 0; } +/******************************************************************************************/ +UINT string_length_get(GX_CONST GX_CHAR* input_string, UINT max_string_length) +{ + UINT length = 0; + + if (input_string) + { + /* Traverse the string. */ + for (length = 0; input_string[length]; length++) + { + /* Check if the string length is bigger than the max string length. */ + if (length >= max_string_length) + { + break; + } + } + } + + return length; +}