apps: common: options: Use character literal instead of string
diff mbox series

Message ID 20260512141840.166903-1-laurent.pinchart@ideasonboard.com
State New
Headers show
Series
  • apps: common: options: Use character literal instead of string
Related show

Commit Message

Laurent Pinchart May 12, 2026, 2:18 p.m. UTC
gcc 12 generates a `-Wrestrict` warning at -O3 optimization levels in
C++20:

g++-12 -Isrc/apps/common/libapps.a.p -Isrc/apps/common -I../src/apps/common -Iinclude -I../include -Iinclude/libcamera -fdiagnostics-color=always -D_FILE_OFFSET_BITS=64 -Wall -Winvalid-pch -Wextra -Werror -std=c++20 -O3 -Wnon-virtual-dtor -Wno-redundant-move -Wmissing-declarations -Wshadow -include /builds/pinchartl/libcamera/build/config.h -fPIC -DHAVE_TIFF -MD -MQ src/apps/common/libapps.a.p/options.cpp.o -MF src/apps/common/libapps.a.p/options.cpp.o.d -o src/apps/common/libapps.a.p/options.cpp.o -c ../src/apps/common/options.cpp
In file included from /usr/include/c++/12/string:40,
                 from /usr/include/c++/12/bits/locale_classes.h:40,
                 from /usr/include/c++/12/bits/ios_base.h:41,
                 from /usr/include/c++/12/iomanip:40,
                 from ../src/apps/common/options.cpp:10:
In static member function ‘static constexpr std::char_traits<char>::char_type* std::char_traits<char>::copy(char_type*, const char_type*, std::size_t)’,
    inlined from ‘static constexpr void std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::_S_copy(_CharT*, const _CharT*, size_type) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]’ at /usr/include/c++/12/bits/basic_string.h:423:21,
    inlined from ‘constexpr std::__cxx11::basic_string<_CharT, _Traits, _Allocator>& std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::_M_replace(size_type, size_type, const _CharT*, size_type) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]’ at /usr/include/c++/12/bits/basic_string.tcc:532:22,
    inlined from ‘constexpr std::__cxx11::basic_string<_CharT, _Traits, _Alloc>& std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::replace(size_type, size_type, const _CharT*, size_type) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]’ at /usr/include/c++/12/bits/basic_string.h:2171:19,
    inlined from ‘constexpr std::__cxx11::basic_string<_CharT, _Traits, _Alloc>& std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::insert(size_type, const _CharT*) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]’ at /usr/include/c++/12/bits/basic_string.h:1928:22,
    inlined from ‘constexpr std::__cxx11::basic_string<_CharT, _Traits, _Allocator> std::operator+(const _CharT*, __cxx11::basic_string<_CharT, _Traits, _Allocator>&&) [with _CharT = char; _Traits = char_traits<char>; _Alloc = allocator<char>]’ at /usr/include/c++/12/bits/basic_string.h:3541:36,
    inlined from ‘std::string Option::optionName() const’ at ../src/apps/common/options.cpp:149:34:
/usr/include/c++/12/bits/char_traits.h:431:56: error: ‘void* __builtin_memcpy(void*, const void*, long unsigned int)’ accessing 9223372036854775810 or more bytes at offsets [18, 9223372036854775807] and 17 may overlap up to 9223372036854775813 bytes at offset -3 [-Werror=restrict]
  431 |         return static_cast<char_type*>(__builtin_memcpy(__s1, __s2, __n));
      |                                        ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~

Work around the issue by using a character literal instead of a string
literal for single character strings.

Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
---
 src/apps/common/options.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)


base-commit: b0dbd5100b1b1665dd2bc839d06263d55a89a6bd

Comments

