Skip to content

Commit b5bc1e6

Browse files
committed
Use size_t for unsigned lengths to address conversion warnings
1 parent ccc6095 commit b5bc1e6

File tree

2 files changed

+37
-37
lines changed

2 files changed

+37
-37
lines changed

src/CppSQLite3.cpp

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ SQLite3Memory::SQLite3Memory() :
3535
{
3636
}
3737

38-
SQLite3Memory::SQLite3Memory(int nBufferLen) :
39-
mpBuf(sqlite3_malloc(nBufferLen)),
38+
SQLite3Memory::SQLite3Memory(size_t nBufferLen) :
39+
mpBuf(sqlite3_malloc(static_cast<int>(nBufferLen))),
4040
mnBufferLen(nBufferLen)
4141
{
4242
if (!mpBuf && mnBufferLen>0)
@@ -66,7 +66,7 @@ SQLite3Memory::~SQLite3Memory()
6666
}
6767

6868
SQLite3Memory::SQLite3Memory(SQLite3Memory const& other) :
69-
mpBuf(sqlite3_malloc(other.mnBufferLen)),
69+
mpBuf(sqlite3_malloc(static_cast<int>(other.mnBufferLen))),
7070
mnBufferLen(other.mnBufferLen)
7171
{
7272
if (!mpBuf && mnBufferLen>0)
@@ -128,7 +128,7 @@ CppSQLite3Exception::CppSQLite3Exception(const int nErrCode,
128128

129129
if (bDeleteMsg && szErrMess)
130130
{
131-
sqlite3_free((void*)szErrMess);
131+
sqlite3_free(static_cast<void*>(const_cast<char*>(szErrMess)));
132132
}
133133
}
134134

@@ -238,7 +238,7 @@ CppSQLite3Binary::~CppSQLite3Binary()
238238
}
239239

240240

241-
void CppSQLite3Binary::setBinary(const unsigned char* pBuf, int nLen)
241+
void CppSQLite3Binary::setBinary(const unsigned char* pBuf, size_t nLen)
242242
{
243243
mpBuf = allocBuffer(nLen);
244244
memcpy(mpBuf, pBuf, nLen);
@@ -249,10 +249,10 @@ void CppSQLite3Binary::setEncoded(const unsigned char* pBuf)
249249
{
250250
clear();
251251

252-
mnEncodedLen = strlen((const char*)pBuf);
252+
mnEncodedLen = strlen(reinterpret_cast<const char*>(pBuf));
253253
mnBufferLen = mnEncodedLen + 1; // Allow for NULL terminator
254254

255-
mpBuf = (unsigned char*)malloc(mnBufferLen);
255+
mpBuf = reinterpret_cast<unsigned char*>(malloc(mnBufferLen));
256256

257257
if (!mpBuf)
258258
{
@@ -270,9 +270,9 @@ const unsigned char* CppSQLite3Binary::getEncoded()
270270
{
271271
if (!mbEncoded)
272272
{
273-
unsigned char* ptmp = (unsigned char*)malloc(mnBinaryLen);
273+
unsigned char* ptmp = reinterpret_cast<unsigned char*>(malloc(mnBinaryLen));
274274
memcpy(ptmp, mpBuf, mnBinaryLen);
275-
mnEncodedLen = sqlite3_encode_binary(ptmp, mnBinaryLen, mpBuf);
275+
mnEncodedLen = static_cast<size_t>(sqlite3_encode_binary(ptmp, static_cast<int>(mnBinaryLen), mpBuf));
276276
free(ptmp);
277277
mbEncoded = true;
278278
}
@@ -286,30 +286,30 @@ const unsigned char* CppSQLite3Binary::getBinary()
286286
if (mbEncoded)
287287
{
288288
// in/out buffers can be the same
289-
mnBinaryLen = sqlite3_decode_binary(mpBuf, mpBuf);
289+
int result = sqlite3_decode_binary(mpBuf, mpBuf);
290290

291-
if (mnBinaryLen == -1)
291+
if (result == -1)
292292
{
293293
throw CppSQLite3Exception(CPPSQLITE_ERROR,
294294
"Cannot decode binary",
295295
DONT_DELETE_MSG);
296296
}
297-
297+
mnBinaryLen = static_cast<size_t>(result);
298298
mbEncoded = false;
299299
}
300300

301301
return mpBuf;
302302
}
303303

304304

305-
int CppSQLite3Binary::getBinaryLength()
305+
size_t CppSQLite3Binary::getBinaryLength()
306306
{
307307
getBinary();
308308
return mnBinaryLen;
309309
}
310310

311311

312-
unsigned char* CppSQLite3Binary::allocBuffer(int nLen)
312+
unsigned char* CppSQLite3Binary::allocBuffer(size_t nLen)
313313
{
314314
clear();
315315

@@ -319,7 +319,7 @@ unsigned char* CppSQLite3Binary::allocBuffer(int nLen)
319319
mnBinaryLen = nLen;
320320
mnBufferLen = 3 + (257*nLen)/254;
321321

322-
mpBuf = (unsigned char*)malloc(mnBufferLen);
322+
mpBuf = static_cast<unsigned char*>(malloc(mnBufferLen));
323323

324324
if (!mpBuf)
325325
{
@@ -430,14 +430,14 @@ const char* CppSQLite3Query::fieldValue(int nField) const
430430
DONT_DELETE_MSG);
431431
}
432432

433-
return (const char*)sqlite3_column_text(mpVM, nField);
433+
return reinterpret_cast<const char*>(sqlite3_column_text(mpVM, nField));
434434
}
435435

436436

437437
const char* CppSQLite3Query::fieldValue(const char* szField) const
438438
{
439439
int nField = fieldIndex(szField);
440-
return (const char*)sqlite3_column_text(mpVM, nField);
440+
return reinterpret_cast<const char*>(sqlite3_column_text(mpVM, nField));
441441
}
442442

443443

@@ -529,7 +529,7 @@ const char* CppSQLite3Query::getStringField(int nField, const char* szNullValue/
529529
}
530530
else
531531
{
532-
return (const char*)sqlite3_column_text(mpVM, nField);
532+
return reinterpret_cast<const char*>(sqlite3_column_text(mpVM, nField));
533533
}
534534
}
535535

@@ -541,7 +541,7 @@ const char* CppSQLite3Query::getStringField(const char* szField, const char* szN
541541
}
542542

543543

544-
const unsigned char* CppSQLite3Query::getBlobField(int nField, int& nLen) const
544+
const unsigned char* CppSQLite3Query::getBlobField(int nField, size_t& nLen) const
545545
{
546546
checkVM();
547547

@@ -552,12 +552,12 @@ const unsigned char* CppSQLite3Query::getBlobField(int nField, int& nLen) const
552552
DONT_DELETE_MSG);
553553
}
554554

