Skip to content

Commit 5fefc4b

Browse files
committedMar 21, 2014
Fix serializing of signed numbers in serializeStructToString
1 parent 0dc1aec commit 5fefc4b

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed
 

Diff for: ‎src/util/serialize.cpp

+8-3
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,9 @@ bool deSerializeStringToStruct(std::string valstr,
385385
}
386386

387387

388+
// Casts *buf to a signed or unsigned fixed-width integer of 'w' width
389+
#define SIGN_CAST(w, buf) (is_unsigned ? *((u##w *) buf) : *((s##w *) buf))
390+
388391
bool serializeStructToString(std::string *out,
389392
std::string format, void *value)
390393
{
@@ -412,15 +415,15 @@ bool serializeStructToString(std::string *out,
412415
case 'i':
413416
if (width == 16) {
414417
bufpos += PADDING(bufpos, u16);
415-
os << *((u16 *) bufpos);
418+
os << SIGN_CAST(16, bufpos);
416419
bufpos += sizeof(u16);
417420
} else if (width == 32) {
418421
bufpos += PADDING(bufpos, u32);
419-
os << *((u32 *) bufpos);
422+
os << SIGN_CAST(32, bufpos);
420423
bufpos += sizeof(u32);
421424
} else if (width == 64) {
422425
bufpos += PADDING(bufpos, u64);
423-
os << *((u64 *) bufpos);
426+
os << SIGN_CAST(64, bufpos);
424427
bufpos += sizeof(u64);
425428
}
426429
break;
@@ -474,3 +477,5 @@ bool serializeStructToString(std::string *out,
474477

475478
return true;
476479
}
480+
481+
#undef SIGN_CAST

0 commit comments

Comments
 (0)