Barnabás Pőcze May 12, 2026, 2:30 p.m. UTC | #1
2026. 05. 12. 16:18 keltezéssel, Laurent Pinchart írta:
> gcc 12 generates a `-Wrestrict` warning at -O3 optimization levels in
> C++20:
> 
> g++-12 -Isrc/apps/common/libapps.a.p -Isrc/apps/common -I../src/apps/common -Iinclude -I../include -Iinclude/libcamera -fdiagnostics-color=always -D_FILE_OFFSET_BITS=64 -Wall -Winvalid-pch -Wextra -Werror -std=c++20 -O3 -Wnon-virtual-dtor -Wno-redundant-move -Wmissing-declarations -Wshadow -include /builds/pinchartl/libcamera/build/config.h -fPIC -DHAVE_TIFF -MD -MQ src/apps/common/libapps.a.p/options.cpp.o -MF src/apps/common/libapps.a.p/options.cpp.o.d -o src/apps/common/libapps.a.p/options.cpp.o -c ../src/apps/common/options.cpp
> In file included from /usr/include/c++/12/string:40,
>                   from /usr/include/c++/12/bits/locale_classes.h:40,
>                   from /usr/include/c++/12/bits/ios_base.h:41,
>                   from /usr/include/c++/12/iomanip:40,
>                   from ../src/apps/common/options.cpp:10:
> In static member function ‘static constexpr std::char_traits<char>::char_type* std::char_traits<char>::copy(char_type*, const char_type*, std::size_t)’,
>      inlined from ‘static constexpr void std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::_S_copy(_CharT*, const _CharT*, size_type) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]’ at /usr/include/c++/12/bits/basic_string.h:423:21,
>      inlined from ‘constexpr std::__cxx11::basic_string<_CharT, _Traits, _Allocator>& std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::_M_replace(size_type, size_type, const _CharT*, size_type) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]’ at /usr/include/c++/12/bits/basic_string.tcc:532:22,
>      inlined from ‘constexpr std::__cxx11::basic_string<_CharT, _Traits, _Alloc>& std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::replace(size_type, size_type, const _CharT*, size_type) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]’ at /usr/include/c++/12/bits/basic_string.h:2171:19,
>      inlined from ‘constexpr std::__cxx11::basic_string<_CharT, _Traits, _Alloc>& std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::insert(size_type, const _CharT*) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]’ at /usr/include/c++/12/bits/basic_string.h:1928:22,
>      inlined from ‘constexpr std::__cxx11::basic_string<_CharT, _Traits, _Allocator> std::operator+(const _CharT*, __cxx11::basic_string<_CharT, _Traits, _Allocator>&&) [with _CharT = char; _Traits = char_traits<char>; _Alloc = allocator<char>]’ at /usr/include/c++/12/bits/basic_string.h:3541:36,
>      inlined from ‘std::string Option::optionName() const’ at ../src/apps/common/options.cpp:149:34:
> /usr/include/c++/12/bits/char_traits.h:431:56: error: ‘void* __builtin_memcpy(void*, const void*, long unsigned int)’ accessing 9223372036854775810 or more bytes at offsets [18, 9223372036854775807] and 17 may overlap up to 9223372036854775813 bytes at offset -3 [-Werror=restrict]
>    431 |         return static_cast<char_type*>(__builtin_memcpy(__s1, __s2, __n));
>        |                                        ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~
> 
> Work around the issue by using a character literal instead of a string
> literal for single character strings.
> 
> Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
> ---

Reviewed-by: Barnabás Pőcze <barnabas.pocze@ideasonboard.com>


>   src/apps/common/options.cpp | 2 +-
>   1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/src/apps/common/options.cpp b/src/apps/common/options.cpp
> index b95f95afadf3..be0c161165ea 100644
> --- a/src/apps/common/options.cpp
> +++ b/src/apps/common/options.cpp
> @@ -146,7 +146,7 @@ std::string Option::optionName() const
>   	if (name)
>   		return "--" + std::string(name);
>   	else
> -		return "-" + std::string(1, opt);
> +		return '-' + std::string(1, opt);
>   }
>   
>   /* -----------------------------------------------------------------------------
> 
> base-commit: b0dbd5100b1b1665dd2bc839d06263d55a89a6bd

Patch
diff mbox series

diff --git a/src/apps/common/options.cpp b/src/apps/common/options.cpp
index b95f95afadf3..be0c161165ea 100644
--- a/src/apps/common/options.cpp
+++ b/src/apps/common/options.cpp
@@ -146,7 +146,7 @@  std::string Option::optionName() const
 	if (name)
 		return "--" + std::string(name);
 	else
-		return "-" + std::string(1, opt);
+		return '-' + std::string(1, opt);
 }
 
 /* -----------------------------------------------------------------------------