Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

code: use calloc() to avoid malloc()+memset()

This also makes sure that there won't be an integer overflow on allocation.
  • Loading branch information...
commit 982e89d74f6ffa129024015762bfb385987a4cb1 1 parent 7c524b6
@Flameeyes authored
Showing with 4 additions and 7 deletions.
  1. +4 −7 imageprocess.c
View
11 imageprocess.c
@@ -1008,7 +1008,6 @@ int blurfilter(int blurfilterScanSize[DIRECTIONS_COUNT], int blurfilterScanStep[
int maxLeft;
int maxTop;
int blocksPerRow;
- int sizeCounts;
int* prevCounts; // Number of dark pixels in previous row
int* curCounts; // Number of dark pixels in current row
int* nextCounts; // Number of dark pixels in next row
@@ -1027,15 +1026,13 @@ int blurfilter(int blurfilterScanSize[DIRECTIONS_COUNT], int blurfilterScanStep[
maxTop = image->height - blurfilterScanSize[VERTICAL];
blocksPerRow = image->width / blurfilterScanSize[HORIZONTAL];
- sizeCounts = (blocksPerRow + 2) * sizeof(int); // One extra block left and right
- prevCounts = (int*) malloc(sizeCounts);
- curCounts = (int*) malloc(sizeCounts);
- nextCounts = (int*) malloc(sizeCounts);
+ // allocate one extra block left and right
+ prevCounts = calloc(blocksPerRow + 2, sizeof(int));
+ curCounts = calloc(blocksPerRow + 2, sizeof(int));
+ nextCounts = calloc(blocksPerRow + 2, sizeof(int));
total = blurfilterScanSize[HORIZONTAL] * blurfilterScanSize[VERTICAL];
- //Initialize prevCounts, curCounts and nextCounts
- memset(prevCounts, total, sizeCounts); // as if the pixels in the blocks of the -1th row were all white
block = 1;
for (left = 0; left <= maxLeft; left += blurfilterScanSize[HORIZONTAL]) {
curCounts[block] = countPixelsRect(left, top, right, bottom, 0, whiteMin, false, image);
Please sign in to comment.
Something went wrong with that request. Please try again.