[v2,6/6] ipa: simple: agc: Prevent division by zero in AGC
diff mbox series

Message ID 20250911135144.56586-7-mzamazal@redhat.com
State New
Headers show
Series
  • Fix stats related problems in software ISP
Related show

Commit Message

Milan Zamazal Sept. 11, 2025, 1:51 p.m. UTC
If the histogram size is non-zero but lower than the number of bins,
yHistValsPerBin is zero and then the AGC processing crashes on division
by it.  Let's check yHistValsPerBin for being zero and stop AGC
processing in such a case.  The condition also covers the cases where
histogramSize or yHistValsPerBinMod are zero.

Signed-off-by: Milan Zamazal <mzamazal@redhat.com>
---
 src/ipa/simple/algorithms/agc.cpp | 6 ++++++
 1 file changed, 6 insertions(+)

Comments

Barnabás Pőcze Sept. 11, 2025, 3:50 p.m. UTC | #1
2025. 09. 11. 15:51 keltezéssel, Milan Zamazal írta:
> If the histogram size is non-zero but lower than the number of bins,
> yHistValsPerBin is zero and then the AGC processing crashes on division
> by it.  Let's check yHistValsPerBin for being zero and stop AGC
> processing in such a case.  The condition also covers the cases where
> histogramSize or yHistValsPerBinMod are zero.
> 
> Signed-off-by: Milan Zamazal <mzamazal@redhat.com>
> ---

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


>   src/ipa/simple/algorithms/agc.cpp | 6 ++++++
>   1 file changed, 6 insertions(+)
> 
> diff --git a/src/ipa/simple/algorithms/agc.cpp b/src/ipa/simple/algorithms/agc.cpp
> index c46bb0ebe..e47949ec4 100644
> --- a/src/ipa/simple/algorithms/agc.cpp
> +++ b/src/ipa/simple/algorithms/agc.cpp
> @@ -124,6 +124,12 @@ void Agc::process(IPAContext &context,
>   	unsigned int denom = 0;
>   	unsigned int num = 0;
>   
> +	if (yHistValsPerBin == 0) {
> +		LOG(IPASoftExposure, Debug)
> +			<< "Not adjusting exposure due to insufficient histogram data";
> +		return;
> +	}
> +
>   	for (unsigned int i = 0; i < histogramSize; i++) {
>   		unsigned int idx = (i - (i / yHistValsPerBinMod)) / yHistValsPerBin;
>   		exposureBins[idx] += histogram[blackLevelHistIdx + i];

Patch
diff mbox series

diff --git a/src/ipa/simple/algorithms/agc.cpp b/src/ipa/simple/algorithms/agc.cpp
index c46bb0ebe..e47949ec4 100644
--- a/src/ipa/simple/algorithms/agc.cpp
+++ b/src/ipa/simple/algorithms/agc.cpp
@@ -124,6 +124,12 @@  void Agc::process(IPAContext &context,
 	unsigned int denom = 0;
 	unsigned int num = 0;
 
+	if (yHistValsPerBin == 0) {
+		LOG(IPASoftExposure, Debug)
+			<< "Not adjusting exposure due to insufficient histogram data";
+		return;
+	}
+
 	for (unsigned int i = 0; i < histogramSize; i++) {
 		unsigned int idx = (i - (i / yHistValsPerBinMod)) / yHistValsPerBin;
 		exposureBins[idx] += histogram[blackLevelHistIdx + i];