Skip to content

Commit

Permalink
Merge pull request #1722 from stweil/cov
Browse files Browse the repository at this point in the history
Fix several issues reported by Coverity Scan
  • Loading branch information
zdenop committed Jul 1, 2018
2 parents 13ec8a7 + abbd78a commit 1a181a3
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 56 deletions.
9 changes: 4 additions & 5 deletions src/ccmain/applybox.cpp
Expand Up @@ -313,11 +313,10 @@ void Tesseract::MaximallyChopWord(const GenericVector<TBOX>& boxes,
/// miss metric gets the blob.
static double BoxMissMetric(const TBOX& box1, const TBOX& box2) {
const int overlap_area = box1.intersection(box2).area();
double miss_metric = box1.area()- overlap_area;
miss_metric /= box1.area();
miss_metric *= box2.area() - overlap_area;
miss_metric /= box2.area();
return miss_metric;
const int a = box1.area();
const int b = box2.area();
ASSERT_HOST(a != 0 && b != 0);
return 1.0 * (a - overlap_area) * (b - overlap_area) / a / b;
}

/// Gather consecutive blobs that match the given box into the best_state
Expand Down
13 changes: 6 additions & 7 deletions src/ccmain/control.cpp
@@ -1,8 +1,8 @@
/******************************************************************
* File: control.cpp (Formerly control.c)
* Description: Module-independent matcher controller.
* Author: Ray Smith
* Created: Thu Apr 23 11:09:58 BST 1992
* Author: Ray Smith
* Created: Thu Apr 23 11:09:58 BST 1992
* ReHacked: Tue Sep 22 08:42:49 BST 1992 Phil Cheatle
*
* (C) Copyright 1992, Hewlett-Packard Ltd.
Expand Down Expand Up @@ -577,9 +577,6 @@ void Tesseract::bigram_correction_pass(PAGE_RES *page_res) {
WERD_CHOICE *p1 = overrides_word1[i];
WERD_CHOICE *p2 = overrides_word2[i];
bigrams_list += p1->unichar_string() + " " + p2->unichar_string();
if (i == kMaxChoicesToPrint) {
bigrams_list += " ...";
}
}
choices_description = "There were many choices: {";
choices_description += bigrams_list;
Expand Down Expand Up @@ -1213,6 +1210,7 @@ float Tesseract::ClassifyBlobPlusOutlines(
C_BLOB* blob, STRING* best_str) {
C_OUTLINE_IT ol_it;
C_OUTLINE* first_to_keep = nullptr;
C_BLOB* local_blob = nullptr;
if (blob != nullptr) {
// Add the required outlines to the blob.
ol_it.set_to_list(blob->out_list());
Expand All @@ -1222,7 +1220,8 @@ float Tesseract::ClassifyBlobPlusOutlines(
if (ok_outlines[i]) {
// This outline is to be added.
if (blob == nullptr) {
blob = new C_BLOB(outlines[i]);
local_blob = new C_BLOB(outlines[i]);
blob = local_blob;
ol_it.set_to_list(blob->out_list());
} else {
ol_it.add_before_stay_put(outlines[i]);
Expand All @@ -1235,7 +1234,7 @@ float Tesseract::ClassifyBlobPlusOutlines(
if (first_to_keep == nullptr) {
// We created blob. Empty its outlines and delete it.
for (; !ol_it.empty(); ol_it.forward()) ol_it.extract();
delete blob;
delete local_blob;
cert = -c2;
} else {
// Remove the outlines that we put in.
Expand Down
2 changes: 1 addition & 1 deletion src/ccstruct/otsuthr.cpp
Expand Up @@ -58,7 +58,7 @@ int OtsuThreshold(Pix* src_pix, int left, int top, int width, int height,
OpenclDevice od;
if (od.selectedDeviceIsOpenCL() && (num_channels == 1 || num_channels == 4) &&
top == 0 && left == 0) {
od.HistogramRectOCL((unsigned char*)pixGetData(src_pix), num_channels,
od.HistogramRectOCL(pixGetData(src_pix), num_channels,
pixGetWpl(src_pix) * 4, left, top, width, height,
kHistogramSize, histogramAllChannels);

Expand Down
75 changes: 33 additions & 42 deletions src/opencl/openclwrapper.cpp
Expand Up @@ -295,14 +295,18 @@ static ds_status readProFile(const char* fileName, char** content,
status = DS_FILE_ERROR;
} else {
fseek(input, 0L, SEEK_END);
size_t size = ftell(input);
long pos = ftell(input);
rewind(input);
char* binary = new char[size];
if (fread(binary, sizeof(char), size, input) != size) {
status = DS_FILE_ERROR;
} else {
*contentSize = size;
*content = binary;
if (pos > 0) {
size_t size = pos;
char *binary = new char[size];
if (fread(binary, sizeof(char), size, input) != size) {
status = DS_FILE_ERROR;
delete[] binary;
} else {
*contentSize = size;
*content = binary;
}
}
fclose(input);
}
Expand Down Expand Up @@ -664,7 +668,6 @@ static cl_mem allocateZeroCopyBuffer(const KernelEnv& rEnv,
static Pix* mapOutputCLBuffer(const KernelEnv& rEnv, cl_mem clbuffer, Pix* pixd,
Pix* pixs, int elements, cl_mem_flags flags,
bool memcopy = false, bool sync = true) {
PROCNAME("mapOutputCLBuffer");
if (!pixd) {
if (memcopy) {
if ((pixd = pixCreateTemplate(pixs)) == nullptr)
Expand Down Expand Up @@ -888,35 +891,33 @@ int OpenclDevice::WriteBinaryToFile(const char* fileName, const char* birary,

return 1;
}

int OpenclDevice::GeneratBinFromKernelSource(cl_program program,
const char* clFileName) {
unsigned int i = 0;
cl_int clStatus;
size_t* binarySizes;
cl_uint numDevices;
cl_device_id* mpArryDevsID;
char* str = nullptr;

clStatus = clGetProgramInfo(program, CL_PROGRAM_NUM_DEVICES,
sizeof(numDevices), &numDevices, nullptr);
CHECK_OPENCL(clStatus, "clGetProgramInfo");

mpArryDevsID = (cl_device_id*)malloc(sizeof(cl_device_id) * numDevices);
if (mpArryDevsID == nullptr) {
return 0;
}
std::vector<cl_device_id> mpArryDevsID(numDevices);

/* grab the handles to all of the devices in the program. */
clStatus = clGetProgramInfo(program, CL_PROGRAM_DEVICES,
sizeof(cl_device_id) * numDevices, mpArryDevsID,
sizeof(cl_device_id) * numDevices,
&mpArryDevsID[0],
nullptr);
CHECK_OPENCL(clStatus, "clGetProgramInfo");

/* figure out the sizes of each of the binaries. */
binarySizes = (size_t*)malloc(sizeof(size_t) * numDevices);
std::vector<size_t> binarySizes(numDevices);

clStatus =
clGetProgramInfo(program, CL_PROGRAM_BINARY_SIZES,
sizeof(size_t) * numDevices, binarySizes, nullptr);
sizeof(size_t) * numDevices, &binarySizes[0], nullptr);
CHECK_OPENCL(clStatus, "clGetProgramInfo");

