Skip to content

Commit

Permalink
implement suggestions from a static analyzer
Browse files Browse the repository at this point in the history
  • Loading branch information
Cristy committed Nov 19, 2022
1 parent 926ad10 commit d4be270
Show file tree
Hide file tree
Showing 16 changed files with 85 additions and 63 deletions.
9 changes: 6 additions & 3 deletions Magick++/lib/Options.cpp
Expand Up @@ -596,9 +596,12 @@ void Magick::Options::strokeDashArray(const double *strokeDashArray_)
if (!_drawInfo->dash_pattern)
throwExceptionExplicit(MagickCore::ResourceLimitError,
"Unable to allocate dash-pattern memory");
// Copy elements
memcpy(_drawInfo->dash_pattern,strokeDashArray_,(x+1)*sizeof(double));
_drawInfo->dash_pattern[x]=0.0;
else
{
// Copy elements
memcpy(_drawInfo->dash_pattern,strokeDashArray_,(x+1)*sizeof(double));
_drawInfo->dash_pattern[x]=0.0;
}
}
}

Expand Down
11 changes: 7 additions & 4 deletions MagickCore/annotate.c
Expand Up @@ -599,6 +599,8 @@ static inline char *ReplaceSpaceWithNewline(char **caption,char *space)
size_t
octets;

