Skip to content

Commit 5552917

Browse files
committed
enable signed conversion warning
1 parent d44e564 commit 5552917

25 files changed

+158
-97
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ set( CMAKE_INCLUDE_CURRENT_DIR ON )
1515
include_directories( program/src/ )
1616

1717
# gcc compiler/linker flags
18-
add_compile_options( -ggdb -Wall -Wextra -Werror -m64 )
18+
add_compile_options( -ggdb -Wall -Wextra -Wsign-conversion -Werror -m64 )
1919
set( CMAKE_CXX_FLAGS -std=c++11 )
2020
set( CMAKE_C_FLAGS -std=c99 )
2121
set( CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -pthread")

pc/bincode.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ namespace pc
100100

101101
inline void bincode::add( uint8_t val )
102102
{
103-
buf_[idx_++] = val;
103+
add_val_T( val );
104104
}
105105

106106
template<class T>

pc/capture.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ void capture::run()
155155
int num = ::gzwrite( zfd_, buf, sz );
156156
if ( num > 0 ) {
157157
buf += num;
158-
sz -= num;
158+
sz -= static_cast< unsigned >( num );
159159
} else {
160160
break;
161161
}

pc/key_pair.cpp

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
#include "misc.hpp"
55
#include <openssl/evp.h>
66

7+
#include <assert.h>
8+
79
#define PC_SYMBOL_SPACES 0x2020202020202020
810

911
using namespace pc;
@@ -75,20 +77,21 @@ void hash::init_from_buf( const uint8_t *pk )
7577
__builtin_memcpy( pk_, pk, len );
7678
}
7779

78-
int hash::enc_base58( uint8_t *buf, uint32_t buflen ) const
80+
int hash::enc_base58( char *buf, int buflen ) const
7981
{
8082
return pc::enc_base58( pk_, len, buf, buflen );
8183
}
8284

8385
int hash::enc_base58( std::string& res ) const
8486
{
8587
char buf[64];
86-
int n = enc_base58( (uint8_t*)buf, 64 );
87-
res.assign( buf, n );
88+
int n = enc_base58( buf, 64 );
89+
assert( n >= 0 );
90+
res.assign( buf, static_cast< unsigned >( n ) );
8891
return n;
8992
}
9093

91-
int hash::dec_base58( const uint8_t *buf, uint32_t buflen )
94+
int hash::dec_base58( const uint8_t *buf, int buflen )
9295
{
9396
return pc::dec_base58( buf, buflen, pk_ );
9497
}
@@ -197,16 +200,17 @@ bool signature::init_from_text( const std::string& buf )
197200
return true;
198201
}
199202

200-
int signature::enc_base58( uint8_t *buf, uint32_t buflen )
203+
int signature::enc_base58( char *buf, int buflen )
201204
{
202205
return pc::enc_base58( sig_, len, buf, buflen );
203206
}
204207

205208
int signature::enc_base58( std::string& res )
206209
{
207210
char buf[256];
208-
int n = enc_base58( (uint8_t*)buf, 256 );
209-
res.assign( buf, n );
211+
int n = enc_base58( buf, 256 );
212+
assert( n >= 0 );
213+
res.assign( buf, static_cast< unsigned >( n ) );
210214
return n;
211215
}
212216

pc/key_pair.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ namespace pc
2525
void init_from_buf( const uint8_t * );
2626

2727
// encode to text buffer
28-
int enc_base58( uint8_t *buf, uint32_t buflen ) const;
28+
int enc_base58( char *buf, int buflen ) const;
2929
int enc_base58( std::string& ) const;
30-
int dec_base58( const uint8_t *buf, uint32_t buflen );
30+
int dec_base58( const uint8_t *buf, int buflen );
3131

3232
// get underlying bytes
3333
const uint8_t *data() const;
@@ -96,7 +96,7 @@ namespace pc
9696
bool init_from_text( const std::string& buf );
9797

9898
// encode to text buffer
99-
int enc_base58( uint8_t *buf, uint32_t buflen );
99+
int enc_base58( char *buf, int buflen );
100100
int enc_base58( std::string& );
101101

102102
// sign message given key_pair

pc/key_store.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ static bool write_key_file(
2727
hd->dealloc();
2828
return false;
2929
}
30-
len -= num;
30+
len -= static_cast< unsigned >( num );
3131
buf += num;
3232
} while( len );
3333
fchmod( fd, 0400 );