/* copy over all of the generated binaries. */
Expand Down Expand Up @@ -966,23 +967,15 @@ int OpenclDevice::GeneratBinFromKernelSource(cl_program program,
free(binaries[i]);
}

free(binarySizes);
binarySizes = nullptr;

free(mpArryDevsID);
mpArryDevsID = nullptr;

return 1;
}

int OpenclDevice::CompileKernelFile(GPUEnv* gpuInfo, const char* buildOption) {
// PERF_COUNT_START("CompileKernelFile")
cl_int clStatus = 0;
size_t length;
char* buildLog = nullptr;
const char* source;
size_t source_size[1];
int b_error, binary_status, binaryExisted, idx;
int binary_status, binaryExisted, idx;
cl_uint numDevices;
FILE *fd, *fd1;
const char* filename = "kernel.cl";
Expand All @@ -1007,12 +1000,13 @@ int OpenclDevice::CompileKernelFile(GPUEnv* gpuInfo, const char* buildOption) {

std::vector<cl_device_id> mpArryDevsID(numDevices);
// PERF_COUNT_SUB("get numDevices")
b_error = 0;
length = 0;
b_error |= fseek(fd, 0, SEEK_END) < 0;
b_error |= (length = ftell(fd)) <= 0;
bool b_error = fseek(fd, 0, SEEK_END) < 0;
long pos = ftell(fd);
b_error |= (pos <= 0);
size_t length = pos;
b_error |= fseek(fd, 0, SEEK_SET) < 0;
if (b_error) {
fclose(fd);
return 0;
}

Expand Down Expand Up @@ -1069,6 +1063,7 @@ int OpenclDevice::CompileKernelFile(GPUEnv* gpuInfo, const char* buildOption) {
PERF_COUNT_END
if (clStatus != CL_SUCCESS) {
printf("BuildProgram error!\n");
size_t length;
if (!gpuInfo->mnIsUserCreated) {
clStatus = clGetProgramBuildInfo(
gpuInfo->mpArryPrograms[idx], gpuInfo->mpArryDevsID[0],
Expand All @@ -1082,18 +1077,15 @@ int OpenclDevice::CompileKernelFile(GPUEnv* gpuInfo, const char* buildOption) {
printf("opencl create build log fail\n");
return 0;
}
buildLog = (char*)malloc(length);
if (buildLog == (char*)nullptr) {
return 0;
}
std::vector<char> buildLog(length);
if (!gpuInfo->mnIsUserCreated) {
clStatus = clGetProgramBuildInfo(
gpuInfo->mpArryPrograms[idx], gpuInfo->mpArryDevsID[0],
CL_PROGRAM_BUILD_LOG, length, buildLog, &length);
CL_PROGRAM_BUILD_LOG, length, &buildLog[0], &length);
} else {
clStatus = clGetProgramBuildInfo(gpuInfo->mpArryPrograms[idx],
gpuInfo->mpDevID, CL_PROGRAM_BUILD_LOG,
length, buildLog, &length);
length, &buildLog[0], &length);
}
if (clStatus != CL_SUCCESS) {
printf("opencl program build info fail\n");
Expand All @@ -1102,11 +1094,10 @@ int OpenclDevice::CompileKernelFile(GPUEnv* gpuInfo, const char* buildOption) {

fd1 = fopen("kernel-build.log", "w+");
if (fd1 != nullptr) {
fwrite(buildLog, sizeof(char), length, fd1);
fwrite(&buildLog[0], sizeof(char), length, fd1);
fclose(fd1);
}

free(buildLog);
// PERF_COUNT_SUB("build error log")
return 0;
}
Expand Down Expand Up @@ -1668,7 +1659,7 @@ void OpenclDevice::pixGetLinesCL(Pix* pixd, Pix* pixs, Pix** pix_vline,
* histogramAllChannels is laid out as all channel 0, then all channel 1...
* only supports 1 or 4 channels (bytes_per_pixel)
************************************************************************/
int OpenclDevice::HistogramRectOCL(unsigned char* imageData,
int OpenclDevice::HistogramRectOCL(void* imageData,
int bytes_per_pixel, int bytes_per_line,
int left, // always 0
int top, // always 0
Expand Down Expand Up @@ -2162,8 +2153,8 @@ static double histogramRectMicroBench(GPUEnv* env,
timespec time_funct_start, time_funct_end;
#endif

int left = 0;
int top = 0;
const int left = 0;
const int top = 0;
int kHistogramSize = 256;
int bytes_per_line = input.width * input.numChannels;
int* histogramAllChannels = new int[kHistogramSize * input.numChannels];
Expand All @@ -2179,7 +2170,7 @@ static double histogramRectMicroBench(GPUEnv* env,

OpenclDevice::gpuEnv = *env;
int retVal = OpenclDevice::HistogramRectOCL(
input.imageData, input.numChannels, bytes_per_line, top, left,
input.imageData, input.numChannels, bytes_per_line, left, top,
input.width, input.height, kHistogramSize, histogramAllChannels);

#if ON_WINDOWS
Expand Down
2 changes: 1 addition & 1 deletion src/opencl/openclwrapper.h
Expand Up @@ -275,7 +275,7 @@ class OpenclDevice {
inline static int AddKernelConfig(int kCount, const char* kName);

/* for binarization */
static int HistogramRectOCL(unsigned char* imagedata, int bytes_per_pixel,
static int HistogramRectOCL(void* imagedata, int bytes_per_pixel,
int bytes_per_line, int left, int top, int width,
int height, int kHistogramSize,
int* histogramAllChannels);
Expand Down

0 comments on commit 1a181a3

Please sign in to comment.