Skip to content

Commit 47867c2

Browse files
Junjie Qimeta-codesync[bot]
authored andcommitted
Add value-initialization to uninitialized member variables
Summary: Fix 59 `cppcoreguidelines-pro-type-member-init` lint warnings by adding `{}` value-initialization to uninitialized member fields across 17 files. This ensures all members are zero/null-initialized by default, preventing potential use of uninitialized values. Affected files: Index.cpp, Index2Layer.cpp, IndexAdditiveQuantizer.cpp, IndexAdditiveQuantizerFastScan.h, IndexBinaryHash.h, IndexBinaryIVF.cpp, IndexFastScan.h, IndexHNSW.h, IndexIVF.h, IndexIVFAdditiveQuantizer.cpp, IndexIVFFastScan.h, IndexIVFFlat.h, IndexIVFPQ.cpp, IndexIVFPQ.h, IndexPQ.cpp, IndexPQ.h, IndexRowwiseMinMax.cpp. Differential Revision: D100577736
1 parent f415dcb commit 47867c2

15 files changed

+40
-39
lines changed

faiss/Index.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ struct GenericDistanceComputer : DistanceComputer {
168168
size_t d;
169169
const Index& storage;
170170
std::vector<float> buf;
171-
const float* q;
171+
const float* q{};
172172

173173
explicit GenericDistanceComputer(const Index& storage_in)
174174
: storage(storage_in) {

faiss/Index2Layer.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,9 @@ struct Distance2Level : DistanceComputer {
138138
size_t d;
139139
const Index2Layer& storage;
140140
std::vector<float> buf;
141-
const float* q;
141+
const float* q{};
142142

143-
const float *pq_l1_tab, *pq_l2_tab;
143+
const float *pq_l1_tab{}, *pq_l2_tab;
144144

145145
explicit Distance2Level(const Index2Layer& storage_) : storage(storage_) {
146146
d = storage_.d;
@@ -162,7 +162,7 @@ struct Distance2Level : DistanceComputer {
162162

163163
// well optimized for xNN+PQNN
164164
struct DistanceXPQ4 : Distance2Level {
165-
int M, k;
165+
int M, k{};
166166

167167
explicit DistanceXPQ4(const Index2Layer& storage_)
168168
: Distance2Level(storage_) {

faiss/IndexAdditiveQuantizer.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ struct AQDistanceComputerDecompress : FlatCodesDistanceComputer {
5252
vd(vd_),
5353
d(iaq.d) {}
5454

55-
const float* q;
55+
const float* q{};
5656
void set_query(const float* x) final {
5757
q = x;
5858
}
@@ -83,7 +83,7 @@ struct AQDistanceComputerLUT : FlatCodesDistanceComputer {
8383
aq(*iaq.aq),
8484
d(iaq.d) {}
8585

86-
float bias;
86+
float bias{};
8787
void set_query(const float* x) final {
8888
q = x;
8989
// this is quite sub-optimal for multiple queries

faiss/IndexAdditiveQuantizerFastScan.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ namespace faiss {
2828
*/
2929

3030
struct IndexAdditiveQuantizerFastScan : IndexFastScan {
31-
AdditiveQuantizer* aq;
31+
AdditiveQuantizer* aq = nullptr;
3232
using Search_type_t = AdditiveQuantizer::Search_type_t;
3333

3434
bool rescale_norm = true;

faiss/IndexBinaryHash.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,10 @@ struct IndexBinaryHash : IndexBinary {
6666
};
6767

6868
struct IndexBinaryHashStats {
69-
size_t nq; // nb of queries run
70-
size_t n0; // nb of empty lists
71-
size_t nlist; // nb of non-empty inverted lists scanned
72-
size_t ndis{}; // nb of distances computed
69+
size_t nq; // nb of queries run
70+
size_t n0; // nb of empty lists
71+
size_t nlist; // nb of non-empty inverted lists scanned
72+
size_t ndis; // nb of distances computed
7373

7474
IndexBinaryHashStats() {
7575
reset();

faiss/IndexBinaryIVF.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ struct IVFBinaryScannerL2 : BinaryInvertedListScanner {
354354
hc.set(query_vector, code_size);
355355
}
356356

357-
idx_t list_no;
357+
idx_t list_no{};
358358
void set_list(idx_t list_no_2, uint8_t /* coarse_dis */) override {
359359
this->list_no = list_no_2;
360360
}
@@ -611,10 +611,10 @@ template <class HammingComputer, int NQ, int K>
611611
struct BlockSearch {
612612
HammingComputer hcs[NQ];
613613
// heaps to update for each query
614-
int32_t* distances[NQ];
615-
idx_t* labels[NQ];
614+
int32_t* distances[NQ]{};
615+
idx_t* labels[NQ]{};
616616
// curent top of heap
617-
int32_t heap_tops[NQ];
617+
int32_t heap_tops[NQ]{};
618618

619619
BlockSearch(
620620
size_t code_size,
@@ -648,10 +648,10 @@ struct BlockSearchVariableK {
648648
int k;
649649
HammingComputer hcs[NQ];
650650
// heaps to update for each query
651-
int32_t* distances[NQ];
652-
idx_t* labels[NQ];
651+
int32_t* distances[NQ]{};
652+
idx_t* labels[NQ]{};
653653
// curent top of heap
654-
int32_t heap_tops[NQ];
654+
int32_t heap_tops[NQ]{};
655655

656656
BlockSearchVariableK(
657657
size_t code_size,

faiss/IndexFastScan.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ struct IndexFastScan : Index {
4444

4545
// vector quantizer
4646
size_t M;
47-
size_t nbits;
48-
size_t ksub;
47+
size_t nbits = 0;
48+
size_t ksub = 0;
4949
size_t code_size;
5050

5151
// packed version of the codes

faiss/IndexHNSW.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ struct IndexHNSWCagra : IndexHNSW {
261261

262262
faiss::NumericType get_numeric_type() const;
263263
void set_numeric_type(faiss::NumericType numeric_type);
264-
NumericType numeric_type_;
264+
NumericType numeric_type_ = Float32;
265265
};
266266

267267
} // namespace faiss

faiss/IndexIVFAdditiveQuantizer.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,14 +181,14 @@ struct AQInvertedListScanner : InvertedListScanner {
181181
tmp.resize(ia.d);
182182
}
183183

184-
const float* q0;
184+
const float* q0{};
185185

186186
/// from now on we handle this query.
187187
void set_query(const float* query_vector) override {
188188
q0 = query_vector;
189189
}
190190

191-
const float* q;
191+
const float* q{};
192192
/// following codes come from this inverted list
193193
void set_list(idx_t list_no_, float /*coarse_dis*/) override {
194194
this->list_no = list_no_;

faiss/IndexIVFFastScan.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,14 @@ struct Quantizer;
4141

4242
struct IndexIVFFastScan : IndexIVF {
4343
// size of the kernel
44-
int bbs; // set at build time
44+
int bbs = 0; // set at build time
4545

46-
size_t M;
47-
size_t nbits;
48-
size_t ksub;
46+
size_t M = 0;
47+
size_t nbits = 0;
48+
size_t ksub = 0;
4949

5050
// M rounded up to a multiple of 2
51-
size_t M2;
51+
size_t M2 = 0;
5252

5353
// search-time implementation
5454
int implem = 0;

0 commit comments

Comments
 (0)