From dd62bd8f2e1aa6822551c668d23b4d288390dcb7 Mon Sep 17 00:00:00 2001 From: Cristy Date: Mon, 21 Feb 2022 11:55:43 -0500 Subject: [PATCH 15/18] ImageMagick: Add support patch 1 to fix CVE-2023-34151 Upstream-Status: Backport [https://github.com/ImageMagick/ImageMagick/commit/eb0882667cddc4ea71b61a583a782c430220faf4] Comment: Refreshed hunk to match latest kirkstone Signed-off-by: Divyanshu Rathore --- MagickCore/image-private.h | 11 +++++++++++ coders/txt.c | 24 ++++++++++++------------ 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/MagickCore/image-private.h b/MagickCore/image-private.h index 40c8686d3..59b88fb6f 100644 --- a/MagickCore/image-private.h +++ b/MagickCore/image-private.h @@ -61,6 +61,17 @@ static inline ssize_t CastDoubleToLong(const double value) return((ssize_t) value); } +static inline QuantumAny CastDoubleToQuantumAny(const double x) +{ + if (IsNaN(x) != 0) + return(0); + if (x > ((double) ((QuantumAny) ~0))) + return((QuantumAny) ~0); + if (x < 0.0) + return(0.0); + return((QuantumAny) (x+0.5)); +} + static inline double DegreesToRadians(const double degrees) { return((double) (MagickPI*degrees/180.0)); diff --git a/coders/txt.c b/coders/txt.c index b9bd08ce5..f8312a4fd 100644 --- a/coders/txt.c +++ b/coders/txt.c @@ -564,18 +564,18 @@ static Image *ReadTXTImage(const ImageInfo *image_info,ExceptionInfo *exception) green+=(range+1)/2.0; blue+=(range+1)/2.0; } - pixel.red=(MagickRealType) ScaleAnyToQuantum((QuantumAny) - MagickMax(red+0.5,0.0),range); - pixel.green=(MagickRealType) ScaleAnyToQuantum((QuantumAny) - MagickMax(green+0.5,0.0),range); - pixel.blue=(MagickRealType) ScaleAnyToQuantum((QuantumAny) - MagickMax(blue+0.5,0.0),range); - pixel.black=(MagickRealType) ScaleAnyToQuantum((QuantumAny) - MagickMax(black+0.5,0.0),range); - pixel.alpha=(MagickRealType) ScaleAnyToQuantum((QuantumAny) - MagickMax(alpha+0.5,0.0),range); - q=GetAuthenticPixels(image,CastDoubleToLong(x_offset), - CastDoubleToLong(y_offset),1,1,exception); + pixel.red=(MagickRealType) ScaleAnyToQuantum(CastDoubleToQuantumAny( + red),range); + pixel.green=(MagickRealType) ScaleAnyToQuantum(CastDoubleToQuantumAny( + green),range); + pixel.blue=(MagickRealType) ScaleAnyToQuantum(CastDoubleToQuantumAny( + blue),range); + pixel.black=(MagickRealType) ScaleAnyToQuantum(CastDoubleToQuantumAny( + black),range); + pixel.alpha=(MagickRealType) ScaleAnyToQuantum(CastDoubleToQuantumAny( + alpha),range); + q=GetAuthenticPixels(image,CastDoubleToLong(x_offset),CastDoubleToLong( + y_offset),1,1,exception); if (q == (Quantum *) NULL) { status=MagickFalse; -- 2.34.1