[Mesa-dev] [PATCH 10/11] util/string_buffer: fix warning in tests
Thomas Helland
thomashelland90 at gmail.com
Tue Jul 17 05:46:27 UTC 2018
This patch is
Reviewed-by: Thomas Helland <thomashelland90 at gmail.com>
Den tir. 17. jul. 2018, 01:40 skrev Caio Marcelo de Oliveira Filho <
caio.oliveira at intel.com>:
> And also specify the maximum size when writing to static buffers. The
> warning below refers to the case where "str5" could be larger than
> "str5 - str4", then the strcat would have overlapping dst and src.
>
> Compiler doesn't pick up the bound from the snprintf above, so we make
> clear the bounds of str5 by using strncat() instead of strcat().
>
> ../../src/util/tests/string_buffer/string_buffer_test.cpp: In member
> function ‘virtual void string_buffer_string_buffer_tests_Test::TestBody()’:
> ../../src/util/tests/string_buffer/string_buffer_test.cpp:106:10: warning:
> ‘char* strcat(char*, const char*)’ accessing 81 or more bytes at offsets 48
> and 128 may overlap 1 byte at offset 128 [-Wrestrict]
> strcat(str4, str5);
> ~~~~~~^~~~~~~~~~~~
> ---
> src/util/tests/string_buffer/string_buffer_test.cpp | 6 +++---
> 1 file changed, 3 insertions(+), 3 deletions(-)
>
> diff --git a/src/util/tests/string_buffer/string_buffer_test.cpp
> b/src/util/tests/string_buffer/string_buffer_test.cpp
> index 545f607fadd..afb6dfb2a19 100644
> --- a/src/util/tests/string_buffer/string_buffer_test.cpp
> +++ b/src/util/tests/string_buffer/string_buffer_test.cpp
> @@ -95,15 +95,15 @@ TEST_F(string_buffer, string_buffer_tests)
> EXPECT_TRUE(strlen(buf->buf) == 0);
>
> /* Test a string with some formatting */
> - sprintf(str4, "Testing formatting %d, %f", 100, 1.0);
> + snprintf(str4, sizeof(str4), "Testing formatting %d, %f", 100, 1.0);
> EXPECT_TRUE(_mesa_string_buffer_printf(buf, "Testing formatting %d,
> %f", 100, 1.0));
> EXPECT_TRUE(strcmp(buf->buf, str4) == 0);
>
> /* Compile a string with some other formatting */
> - sprintf(str5, "Testing formatting %d, %x", 100, 0xDEADBEAF);
> + snprintf(str5, sizeof(str5), "Testing formatting %d, %x", 100,
> 0xDEADBEAF);
>
> /* Concatenate str5 to str4 */
> - strcat(str4, str5);
> + strncat(str4, str5, sizeof(str5));
>
> /* Now use the formatted append function again */
> EXPECT_TRUE(_mesa_string_buffer_printf(buf, "Testing formatting %d,
> %x", 100, 0xDEADBEAF));
> --
> 2.18.0
>
> _______________________________________________
> mesa-dev mailing list
> mesa-dev at lists.freedesktop.org
> https://lists.freedesktop.org/mailman/listinfo/mesa-dev
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.freedesktop.org/archives/mesa-dev/attachments/20180717/e645bf8d/attachment.html>
More information about the mesa-dev
mailing list