if ((caption == (char **) NULL) || (space == (char *) NULL))
return((char *) NULL);
octets=(size_t) GetUTFOctets(space);
if (octets == 1)
*space='\n';
Expand Down Expand Up @@ -628,14 +630,14 @@ MagickExport ssize_t FormatMagickCaption(Image *image,DrawInfo *draw_info,
const MagickBooleanType split,TypeMetric *metrics,char **caption,
ExceptionInfo *exception)
{
MagickBooleanType
status;

char
*p,
*q,
*s;

MagickBooleanType
status;

size_t
width;

Expand Down Expand Up @@ -1590,7 +1592,8 @@ static MagickBooleanType RenderFreetype(Image *image,const DrawInfo *draw_info,
Extract face index, e.g. @msgothic[1].
*/
ImageInfo *image_info = AcquireImageInfo();
(void) strcpy(image_info->filename,draw_info->font+1);
(void) CopyMagickString(image_info->filename,draw_info->font+1,
MagickPathExtent);
(void) SetImageInfo(image_info,0,exception);
face_index=(FT_Long) image_info->scene;
args.pathname=ConstantString(image_info->filename);
Expand Down
2 changes: 1 addition & 1 deletion MagickCore/constitute.c
Expand Up @@ -1163,7 +1163,7 @@ MagickExport Image *ReadInlineImage(const ImageInfo *image_info,
*/
if (LocaleNCompare(++p,"x-",2) == 0)
p+=2;
(void) strcpy(read_info->filename,"data.");
(void) CopyMagickString(read_info->filename,"data.",MagickPathExtent);
q=read_info->filename+5;
for (i=0; (*p != ';') && (*p != '\0') && (i < (MagickPathExtent-6)); i++)
*q++=(*p++);
Expand Down
4 changes: 3 additions & 1 deletion MagickCore/histogram.c
Expand Up @@ -50,6 +50,7 @@
#include "MagickCore/image.h"
#include "MagickCore/linked-list.h"
#include "MagickCore/list.h"
#include "MagickCore/locale_.h"
#include "MagickCore/memory_.h"
#include "MagickCore/monitor-private.h"
#include "MagickCore/pixel-accessor.h"
Expand Down Expand Up @@ -1101,7 +1102,8 @@ MagickExport size_t GetNumberColors(const Image *image,FILE *file,
(void) ConcatenateMagickString(tuple,")",MagickPathExtent);
(void) QueryColorname(image,&pixel,SVGCompliance,color,exception);
GetColorTuple(&pixel,MagickTrue,hex);
(void) sprintf(count,"%10.20g:",(double) ((MagickOffsetType) p->count));
(void) FormatLocaleString(count,MagickPathExtent,"%10.20g:",(double)
((MagickOffsetType) p->count));
(void) FormatLocaleFile(file," %s %s %s %s\n",count,tuple,hex,color);
if (image->progress_monitor != (MagickProgressMonitor) NULL)
{
Expand Down
13 changes: 8 additions & 5 deletions MagickCore/opencl.c
Expand Up @@ -59,6 +59,7 @@
#include "MagickCore/image.h"
#include "MagickCore/image-private.h"
#include "MagickCore/layer.h"
#include "MagickCore/locale_.h"
#include "MagickCore/mime-private.h"
#include "MagickCore/memory_.h"
#include "MagickCore/memory-private.h"
Expand Down Expand Up @@ -1597,12 +1598,13 @@ MagickPrivate void DumpOpenCLProfileData()
profile;

profile=device->profile_records[j];
strcpy(indent," ");
(void) CopyMagickString(indent," ",
sizeof(indent));
CopyMagickString(indent,profile->kernel_name,MagickMin(strlen(
profile->kernel_name),strlen(indent)));
sprintf(buf,"%s %7d %7d %7d %7d",indent,(int) (profile->total/
profile->count),(int) profile->count,(int) profile->min,
(int) profile->max);
(void) FormatLocaleString(buf,sozeof(buf),"%s %7d %7d %7d %7d",indent,
(int) (profile->total/profile->count),(int) profile->count,
(int) profile->min,(int) profile->max);
OpenCLLog(buf);
j++;
}
Expand Down Expand Up @@ -2176,7 +2178,8 @@ static MagickBooleanType HasOpenCLDevices(MagickCLEnv clEnv,
strlen(accelerateKernels)+strlen(accelerateKernels2)+1);
if (accelerateKernelsBuffer == (char*) NULL)
return(MagickFalse);
sprintf(accelerateKernelsBuffer,"%s%s",accelerateKernels,accelerateKernels2);
(void) FormatLocaleString(accelerateKernelsBuffer,strlen(accelerateKernels)+
strlen(accelerateKernels2)+1,"%s%s",accelerateKernels,accelerateKernels2);
signature^=StringSignature(accelerateKernelsBuffer);

status=MagickTrue;
Expand Down
7 changes: 4 additions & 3 deletions MagickCore/quantum-import.c
Expand Up @@ -260,9 +260,10 @@ static inline const unsigned char *PushQuantumPixel(QuantumInfo *quantum_info,
quantum_bits=quantum_info->state.bits;
i-=(ssize_t) quantum_bits;
quantum_info->state.bits-=quantum_bits;
*quantum=(unsigned int) ((*quantum << quantum_bits) |
((quantum_info->state.pixel >> quantum_info->state.bits) &~ ((~0UL) <<
quantum_bits)));
if (quantum_bits < 64)
*quantum=(unsigned int) (((MagickSizeType) *quantum << quantum_bits) |
((quantum_info->state.pixel >> quantum_info->state.bits) &~
((~0UL) << quantum_bits)));
}
return(pixels);
}
Expand Down
3 changes: 2 additions & 1 deletion MagickCore/segment.c
Expand Up @@ -1150,7 +1150,8 @@ MagickExport MagickBooleanType GetImageDynamicThreshold(const Image *image,
}
object=head;
background=head;
if (count > 1)
if ((count > 1) && (head != (Cluster *) NULL) &&
(head->next != (Cluster *) NULL))
{
object=head->next;
for (cluster=object; cluster->next != (Cluster *) NULL; )
Expand Down
2 changes: 1 addition & 1 deletion MagickWand/display.c
Expand Up @@ -1374,7 +1374,7 @@ WandExport MagickBooleanType DisplayImageCommand(ImageInfo *image_info,
resource_info.map_type=(char *) NULL;
if (*option == '+')
break;
(void) strcpy(argv[i]+1,"san");
(void) CopyMagickString(argv[i]+1,"...",strlen(argv[i]+1)+1);
i++;
if (i == (ssize_t) argc)
ThrowDisplayException(OptionError,"MissingArgument",option);
Expand Down
10 changes: 6 additions & 4 deletions coders/meta.c
Expand Up @@ -381,11 +381,13 @@ static ssize_t parse8BIM(Image *ifile, Image *ofile)
recnum = (unsigned int) StringToUnsignedLong(newstr);
break;
case 2:
name=(char *) AcquireQuantumMemory(strlen(newstr)+MagickPathExtent,
sizeof(*name));
if (name)
(void) strcpy(name,newstr);
{
size_t extent = strlen(newstr)+MagickPathExtent;
name=(char *) AcquireQuantumMemory(extent,sizeof(*name));
if (name != (char *) NULL)
(void) CopyMagickString(name,newstr,extent);
break;
}
}
s++;
}
Expand Down
2 changes: 1 addition & 1 deletion coders/palm.c
Expand Up @@ -771,7 +771,7 @@ static MagickBooleanType WritePALMImage(const ImageInfo *image_info,
if (IssRGBCompatibleColorspace(image->colorspace) == MagickFalse)
(void) TransformImageColorspace(image,sRGBColorspace,exception);
count=GetNumberColors(image,NULL,exception);
for (bits_per_pixel=1; (one << bits_per_pixel) < count; bits_per_pixel*=2) ;
for (bits_per_pixel=1; (bits_per_pixel < 64) && (one << bits_per_pixel) < count; bits_per_pixel*=2) ;
if (bits_per_pixel > 16)
bits_per_pixel=16;
else
Expand Down
4 changes: 2 additions & 2 deletions coders/png.c
Expand Up @@ -880,7 +880,7 @@ LosslessReduceDepthOK(Image *image,ExceptionInfo *exception)
QuantumToCharToQuantumEqQuantum(image->background_color.blue) ?
MagickTrue : MagickFalse;

if (ok_to_reduce != MagickFalse && image->storage_class == PseudoClass)
if (ok_to_reduce != MagickFalse && image->storage_class == PseudoClass && image->colormap != NULL)
{
int indx;

Expand Down Expand Up @@ -10345,7 +10345,7 @@ static MagickBooleanType WriteOnePNGImage(MngInfo *mng_info,
png_error(ping,"image has 0 colors");
}

while ((int) (one << ping_bit_depth) < (ssize_t) image_colors)
while ((ping_bit_depth < 64) && (int) (one << ping_bit_depth) < (ssize_t) image_colors)
ping_bit_depth <<= 1;
}

Expand Down
2 changes: 1 addition & 1 deletion coders/psd.c
Expand Up @@ -3981,7 +3981,7 @@ static MagickBooleanType WritePSDImage(const ImageInfo *image_info,
size_offset;

size_t
size;
size = 0;

size_offset=TellBlob(image);
(void) SetPSDSize(&psd_info,image,0);
Expand Down
28 changes: 16 additions & 12 deletions coders/sixel.c
Expand Up @@ -57,6 +57,7 @@
#include "MagickCore/image.h"
#include "MagickCore/image-private.h"
#include "MagickCore/list.h"
#include "MagickCore/locale_.h"
#include "MagickCore/magick.h"
#include "MagickCore/memory_.h"
#include "MagickCore/monitor.h"
Expand Down Expand Up @@ -673,10 +674,11 @@ static int sixel_put_flash(sixel_output_t *const context)
if (context->save_count > 3)
{
/* DECGRI Graphics Repeat Introducer ! Pn Ch */
nwrite=sprintf((char *) context->buffer+context->pos,"!%d%c",
context->save_count,context->save_pixel);
nwrite=FormatLocaleString((char *) context->buffer+context->pos,
sizeof(context->buffer),"!%d%c",context->save_count,
context->save_pixel);
if (nwrite <= 0)
return (-1);
return(-1);
sixel_advance(context,nwrite);
}
else
Expand Down Expand Up @@ -741,8 +743,8 @@ static int sixel_put_node(sixel_output_t *const context,int x,sixel_node_t *np,
/* designate palette index */
if (context->active_palette != np->color)
{
nwrite=sprintf((char *) context->buffer+context->pos,"#%d",
np->color);
nwrite=FormatLocaleString((char *) context->buffer+context->pos,
sizeof(context->buffer),"#%d",np->color);
sixel_advance(context,nwrite);
context->active_palette=np->color;
}
Expand Down Expand Up @@ -799,14 +801,16 @@ static MagickBooleanType sixel_encode_impl(sixel_pixel_t *pixels,size_t width,
return (MagickFalse);
(void) memset(map,0,len*sizeof(sixel_pixel_t));
if (context->has_8bit_control)
nwrite=sprintf((char *)context->buffer,"\x90" "0;0;0" "q");
nwrite=FormatLocaleString((char *) context->buffer,sizeof(context->buffer),
"\x90" "0;0;0" "q");
else
nwrite=sprintf((char *)context->buffer,"\x1bP" "0;0;0" "q");
nwrite=FormatLocaleString((char *) context->buffer,sizeof(context->buffer),
"\x1bP" "0;0;0" "q");
if (nwrite <= 0)
return(MagickFalse);
sixel_advance(context,nwrite);
nwrite=sprintf((char *)context->buffer+context->pos,"\"1;1;%d;%d",
(int) width,(int) height);
nwrite=FormatLocaleString((char *) context->buffer+context->pos,
sizeof(context->buffer),"\"1;1;%d;%d",(int) width,(int) height);
if (nwrite <= 0)
{
RelinquishNodesAndMap;
Expand All @@ -817,9 +821,9 @@ static MagickBooleanType sixel_encode_impl(sixel_pixel_t *pixels,size_t width,
for (n = 0; n < (ssize_t) ncolors; n++)
{
/* DECGCI Graphics Color Introducer # Pc ; Pu; Px; Py; Pz */
nwrite=sprintf((char *)context->buffer+context->pos,
"#%d;2;%d;%d;%d",n,(palette[n*3+0]*100+127)/255,
(palette[n*3+1]*100+127)/255,(palette[n*3+2]*100+127)/255);
nwrite=FormatLocaleString((char *) context->buffer+context->pos,
sizeof(context->buffer),"#%d;2;%d;%d;%d",n,(palette[n*3+0]*100+127)/
255,(palette[n*3+1]*100+127)/255,(palette[n*3+2]*100+127)/255);
if (nwrite <= 0)
{
RelinquishNodesAndMap;
Expand Down
13 changes: 7 additions & 6 deletions coders/svg.c
Expand Up @@ -3650,13 +3650,14 @@ static Image *RenderMSVGImage(const ImageInfo *image_info,Image *image,
if (svg_info->comment != (char *) NULL)
(void) SetImageProperty(image,"svg:comment",svg_info->comment,
exception);
for (next=GetFirstImageInList(image); next != (Image *) NULL; )
{
(void) CopyMagickString(next->filename,image->filename,
MagickPathExtent);
(void) CopyMagickString(next->magick,"SVG",MagickPathExtent);
next=GetNextImageInList(next);
}
}
for (next=GetFirstImageInList(image); next != (Image *) NULL; )
{
(void) CopyMagickString(next->filename,image->filename,MagickPathExtent);
(void) CopyMagickString(next->magick,"SVG",MagickPathExtent);
next=GetNextImageInList(next);
}
svg_info=DestroySVGInfo(svg_info);
(void) RelinquishUniqueFileResource(filename);
return(GetFirstImageInList(image));
Expand Down
34 changes: 18 additions & 16 deletions coders/tiff.c
Expand Up @@ -62,6 +62,7 @@
#include "MagickCore/image.h"
#include "MagickCore/image-private.h"
#include "MagickCore/list.h"
#include "MagickCore/locale_.h"
#include "MagickCore/log.h"
#include "MagickCore/magick.h"
#include "MagickCore/memory_.h"
Expand Down Expand Up @@ -807,8 +808,8 @@ static void TIFFSetImageProperties(TIFF *tiff,Image *image,
if (p == (char *) NULL)
continue;
*p='\0';
(void) sprintf(property,"%s%.1024s",tag,buffer);
(void) sprintf(value,"%s",p+1);
(void) FormatLocaleString(property,MagickPathExtent,"%s%s",tag,buffer);
(void) FormatLocaleString(value,MagickPathExtent,"%s",p+1);
(void) StripMagickString(value);
(void) SetImageProperty(image,property,value,exception);
}
Expand Down Expand Up @@ -2928,9 +2929,6 @@ static MagickBooleanType GetTIFFInfo(const ImageInfo *image_info,
static tmsize_t TIFFWritePixels(TIFF *tiff,TIFFInfo *tiff_info,ssize_t row,
tsample_t sample,Image *image)
{
ssize_t
i;

tmsize_t
status;

Expand All @@ -2940,6 +2938,7 @@ static tmsize_t TIFFWritePixels(TIFF *tiff,TIFFInfo *tiff_info,ssize_t row,

ssize_t
bytes_per_pixel,
i = 0,
j,
k,
l;
Expand All @@ -2948,18 +2947,21 @@ static tmsize_t TIFFWritePixels(TIFF *tiff,TIFFInfo *tiff_info,ssize_t row,
*p,
*q;

if (TIFFIsTiled(tiff) == 0)
if ((TIFFIsTiled(tiff) == 0) || (tiff_info->tile_geometry.height == 0))
return(TIFFWriteScanline(tiff,tiff_info->scanline,(uint32) row,sample));
/*
Fill scanlines to tile height.
*/
i=(ssize_t) (row % tiff_info->tile_geometry.height)*TIFFScanlineSize(tiff);
(void) memcpy(tiff_info->scanlines+i,(char *) tiff_info->scanline,
(size_t) TIFFScanlineSize(tiff));
if (((size_t) (row % tiff_info->tile_geometry.height) !=
(tiff_info->tile_geometry.height-1)) &&
(row != (ssize_t) (image->rows-1)))
return(0);
if (tiff_info->scanline != NULL)
{
/*
Fill scanlines to tile height.
*/
i=(ssize_t) (row % tiff_info->tile_geometry.height)*
TIFFScanlineSize(tiff);
(void) memcpy(tiff_info->scanlines+i,(char *) tiff_info->scanline,
(size_t) TIFFScanlineSize(tiff));
if (((size_t) (row % tiff_info->tile_geometry.height) != (tiff_info->tile_geometry.height-1)) &&
(row != (ssize_t) (image->rows-1)))
return(0);
}
/*
Write tile to TIFF image.
*/
Expand Down
4 changes: 2 additions & 2 deletions tests/wandtest.c
Expand Up @@ -5225,9 +5225,9 @@ int main(int argc,char **argv)
p=getenv("SRCDIR");
if (p != (char *) NULL)
{
(void) strcpy(path,p);
(void) CopyMagickString(path,p,MagickPathExtent);
if (path[strlen(path)-1] != '/')
(void) strcat(path,"/");
(void) ConcatenateMagickString(path,"/",MagickPathExtent);
}
(void) strcat(path,"sequence.miff");
status=MagickReadImage(magick_wand,path);
Expand Down

0 comments on commit d4be270

Please sign in to comment.