Skip to content

Commit 75243f9

Browse files
committed
Fewer g++ -Wsign-compare warnings
1 parent 2a7479d commit 75243f9

File tree

9 files changed

+32
-24
lines changed

9 files changed

+32
-24
lines changed

api/pdfrenderer.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -671,7 +671,7 @@ bool TessPDFRenderer::BeginDocumentHandler() {
671671
long int size = ftell(fp);
672672
fseek(fp, 0, SEEK_SET);
673673
char *buffer = new char[size];
674-
if (fread(buffer, 1, size, fp) != size) {
674+
if (fread(buffer, 1, size, fp) != static_cast<unsigned long>(size)) {
675675
fclose(fp);
676676
delete[] buffer;
677677
return false;
@@ -952,7 +952,7 @@ bool TessPDFRenderer::EndDocumentHandler() {
952952
if (n >= sizeof(buf)) return false;
953953
AppendString(buf);
954954
size_t pages_objsize = strlen(buf);
955-
for (size_t i = 0; i < pages_.size(); i++) {
955+
for (size_t i = 0; i < pages_.unsigned_size(); i++) {
956956
n = snprintf(buf, sizeof(buf),
957957
"%ld 0 R ", pages_[i]);
958958
if (n >= sizeof(buf)) return false;

ccmain/docqual.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -513,7 +513,7 @@ BOOL8 Tesseract::terrible_word_crunch(WERD_RES *word,
513513

514514
if ((word->best_choice->unichar_string().length () == 0) ||
515515
(strspn (word->best_choice->unichar_string().string(), " ") ==
516-
word->best_choice->unichar_string().length ()))
516+
word->best_choice->unichar_string().unsigned_size ()))
517517
crunch_mode = 1;
518518
else {
519519
adjusted_len = word->reject_map.length ();

ccutil/genericvector.h

+12-6
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,12 @@ class GenericVector {
7272
int size() const {
7373
return size_used_;
7474
}
75+
// Workaround to avoid g++ -Wsign-compare warnings.
76+
unsigned int unsigned_size() const {
77+
static_assert(sizeof(size_used_) <= sizeof(unsigned int), "");
78+
assert(0 <= size_used_);
79+
return static_cast<unsigned int>(size_used_);
80+
}
7581
int size_reserved() const {
7682
return size_reserved_;
7783
}
@@ -880,7 +886,7 @@ bool GenericVector<T>::write(
880886
}
881887
delete cb;
882888
} else {
883-
if (fwrite(data_, sizeof(T), size_used_, f) != size_used_) return false;
889+
if (fwrite(data_, sizeof(T), size_used_, f) != unsigned_size()) return false;
884890
}
885891
return true;
886892
}
@@ -912,7 +918,7 @@ bool GenericVector<T>::read(
912918
template <typename T>
913919
bool GenericVector<T>::Serialize(FILE* fp) const {
914920
if (fwrite(&size_used_, sizeof(size_used_), 1, fp) != 1) return false;
915-
if (fwrite(data_, sizeof(*data_), size_used_, fp) != size_used_) return false;
921+
if (fwrite(data_, sizeof(*data_), size_used_, fp) != unsigned_size()) return false;
916922
return true;
917923
}
918924
template <typename T>
@@ -933,7 +939,7 @@ bool GenericVector<T>::DeSerialize(bool swap, FILE* fp) {
933939
if (swap) Reverse32(&reserved);
934940
reserve(reserved);
935941
size_used_ = reserved;
936-
if (fread(data_, sizeof(T), size_used_, fp) != size_used_) return false;
942+
if (fread(data_, sizeof(T), size_used_, fp) != unsigned_size()) return false;
937943
if (swap) {
938944
for (int i = 0; i < size_used_; ++i)
939945
ReverseN(&data_[i], sizeof(data_[i]));
@@ -982,7 +988,7 @@ bool GenericVector<T>::SerializeClasses(tesseract::TFile* fp) const {
982988
// If swap is true, assumes a big/little-endian swap is needed.
983989
template <typename T>
984990
bool GenericVector<T>::DeSerializeClasses(bool swap, FILE* fp) {
985-
uinT32 reserved;
991+
inT32 reserved;
986992
if (fread(&reserved, sizeof(reserved), 1, fp) != 1) return false;
987993
if (swap) Reverse32(&reserved);
988994
T empty;
@@ -994,7 +1000,7 @@ bool GenericVector<T>::DeSerializeClasses(bool swap, FILE* fp) {
9941000
}
9951001
template <typename T>
9961002
bool GenericVector<T>::DeSerializeClasses(tesseract::TFile* fp) {
997-
uinT32 reserved;
1003+
inT32 reserved;
9981004
if (fp->FReadEndian(&reserved, sizeof(reserved), 1) != 1) return false;
9991005
T empty;
10001006
init_to_size(reserved, empty);
@@ -1005,7 +1011,7 @@ bool GenericVector<T>::DeSerializeClasses(tesseract::TFile* fp) {
10051011
}
10061012
template <typename T>
10071013
bool GenericVector<T>::SkipDeSerializeClasses(tesseract::TFile* fp) {
1008-
uinT32 reserved;
1014+
inT32 reserved;
10091015
if (fp->FReadEndian(&reserved, sizeof(reserved), 1) != 1) return false;
10101016
for (int i = 0; i < reserved; ++i) {
10111017
if (!T::SkipDeSerialize(fp)) return false;

ccutil/strngs.h

+7
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#ifndef STRNGS_H
2121
#define STRNGS_H
2222

23+
#include <assert.h>
2324
#include <stdio.h>
2425
#include <string.h>
2526
#include "platform.h"
@@ -66,6 +67,12 @@ class TESS_API STRING
6667
BOOL8 contains(const char c) const;
6768
inT32 length() const;
6869
inT32 size() const { return length(); }
70+
// Workaround to avoid g++ -Wsign-compare warnings.
71+
uinT32 unsigned_size() const {
72+
const inT32 len = length();
73+
assert(0 <= len);
74+
return static_cast<uinT32>(len);
75+
}
6976
const char *string() const;
7077
const char *c_str() const;
7178

classify/blobclass.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ void Classify::LearnBlob(const STRING& fontname, TBLOB* blob,
9797
bool Classify::WriteTRFile(const STRING& filename) {
9898
STRING tr_filename = filename + ".tr";
9999
FILE* fp = Efopen(tr_filename.string(), "wb");
100-
int len = tr_file_data_.length();
100+
size_t len = tr_file_data_.length();
101101
bool result =
102102
fwrite(&tr_file_data_[0], sizeof(tr_file_data_[0]), len, fp) == len;
103103
fclose(fp);

classify/featdefs.cpp

+5-10
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,8 @@ void InitFeatureDefs(FEATURE_DEFS_STRUCT *featuredefs) {
139139
* @note History: Wed May 23 13:52:19 1990, DSJ, Created.
140140
*/
141141
void FreeCharDescription(CHAR_DESC CharDesc) {
142-
int i;
143-
144142
if (CharDesc) {
145-
for (i = 0; i < CharDesc->NumFeatureSets; i++)
143+
for (size_t i = 0; i < CharDesc->NumFeatureSets; i++)
146144
FreeFeatureSet (CharDesc->FeatureSets[i]);
147145
Efree(CharDesc);
148146
}
@@ -163,12 +161,10 @@ void FreeCharDescription(CHAR_DESC CharDesc) {
163161
*/
164162
CHAR_DESC NewCharDescription(const FEATURE_DEFS_STRUCT &FeatureDefs) {
165163
CHAR_DESC CharDesc;
166-
int i;
167-
168164
CharDesc = (CHAR_DESC) Emalloc (sizeof (CHAR_DESC_STRUCT));
169165
CharDesc->NumFeatureSets = FeatureDefs.NumFeatureTypes;
170166

171-
for (i = 0; i < CharDesc->NumFeatureSets; i++)
167+
for (size_t i = 0; i < CharDesc->NumFeatureSets; i++)
172168
CharDesc->FeatureSets[i] = NULL;
173169

174170
return (CharDesc);
@@ -196,16 +192,15 @@ CHAR_DESC NewCharDescription(const FEATURE_DEFS_STRUCT &FeatureDefs) {
196192
*/
197193
void WriteCharDescription(const FEATURE_DEFS_STRUCT& FeatureDefs,
198194
CHAR_DESC CharDesc, STRING* str) {
199-
int Type;
200195
int NumSetsToWrite = 0;
201196

202-
for (Type = 0; Type < CharDesc->NumFeatureSets; Type++)
197+
for (size_t Type = 0; Type < CharDesc->NumFeatureSets; Type++)
203198
if (CharDesc->FeatureSets[Type])
204199
NumSetsToWrite++;
205200

206201
str->add_str_int(" ", NumSetsToWrite);
207202
*str += "\n";
208-
for (Type = 0; Type < CharDesc->NumFeatureSets; Type++) {
203+
for (size_t Type = 0; Type < CharDesc->NumFeatureSets; Type++) {
209204
if (CharDesc->FeatureSets[Type]) {
210205
*str += FeatureDefs.FeatureDesc[Type]->ShortName;
211206
*str += " ";
@@ -220,7 +215,7 @@ bool ValidCharDescription(const FEATURE_DEFS_STRUCT &FeatureDefs,
220215
CHAR_DESC CharDesc) {
221216
bool anything_written = false;
222217
bool well_formed = true;
223-
for (int Type = 0; Type < CharDesc->NumFeatureSets; Type++) {
218+
for (size_t Type = 0; Type < CharDesc->NumFeatureSets; Type++) {
224219
if (CharDesc->FeatureSets[Type]) {
225220
for (int i = 0; i < CharDesc->FeatureSets[Type]->NumFeatures; i++) {
226221
FEATURE feat = CharDesc->FeatureSets[Type]->Features[i];

lstm/networkio.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ void NetworkIO::FromPixes(const StaticShape& shape,
184184
stride_map_.SetStride(h_w_pairs);
185185
ResizeToMap(int_mode(), stride_map_, shape.depth());
186186
// Iterate over the images again to copy the data.
187-
for (int b = 0; b < pixes.size(); ++b) {
187+
for (size_t b = 0; b < pixes.size(); ++b) {
188188
Pix* pix = const_cast<Pix*>(pixes[b]);
189189
float black = 0.0f, white = 255.0f;
190190
if (shape.depth() != 3) ComputeBlackWhite(pix, &black, &white);

lstm/stridemap.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ bool StrideMap::Index::IsLast(FlexDimensions dimension) const {
4343
int StrideMap::Index::MaxIndexOfDim(FlexDimensions dim) const {
4444
int max_index = stride_map_->shape_[dim] - 1;
4545
if (dim == FD_BATCH) return max_index;
46-
int batch = indices_[FD_BATCH];
46+
assert(0 <= indices_[FD_BATCH]);
47+
const size_t batch = indices_[FD_BATCH];
4748
if (dim == FD_HEIGHT) {
4849
if (batch >= stride_map_->heights_.size() ||
4950
stride_map_->heights_[batch] > max_index)

training/commontraining.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,6 @@ void ReadTrainingSamples(const FEATURE_DEFS_STRUCT& feature_defs,
368368
LABELEDLIST char_sample;
369369
FEATURE_SET feature_samples;
370370
CHAR_DESC char_desc;
371-
int i;
372371
int feature_type = ShortNameToFeatureType(feature_defs, feature_name);
373372
// Zero out the font_sample_count for all the classes.
374373
LIST it = *training_samples;
@@ -404,7 +403,7 @@ void ReadTrainingSamples(const FEATURE_DEFS_STRUCT& feature_defs,
404403
} else {
405404
FreeFeatureSet(feature_samples);
406405
}
407-
for (i = 0; i < char_desc->NumFeatureSets; i++) {
406+
for (size_t i = 0; i < char_desc->NumFeatureSets; i++) {
408407
if (feature_type != i)
409408
FreeFeatureSet(char_desc->FeatureSets[i]);
410409
}

0 commit comments

Comments
 (0)