diff --git a/api/pdfrenderer.cpp b/api/pdfrenderer.cpp index c388bb5af6..e97d9dd6a1 100644 --- a/api/pdfrenderer.cpp +++ b/api/pdfrenderer.cpp @@ -670,7 +670,7 @@ bool TessPDFRenderer::BeginDocumentHandler() { long int size = ftell(fp); fseek(fp, 0, SEEK_SET); const std::unique_ptr buffer(new char[size]); - if (fread(buffer.get(), 1, size, fp) != size) { + if (fread(buffer.get(), 1, size, fp) != static_cast(size)) { fclose(fp); return false; } @@ -945,7 +945,7 @@ bool TessPDFRenderer::EndDocumentHandler() { if (n >= sizeof(buf)) return false; AppendString(buf); size_t pages_objsize = strlen(buf); - for (size_t i = 0; i < pages_.size(); i++) { + for (size_t i = 0; i < pages_.unsigned_size(); i++) { n = snprintf(buf, sizeof(buf), "%ld 0 R ", pages_[i]); if (n >= sizeof(buf)) return false; diff --git a/ccmain/docqual.cpp b/ccmain/docqual.cpp index 4706fb3b26..ad7f228053 100644 --- a/ccmain/docqual.cpp +++ b/ccmain/docqual.cpp @@ -513,7 +513,7 @@ BOOL8 Tesseract::terrible_word_crunch(WERD_RES *word, if ((word->best_choice->unichar_string().length () == 0) || (strspn (word->best_choice->unichar_string().string(), " ") == - word->best_choice->unichar_string().length ())) + word->best_choice->unichar_string().unsigned_size ())) crunch_mode = 1; else { adjusted_len = word->reject_map.length (); diff --git a/ccutil/genericvector.h b/ccutil/genericvector.h index 39a245d665..c0a5d9585d 100644 --- a/ccutil/genericvector.h +++ b/ccutil/genericvector.h @@ -72,6 +72,12 @@ class GenericVector { int size() const { return size_used_; } + // Workaround to avoid g++ -Wsign-compare warnings. + unsigned int unsigned_size() const { + static_assert(sizeof(size_used_) <= sizeof(unsigned int), ""); + assert(0 <= size_used_); + return static_cast(size_used_); + } int size_reserved() const { return size_reserved_; } @@ -880,7 +886,7 @@ bool GenericVector::write( } delete cb; } else { - if (fwrite(data_, sizeof(T), size_used_, f) != size_used_) return false; + if (fwrite(data_, sizeof(T), size_used_, f) != unsigned_size()) return false; } return true; } @@ -912,7 +918,7 @@ bool GenericVector::read( template bool GenericVector::Serialize(FILE* fp) const { if (fwrite(&size_used_, sizeof(size_used_), 1, fp) != 1) return false; - if (fwrite(data_, sizeof(*data_), size_used_, fp) != size_used_) return false; + if (fwrite(data_, sizeof(*data_), size_used_, fp) != unsigned_size()) return false; return true; } template @@ -933,7 +939,7 @@ bool GenericVector::DeSerialize(bool swap, FILE* fp) { if (swap) Reverse32(&reserved); reserve(reserved); size_used_ = reserved; - if (fread(data_, sizeof(T), size_used_, fp) != size_used_) return false; + if (fread(data_, sizeof(T), size_used_, fp) != unsigned_size()) return false; if (swap) { for (int i = 0; i < size_used_; ++i) ReverseN(&data_[i], sizeof(data_[i])); @@ -982,7 +988,7 @@ bool GenericVector::SerializeClasses(tesseract::TFile* fp) const { // If swap is true, assumes a big/little-endian swap is needed. template bool GenericVector::DeSerializeClasses(bool swap, FILE* fp) { - uinT32 reserved; + inT32 reserved; if (fread(&reserved, sizeof(reserved), 1, fp) != 1) return false; if (swap) Reverse32(&reserved); T empty; @@ -994,7 +1000,7 @@ bool GenericVector::DeSerializeClasses(bool swap, FILE* fp) { } template bool GenericVector::DeSerializeClasses(tesseract::TFile* fp) { - uinT32 reserved; + inT32 reserved; if (fp->FReadEndian(&reserved, sizeof(reserved), 1) != 1) return false; T empty; init_to_size(reserved, empty); @@ -1005,7 +1011,7 @@ bool GenericVector::DeSerializeClasses(tesseract::TFile* fp) { } template bool GenericVector::SkipDeSerializeClasses(tesseract::TFile* fp) { - uinT32 reserved; + inT32 reserved; if (fp->FReadEndian(&reserved, sizeof(reserved), 1) != 1) return false; for (int i = 0; i < reserved; ++i) { if (!T::SkipDeSerialize(fp)) return false; diff --git a/ccutil/strngs.h b/ccutil/strngs.h index 5ca9c4fc8c..17169e21ae 100644 --- a/ccutil/strngs.h +++ b/ccutil/strngs.h @@ -20,6 +20,7 @@ #ifndef STRNGS_H #define STRNGS_H +#include #include #include #include "platform.h" @@ -66,6 +67,12 @@ class TESS_API STRING BOOL8 contains(const char c) const; inT32 length() const; inT32 size() const { return length(); } + // Workaround to avoid g++ -Wsign-compare warnings. + uinT32 unsigned_size() const { + const inT32 len = length(); + assert(0 <= len); + return static_cast(len); + } const char *string() const; const char *c_str() const; diff --git a/classify/blobclass.cpp b/classify/blobclass.cpp index 314a71f726..2edb42b84f 100644 --- a/classify/blobclass.cpp +++ b/classify/blobclass.cpp @@ -97,7 +97,7 @@ void Classify::LearnBlob(const STRING& fontname, TBLOB* blob, bool Classify::WriteTRFile(const STRING& filename) { STRING tr_filename = filename + ".tr"; FILE* fp = Efopen(tr_filename.string(), "wb"); - int len = tr_file_data_.length(); + size_t len = tr_file_data_.length(); bool result = fwrite(&tr_file_data_[0], sizeof(tr_file_data_[0]), len, fp) == len; fclose(fp); diff --git a/classify/featdefs.cpp b/classify/featdefs.cpp index dd31f91d86..52b22edc7a 100644 --- a/classify/featdefs.cpp +++ b/classify/featdefs.cpp @@ -139,10 +139,8 @@ void InitFeatureDefs(FEATURE_DEFS_STRUCT *featuredefs) { * @note History: Wed May 23 13:52:19 1990, DSJ, Created. */ void FreeCharDescription(CHAR_DESC CharDesc) { - int i; - if (CharDesc) { - for (i = 0; i < CharDesc->NumFeatureSets; i++) + for (size_t i = 0; i < CharDesc->NumFeatureSets; i++) FreeFeatureSet (CharDesc->FeatureSets[i]); Efree(CharDesc); } @@ -163,12 +161,10 @@ void FreeCharDescription(CHAR_DESC CharDesc) { */ CHAR_DESC NewCharDescription(const FEATURE_DEFS_STRUCT &FeatureDefs) { CHAR_DESC CharDesc; - int i; - CharDesc = (CHAR_DESC) Emalloc (sizeof (CHAR_DESC_STRUCT)); CharDesc->NumFeatureSets = FeatureDefs.NumFeatureTypes; - for (i = 0; i < CharDesc->NumFeatureSets; i++) + for (size_t i = 0; i < CharDesc->NumFeatureSets; i++) CharDesc->FeatureSets[i] = NULL; return (CharDesc); @@ -196,16 +192,15 @@ CHAR_DESC NewCharDescription(const FEATURE_DEFS_STRUCT &FeatureDefs) { */ void WriteCharDescription(const FEATURE_DEFS_STRUCT& FeatureDefs, CHAR_DESC CharDesc, STRING* str) { - int Type; int NumSetsToWrite = 0; - for (Type = 0; Type < CharDesc->NumFeatureSets; Type++) + for (size_t Type = 0; Type < CharDesc->NumFeatureSets; Type++) if (CharDesc->FeatureSets[Type]) NumSetsToWrite++; str->add_str_int(" ", NumSetsToWrite); *str += "\n"; - for (Type = 0; Type < CharDesc->NumFeatureSets; Type++) { + for (size_t Type = 0; Type < CharDesc->NumFeatureSets; Type++) { if (CharDesc->FeatureSets[Type]) { *str += FeatureDefs.FeatureDesc[Type]->ShortName; *str += " "; @@ -220,7 +215,7 @@ bool ValidCharDescription(const FEATURE_DEFS_STRUCT &FeatureDefs, CHAR_DESC CharDesc) { bool anything_written = false; bool well_formed = true; - for (int Type = 0; Type < CharDesc->NumFeatureSets; Type++) { + for (size_t Type = 0; Type < CharDesc->NumFeatureSets; Type++) { if (CharDesc->FeatureSets[Type]) { for (int i = 0; i < CharDesc->FeatureSets[Type]->NumFeatures; i++) { FEATURE feat = CharDesc->FeatureSets[Type]->Features[i]; diff --git a/lstm/networkio.cpp b/lstm/networkio.cpp index 92edff2ab6..dfa46aa4cc 100644 --- a/lstm/networkio.cpp +++ b/lstm/networkio.cpp @@ -184,7 +184,7 @@ void NetworkIO::FromPixes(const StaticShape& shape, stride_map_.SetStride(h_w_pairs); ResizeToMap(int_mode(), stride_map_, shape.depth()); // Iterate over the images again to copy the data. - for (int b = 0; b < pixes.size(); ++b) { + for (size_t b = 0; b < pixes.size(); ++b) { Pix* pix = const_cast(pixes[b]); float black = 0.0f, white = 255.0f; if (shape.depth() != 3) ComputeBlackWhite(pix, &black, &white); diff --git a/lstm/stridemap.cpp b/lstm/stridemap.cpp index b3e70bca9e..26a2cf6669 100644 --- a/lstm/stridemap.cpp +++ b/lstm/stridemap.cpp @@ -43,7 +43,8 @@ bool StrideMap::Index::IsLast(FlexDimensions dimension) const { int StrideMap::Index::MaxIndexOfDim(FlexDimensions dim) const { int max_index = stride_map_->shape_[dim] - 1; if (dim == FD_BATCH) return max_index; - int batch = indices_[FD_BATCH]; + assert(0 <= indices_[FD_BATCH]); + const size_t batch = indices_[FD_BATCH]; if (dim == FD_HEIGHT) { if (batch >= stride_map_->heights_.size() || stride_map_->heights_[batch] > max_index) diff --git a/training/commontraining.cpp b/training/commontraining.cpp index 0d32daf28b..af483ff917 100644 --- a/training/commontraining.cpp +++ b/training/commontraining.cpp @@ -368,7 +368,6 @@ void ReadTrainingSamples(const FEATURE_DEFS_STRUCT& feature_defs, LABELEDLIST char_sample; FEATURE_SET feature_samples; CHAR_DESC char_desc; - int i; int feature_type = ShortNameToFeatureType(feature_defs, feature_name); // Zero out the font_sample_count for all the classes. LIST it = *training_samples; @@ -404,7 +403,7 @@ void ReadTrainingSamples(const FEATURE_DEFS_STRUCT& feature_defs, } else { FreeFeatureSet(feature_samples); } - for (i = 0; i < char_desc->NumFeatureSets; i++) { + for (size_t i = 0; i < char_desc->NumFeatureSets; i++) { if (feature_type != i) FreeFeatureSet(char_desc->FeatureSets[i]); }