pc/manager.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -498,8 +498,10 @@ void manager::poll_schedule()
498498
{
499499
while ( is_pub_ && kidx_ < kvec_.size() ) {
500500
price_sched *kptr = kvec_[kidx_];
501-
int64_t pub_ts = pub_ts_ + ( pub_int_ * kptr->get_hash() ) /
502-
price_sched::fraction;
501+
int64_t pub_ts = pub_ts_ + static_cast< int64_t >(
502+
( static_cast< uint64_t >( pub_int_ ) * kptr->get_hash() )
503+
/ price_sched::fraction
504+
);
503505
if ( curr_ts_ > pub_ts ) {
504506
kptr->schedule();
505507
if ( ++kidx_ >= kvec_.size() ) {

pc/mem_map.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
#include "mem_map.hpp"
2+
3+
#include <assert.h>
24
#include <sys/types.h>
35
#include <sys/mman.h>
46
#include <sys/stat.h>
@@ -47,7 +49,8 @@ bool mem_map::remap()
4749
if ( 0 != fstat( fd_, fst ) || fst->st_size == 0 ) {
4850
return false;
4951
}
50-
size_t nlen = fst->st_size;
52+
assert( fst->st_size >= 0 );
53+
size_t nlen = static_cast< size_t >( fst->st_size );
5154
if ( nlen == len_ ) {
5255
return false;
5356
}
@@ -72,7 +75,8 @@ bool mem_map::init()
7275
::close( fd );
7376
return false;
7477
}
75-
len_ = fst->st_size;
78+
assert( fst->st_size >= 0 );
79+
len_ = static_cast< size_t >( fst->st_size );
7680
void *buf = mmap( NULL, len_, PROT_READ, MAP_SHARED, fd, 0 );
7781
if ( buf == MAP_FAILED ) {
7882
return false;

pc/misc.cpp

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
#include "misc.hpp"
2+
3+
#include <assert.h>
24
#include <ctype.h>
35
#include <time.h>
46

@@ -29,18 +31,24 @@ const char ALPHABET_MAP[256] = {
2931
const double iFactor = 1.36565823730976103695740418120764243208481439700722980119458355862779176747360903943915516885072037696111192757109;
3032

3133
// reslen is the allocated length for result, feel free to overallocate
32-
int enc_base58(const uint8_t *source, int len, uint8_t result[], int reslen) {
34+
int enc_base58(const uint8_t *source, int len, char result[], int reslen)
35+
{
36+
assert( source );
37+
assert( len >= 0 );
38+
assert( reslen >= 0 );
39+
3340
int zeros = 0, length = 0, pbegin = 0, pend;
3441
if (!(pend = len)) return 0;
3542
while (pbegin != pend && !source[pbegin]) pbegin = ++zeros;
3643
int size = 1 + iFactor * (double)(pend - pbegin);
37-
uint8_t b58[size];
44+
assert( size > 0 );
45+
uint8_t b58[ static_cast< unsigned >( size ) ];
3846
for (int i = 0; i < size; i++) b58[i] = 0;
3947
while (pbegin != pend) {
4048
uint32_t carry = source[pbegin];
4149
int i = 0;
4250
for (int it1 = size - 1; (carry || i < length) && (it1 != -1); it1--,i++) {
43-
carry += 256 * b58[it1];
51+
carry += 256U * b58[it1];
4452
b58[it1] = carry % 58;
4553
carry /= 58;
4654
}
@@ -61,6 +69,10 @@ int enc_base58(const uint8_t *source, int len, uint8_t result[], int reslen) {
6169
// result must be declared (for the worst case): char result[len * 2];
6270
int dec_base58( const uint8_t *str, int len, uint8_t *result)
6371
{
72+
assert( str );
73+
assert( len >= 0 );
74+
assert( result );
75+
6476
result[0] = 0;
6577
int resultlen = 1;
6678
for (int i = 0; i < len; i++) {
@@ -105,15 +117,15 @@ char *uint_to_str( uint64_t val, char *cptr )
105117
return cptr;
106118
}
107119

108-
uint64_t str_to_uint( const char *val, int len )
120+
uint64_t str_to_uint( const char *val, const unsigned len )
109121
{
110122
uint64_t res = 0L;
111123
if ( len ) {
112124
const char *cptr = val;
113125
const char *end = &val[len];
114126
for(; cptr != end; ++cptr ) {
115127
if ( isdigit( *cptr ) ) {
116-
res = res*10UL + (*cptr-'0');
128+
res = res*10UL + static_cast< unsigned >(*cptr-'0');
117129
} else {
118130
res = 0L;
119131
break;
@@ -143,7 +155,7 @@ char *int_to_str( int64_t val, char *cptr )
143155
return cptr;
144156
}
145157

146-
int64_t str_to_int( const char *val, int len )
158+
int64_t str_to_int( const char *val, const unsigned len )
147159
{
148160
bool is_neg = false;
149161
int64_t res = 0L;
@@ -152,7 +164,7 @@ int64_t str_to_int( const char *val, int len )
152164
const char *end = &val[len];
153165
for(; cptr != end; ++cptr ) {
154166
if ( isdigit( *cptr ) ) {
155-
res = res*10UL + (*cptr-'0');
167+
res = res*10L + (*cptr-'0');
156168
} else if ( *cptr == '-' ) {
157169
is_neg = true;
158170
} else {
@@ -181,14 +193,14 @@ inline void a3_to_a4(uint8_t* a4, uint8_t* a3)
181193
a4[3] = (a3[2] & 0x3f);
182194
}
183195

184-
inline void a4_to_a3( uint8_t* a3, uint8_t* a4)
196+
inline void a4_to_a3( uint8_t* a3, const uint8_t* a4)
185197
{
186198
a3[0] = (a4[0] << 2) + ((a4[1] & 0x30) >> 4);
187199
a3[1] = ((a4[1] & 0xf) << 4) + ((a4[2] & 0x3c) >> 2);
188200
a3[2] = ((a4[2] & 0x3) << 6) + a4[3];
189201
}
190202

191-
inline uint8_t b64_lookup(char c)
203+
inline char b64_lookup(char c)
192204
{
193205
if(c >='A' && c <='Z') return c - 'A';
194206
if(c >='a' && c <='z') return c - 71;
@@ -198,14 +210,15 @@ inline uint8_t b64_lookup(char c)
198210
return -1;
199211
}
200212

201-
int enc_base64_len( int n )
213+
size_t enc_base64_len( const size_t n )
202214
{
203215
return (n + 2 - ((n + 2) % 3)) / 3 * 4;
204216
}
205217

206-
int enc_base64( const uint8_t *inp, int len, uint8_t *out )
218+
size_t enc_base64( const uint8_t *inp, int len, char *out )
207219
{
208-
int i = 0, j = 0, encLen = 0;
220+
int i = 0, j = 0;
221+
size_t encLen = 0;
209222
uint8_t a3[3];
210223
uint8_t a4[4];
211224

@@ -236,22 +249,23 @@ int enc_base64( const uint8_t *inp, int len, uint8_t *out )
236249
return encLen;
237250
}
238251

239-
int dec_base64( const uint8_t *inp, int len, uint8_t *out )
252+
size_t dec_base64( const char *inp, int len, uint8_t *out )
240253
{
241-
int i = 0, j = 0, decLen = 0;
254+
int i = 0, j = 0;
255+
size_t decLen = 0;
242256
uint8_t a3[3] = { 0,0,0 };
243-
uint8_t a4[4];
257+
char a4[4];
244258

245259
while ( len-- ) {
246260
if(*inp == '=') {
247261
break;
248262
}
249263
a4[i++] = *(inp++);
250264
if (i == 4) {
251-
for (i = 0; i <4; i++) {
265+
for (i = 0; i < 4; i++) {
252266
a4[i] = b64_lookup(a4[i]);
253267
}
254-
a4_to_a3(a3,a4);
268+
a4_to_a3( a3, reinterpret_cast< uint8_t* >( a4 ) );
255269
for (i = 0; i < 3; i++) {
256270
out[decLen++] = a3[i];
257271
}
@@ -266,7 +280,7 @@ int dec_base64( const uint8_t *inp, int len, uint8_t *out )
266280
for (j = 0; j <4; j++) {
267281
a4[j] = b64_lookup(a4[j]);
268282
}
269-
a4_to_a3(a3,a4);
283+
a4_to_a3( a3, reinterpret_cast< uint8_t* >( a4 ) );
270284
for (j = 0; j < i - 1; j++) {
271285
out[decLen++] = a3[j];
272286
}
@@ -279,7 +293,7 @@ int64_t get_now()
279293
struct timespec ts[1];
280294
clock_gettime( CLOCK_REALTIME, ts );
281295
int64_t res = ts->tv_sec;
282-
res *= 1000000000UL;
296+
res *= 1000000000L;
283297
res += ts->tv_nsec;
284298
return res;
285299
}

pc/misc.hpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,20 @@ namespace pc
1313
{
1414

1515
// base58 encoding from base-x conversion impl.
16-
int enc_base58( const uint8_t *src, int len, uint8_t *result, int rlen);
16+
int enc_base58( const uint8_t *src, int len, char *result, int rlen);
1717
int dec_base58( const uint8_t *str, int len, uint8_t *result );
1818

1919
// base64 encoding courtesy of
2020
// Adam Rudd per licence: github.com/adamvr/arduino-base64
21-
int enc_base64_len( int len );
22-
int enc_base64( const uint8_t *src, int len, uint8_t *result );
23-
int dec_base64( const uint8_t *str, int len, uint8_t *result );
21+
size_t enc_base64_len( size_t len );
22+
size_t enc_base64( const uint8_t *src, int len, char *result );
23+
size_t dec_base64( const char *str, int len, uint8_t *result );
2424

2525
// integer to string encoding
2626
char *uint_to_str( uint64_t val, char *end_ptr );
27-
uint64_t str_to_uint( const char *str, int len );
27+
uint64_t str_to_uint( const char *str, unsigned len );
2828
char *int_to_str( int64_t val, char *end_ptr );
29-
int64_t str_to_int( const char *str, int len );
29+
int64_t str_to_int( const char *str, unsigned len );
3030

3131
// string representation of decimal to integer with implied decimal places
3232
int64_t str_to_dec( const char *str, int len, int expo );

0 commit comments

Comments
 (0)