[v2,2/2] libcamera: base: Fix log level parsing when multiple categories are listed
diff mbox series

Message ID 20250612135943.522819-3-stefan.klug@ideasonboard.com
State Accepted
Headers show
Series
  • Fix log level parsing when multiple categories are listed
Related show

Commit Message

Stefan Klug June 12, 2025, 1:56 p.m. UTC
For a list of log levels like LIBCAMERA_LOG_LEVELS="CatA:0,CatB:1" only
the severity of the last entry is correctly parsed.

Due to the change of level to a string_view in 24c2caa1c1b3 ("libcamera:
base: log: Use `std::string_view` to avoid some copies") the level is no
longer necessarily null terminated as it is a view on the original data.

Replace the check for a terminating null by a check for the end position
to fix the issue.

Fixes: 24c2caa1c1b3 ("libcamera: base: log: Use `std::string_view` to avoid some copies")
Signed-off-by: Stefan Klug <stefan.klug@ideasonboard.com>
Reviewed-by: Barnabás Pőcze <barnabas.pocze@ideasonboard.com>
---
 src/libcamera/base/log.cpp | 5 +++--
 test/log/meson.build       | 2 +-
 2 files changed, 4 insertions(+), 3 deletions(-)

Comments

Laurent Pinchart June 12, 2025, 2:10 p.m. UTC | #1
On Thu, Jun 12, 2025 at 03:56:45PM +0200, Stefan Klug wrote:
> For a list of log levels like LIBCAMERA_LOG_LEVELS="CatA:0,CatB:1" only
> the severity of the last entry is correctly parsed.
> 
> Due to the change of level to a string_view in 24c2caa1c1b3 ("libcamera:
> base: log: Use `std::string_view` to avoid some copies") the level is no
> longer necessarily null terminated as it is a view on the original data.
> 
> Replace the check for a terminating null by a check for the end position
> to fix the issue.
> 
> Fixes: 24c2caa1c1b3 ("libcamera: base: log: Use `std::string_view` to avoid some copies")
> Signed-off-by: Stefan Klug <stefan.klug@ideasonboard.com>
> Reviewed-by: Barnabás Pőcze <barnabas.pocze@ideasonboard.com>

Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>

> ---
>  src/libcamera/base/log.cpp | 5 +++--
>  test/log/meson.build       | 2 +-
>  2 files changed, 4 insertions(+), 3 deletions(-)
> 
> diff --git a/src/libcamera/base/log.cpp b/src/libcamera/base/log.cpp
> index 8bf3e1daa9c6..6a8e2a3eb844 100644
> --- a/src/libcamera/base/log.cpp
> +++ b/src/libcamera/base/log.cpp
> @@ -690,8 +690,9 @@ LogSeverity Logger::parseLogLevel(std::string_view level)
>  	unsigned int severity = LogInvalid;
>  
>  	if (std::isdigit(level[0])) {
> -		auto [end, ec] = std::from_chars(level.data(), level.data() + level.size(), severity);
> -		if (ec != std::errc() || *end != '\0' || severity > LogFatal)
> +		const char *levelEnd = level.data() + level.size();
> +		auto [end, ec] = std::from_chars(level.data(), levelEnd, severity);
> +		if (ec != std::errc() || end != levelEnd || severity > LogFatal)
>  			severity = LogInvalid;
>  	} else {
>  		for (unsigned int i = 0; i < std::size(names); ++i) {
> diff --git a/test/log/meson.build b/test/log/meson.build
> index d91f62b9ea5b..f413c3898b2d 100644
> --- a/test/log/meson.build
> +++ b/test/log/meson.build
> @@ -1,7 +1,7 @@
>  # SPDX-License-Identifier: CC0-1.0
>  
>  log_test = [
> -    {'name': 'log_api', 'sources': ['log_api.cpp'], 'should_fail':true},
> +    {'name': 'log_api', 'sources': ['log_api.cpp']},
>      {'name': 'log_process', 'sources': ['log_process.cpp']},
>  ]
>

Patch
diff mbox series

diff --git a/src/libcamera/base/log.cpp b/src/libcamera/base/log.cpp
index 8bf3e1daa9c6..6a8e2a3eb844 100644
--- a/src/libcamera/base/log.cpp
+++ b/src/libcamera/base/log.cpp
@@ -690,8 +690,9 @@  LogSeverity Logger::parseLogLevel(std::string_view level)
 	unsigned int severity = LogInvalid;
 
 	if (std::isdigit(level[0])) {
-		auto [end, ec] = std::from_chars(level.data(), level.data() + level.size(), severity);
-		if (ec != std::errc() || *end != '\0' || severity > LogFatal)
+		const char *levelEnd = level.data() + level.size();
+		auto [end, ec] = std::from_chars(level.data(), levelEnd, severity);
+		if (ec != std::errc() || end != levelEnd || severity > LogFatal)
 			severity = LogInvalid;
 	} else {
 		for (unsigned int i = 0; i < std::size(names); ++i) {
diff --git a/test/log/meson.build b/test/log/meson.build
index d91f62b9ea5b..f413c3898b2d 100644
--- a/test/log/meson.build
+++ b/test/log/meson.build
@@ -1,7 +1,7 @@ 
 # SPDX-License-Identifier: CC0-1.0
 
 log_test = [
-    {'name': 'log_api', 'sources': ['log_api.cpp'], 'should_fail':true},
+    {'name': 'log_api', 'sources': ['log_api.cpp']},
     {'name': 'log_process', 'sources': ['log_process.cpp']},
 ]