555-
nLen = sqlite3_column_bytes(mpVM, nField);
556-
return (const unsigned char*)sqlite3_column_blob(mpVM, nField);
555+
nLen = static_cast<size_t>(sqlite3_column_bytes(mpVM, nField));
556+
return reinterpret_cast<const unsigned char*>(sqlite3_column_blob(mpVM, nField));
557557
}
558558

559559

560-
const unsigned char* CppSQLite3Query::getBlobField(const char* szField, int& nLen) const
560+
const unsigned char* CppSQLite3Query::getBlobField(const char* szField, size_t& nLen) const
561561
{
562562
int nField = fieldIndex(szField);
563563
return getBlobField(nField, nLen);
@@ -1147,11 +1147,11 @@ void CppSQLite3Statement::bind(int nParam, const double dValue)
11471147
}
11481148

11491149

1150-
void CppSQLite3Statement::bind(int nParam, const unsigned char* blobValue, int nLen)
1150+
void CppSQLite3Statement::bind(int nParam, const unsigned char* blobValue, size_t nLen)
11511151
{
11521152
checkVM();
1153-
int nRes = sqlite3_bind_blob(mpVM, nParam,
1154-
(const void*)blobValue, nLen, SQLITE_TRANSIENT);
1153+
int nRes = sqlite3_bind_blob(mpVM, nParam, static_cast<const void*>(blobValue),
1154+
static_cast<int>(nLen), SQLITE_TRANSIENT);
11551155

11561156
if (nRes != SQLITE_OK)
11571157
{

src/CppSQLite3.h

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ namespace detail
2727
// Default constructor
2828
SQLite3Memory();
2929
// Constructor that allocates memory of a given size
30-
SQLite3Memory(int nBufferLen);
30+
SQLite3Memory(size_t nBufferLen);
3131
// Constructor that formats a string with sqlite memory allocation
3232
SQLite3Memory(const char* szFormat, va_list list);
3333
// Destructor
@@ -46,7 +46,7 @@ namespace detail
4646
// Swap operation
4747
void swap(SQLite3Memory& other);
4848

49-
int getLength() const { return mnBufferLen; }
49+
size_t getLength() const { return mnBufferLen; }
5050

5151
void* getBuffer() const { return mpBuf; }
5252

@@ -55,7 +55,7 @@ namespace detail
5555
private:
5656

5757
void* mpBuf;
58-
int mnBufferLen;
58+
size_t mnBufferLen;
5959
};
6060
}
6161

@@ -110,24 +110,24 @@ class CppSQLite3Binary
110110

111111
~CppSQLite3Binary();
112112

113-
void setBinary(const unsigned char* pBuf, int nLen);
113+
void setBinary(const unsigned char* pBuf, size_t nLen);
114114
void setEncoded(const unsigned char* pBuf);
115115

116116
const unsigned char* getEncoded();
117117
const unsigned char* getBinary();
118118

119-
int getBinaryLength();
119+
size_t getBinaryLength();
120120

121-
unsigned char* allocBuffer(int nLen);
121+
unsigned char* allocBuffer(size_t nLen);
122122

123123
void clear();
124124

125125
private:
126126

127127
unsigned char* mpBuf;
128-
int mnBinaryLen;
129-
int mnBufferLen;
130-
int mnEncodedLen;
128+
size_t mnBinaryLen;
129+
size_t mnBufferLen;
130+
size_t mnEncodedLen;
131131
bool mbEncoded;
132132
};
133133

@@ -175,8 +175,8 @@ class CppSQLite3Query
175175
const char* getStringField(int nField, const char* szNullValue="") const;
176176
const char* getStringField(const char* szField, const char* szNullValue="") const;
177177

178-
const unsigned char* getBlobField(int nField, int& nLen) const;
179-
const unsigned char* getBlobField(const char* szField, int& nLen) const;
178+
const unsigned char* getBlobField(int nField, size_t& nLen) const;
179+
const unsigned char* getBlobField(const char* szField, size_t& nLen) const;
180180

181181
bool fieldIsNull(int nField) const;
182182
bool fieldIsNull(const char* szField) const;
@@ -274,7 +274,7 @@ class CppSQLite3Statement
274274
void bind(int nParam, const int nValue);
275275
void bind(int nParam, const long long nValue);
276276
void bind(int nParam, const double dwValue);
277-
void bind(int nParam, const unsigned char* blobValue, int nLen);
277+
void bind(int nParam, const unsigned char* blobValue, size_t nLen);
278278
void bindNull(int nParam);
279279

280280
void reset();

0 commit comments

Comments
 (0)