1
0
forked from cheng/wallet

fixed those irritating uninitialized memory warnings by annotating parameters

This commit is contained in:
Cheng 2023-09-23 15:58:56 +10:00
parent 1b0d5148e8
commit 89ebcee054
No known key found for this signature in database
GPG Key ID: 571C3A9C3B9E6FCA
4 changed files with 38 additions and 25 deletions

View File

@ -71,6 +71,7 @@
<CompileAsManaged>false</CompileAsManaged> <CompileAsManaged>false</CompileAsManaged>
<CompileAsWinRT>false</CompileAsWinRT> <CompileAsWinRT>false</CompileAsWinRT>
<DebugInformationFormat>ProgramDatabase</DebugInformationFormat> <DebugInformationFormat>ProgramDatabase</DebugInformationFormat>
<TreatWarningAsError>true</TreatWarningAsError>
</ClCompile> </ClCompile>
<Link> <Link>
<SubSystem>Windows</SubSystem> <SubSystem>Windows</SubSystem>
@ -108,6 +109,7 @@
<AdditionalOptions>/Zc:__cplusplus /utf-8 %(AdditionalOptions)</AdditionalOptions> <AdditionalOptions>/Zc:__cplusplus /utf-8 %(AdditionalOptions)</AdditionalOptions>
<CompileAsManaged>false</CompileAsManaged> <CompileAsManaged>false</CompileAsManaged>
<CompileAsWinRT>false</CompileAsWinRT> <CompileAsWinRT>false</CompileAsWinRT>
<TreatWarningAsError>true</TreatWarningAsError>
</ClCompile> </ClCompile>
<Link> <Link>
<SubSystem>Windows</SubSystem> <SubSystem>Windows</SubSystem>

View File

@ -431,7 +431,9 @@ namespace ristretto255 {
// It always a wise precaution to not use old type C arays, or wrap them // It always a wise precaution to not use old type C arays, or wrap them
// in a span. // in a span.
// Old type zero terminated strings work. The trailing zero is included // Old type zero terminated strings work. The trailing zero is included
// in the hash // in the hash, thus hash("the quick ", "brown fox") != hash("the quick brown fox")
template<unsigned int hashsize = 256> class hsh { template<unsigned int hashsize = 256> class hsh {
public: public:
static_assert(hashsize > 63 && hashsize % 64 == 0 && crypto_generichash_BYTES_MIN * 8 <= hashsize && hashsize <= crypto_generichash_BYTES_MAX * 8, "Bad hash size."); static_assert(hashsize > 63 && hashsize % 64 == 0 && crypto_generichash_BYTES_MIN * 8 <= hashsize && hashsize <= crypto_generichash_BYTES_MAX * 8, "Bad hash size.");
@ -504,6 +506,7 @@ namespace ristretto255 {
} }
} }
}; };
static_assert(!ro::is_serializable<hsh<256> >::value, "Don't want to partially hash partial hashes");
// This constructs a finalized hash. // This constructs a finalized hash.
// If it has one argument, and that argument is hsh (unfinalized hash) object, // If it has one argument, and that argument is hsh (unfinalized hash) object,
@ -550,7 +553,6 @@ namespace ristretto255 {
assert(i == 0); assert(i == 0);
if (i) throw HashReuseException(); if (i) throw HashReuseException();
} }
static_assert(!ro::is_serializable<hsh<hashsize> >::value, "Don't want to partially hash partial hashes");
template<typename T, typename... Args, template<typename T, typename... Args,
typename std::enable_if< ro::is_serializable<const T, Args...>::value, int >::type dummy_arg = 0 typename std::enable_if< ro::is_serializable<const T, Args...>::value, int >::type dummy_arg = 0
>explicit hash(const T& first, Args... args) { >explicit hash(const T& first, Args... args) {
@ -616,7 +618,7 @@ namespace ristretto255 {
} }
point operator+(const point &pt) const& { point operator+(const point &pt) const& {
point me; point me;
auto i{ crypto_core_ristretto255_add(&me.blob[0], &blob[0], &pt.blob[0]) }; auto i{ crypto_core_ristretto255_add(_Out_ & me.blob[0], &blob[0], &pt.blob[0])};
assert(i == 0); assert(i == 0);
if (i != 0)throw NonRandomScalarException(); if (i != 0)throw NonRandomScalarException();
return me; return me;
@ -624,7 +626,7 @@ namespace ristretto255 {
point operator-(const point& pt) const& { point operator-(const point& pt) const& {
point me; point me;
auto i{ crypto_core_ristretto255_sub(&me.blob[0], &blob[0], &pt.blob[0]) }; auto i{ crypto_core_ristretto255_sub(_Out_ &me.blob[0], &blob[0], &pt.blob[0]) };
assert(i == 0); assert(i == 0);
if (i != 0)throw NonRandomScalarException(); if (i != 0)throw NonRandomScalarException();
return me; return me;
@ -646,7 +648,7 @@ namespace ristretto255 {
static point random(void) { static point random(void) {
point me; point me;
crypto_core_ristretto255_random(&(me.blob[0])); crypto_core_ristretto255_random(_Out_ &(me.blob[0]));
return me; return me;
} }
@ -699,14 +701,14 @@ namespace ristretto255 {
// } // }
scalar operator+(const scalar sclr) const& { scalar operator+(const scalar sclr) const& {
scalar me; scalar me;
crypto_core_ristretto255_scalar_add(&me.blob[0], &blob[0], &sclr.blob[0]); crypto_core_ristretto255_scalar_add( _Out_&me.blob[0], &blob[0], &sclr.blob[0]);
return me; return me;
} }
static_assert(sizeof(scalar::blob) == 32, "compiled"); static_assert(sizeof(scalar::blob) == 32, "compiled");
scalar multiplicative_inverse() const &{ scalar multiplicative_inverse() const &{
scalar me; scalar me;
auto i = crypto_core_ristretto255_scalar_invert(&me.blob[0], &blob[0]); auto i = crypto_core_ristretto255_scalar_invert(_Out_&me.blob[0], &blob[0]);
assert(i == 0); assert(i == 0);
if (i != 0)throw NonRandomScalarException(); if (i != 0)throw NonRandomScalarException();
return me; return me;
@ -714,13 +716,13 @@ namespace ristretto255 {
scalar operator-(const scalar& sclr) const& { scalar operator-(const scalar& sclr) const& {
scalar me; scalar me;
crypto_core_ristretto255_scalar_sub(&me.blob[0], &blob[0], &sclr.blob[0]); crypto_core_ristretto255_scalar_sub(_Out_&me.blob[0], &blob[0], &sclr.blob[0]);
return me; return me;
} }
scalar operator*(const scalar& sclr) const& { scalar operator*(const scalar& sclr) const& {
scalar me; scalar me;
crypto_core_ristretto255_scalar_mul(&me.blob[0], &blob[0], &sclr.blob[0]); crypto_core_ristretto255_scalar_mul(_Out_&me.blob[0], &blob[0], &sclr.blob[0]);
return me; return me;
} }
@ -734,7 +736,7 @@ namespace ristretto255 {
point operator*(const point& pt) const& { point operator*(const point& pt) const& {
point me; point me;
auto i{ crypto_scalarmult_ristretto255(&me.blob[0], &blob[0], &pt.blob[0]) }; auto i{ crypto_scalarmult_ristretto255(_Out_&me.blob[0], &blob[0], &pt.blob[0]) };
assert(i == 0); assert(i == 0);
if (i != 0)throw NonRandomScalarException(); if (i != 0)throw NonRandomScalarException();
return me; return me;
@ -742,7 +744,7 @@ namespace ristretto255 {
point timesBase() const& { point timesBase() const& {
point me; point me;
auto i{ crypto_scalarmult_ristretto255_base(&me.blob[0], &blob[0]) }; auto i{ crypto_scalarmult_ristretto255_base(_Out_ & me.blob[0], &blob[0]) };
assert(i == 0); assert(i == 0);
if (i != 0)throw NonRandomScalarException(); if (i != 0)throw NonRandomScalarException();
return me; return me;
@ -755,7 +757,7 @@ namespace ristretto255 {
static scalar random(void) { static scalar random(void) {
scalar me; scalar me;
crypto_core_ristretto255_scalar_random(&me.blob[0]); crypto_core_ristretto255_scalar_random(_Out_ & me.blob[0]);
return me; return me;
} }

View File

@ -28,6 +28,7 @@ namespace testbed {
using ristretto255::hash, ristretto255::hsh, ristretto255::scalar, using ristretto255::hash, ristretto255::hsh, ristretto255::scalar,
ristretto255::point, ro::serialize, ro::bin2hex, ro::hex2bin, ristretto255::point, ro::serialize, ro::bin2hex, ro::hex2bin,
ro::bin2hex, ro::fasthash,ro::CompileSizedString ; ro::bin2hex, ro::fasthash,ro::CompileSizedString ;
static constexpr char SrcFilename[]{ "src/testbed.cpp" };
/* experimental code called during unit test /* experimental code called during unit test
Anything here is a residue of forgotten experiments, Anything here is a residue of forgotten experiments,
@ -36,21 +37,29 @@ namespace testbed {
This is a playground, where you can do stuff without worrying you might This is a playground, where you can do stuff without worrying you might
inadvertently break something that matters inadvertently break something that matters
No mechanism for input is available. You generally do not need it because you Output goes to the unit test log.
hard code the testing data, and detect errors with asserts, rather than exceptions
but, of course, it can post a dialog using postmessage, then immediately return
and the dialog can then call anything.
Uncaught exceptions result in unit test failure, but not in an error No mechanism for input is available. You generally do not need it
message in the main program UI. because you hard code the testing data, and detect errors with
asserts, rather than exceptions but, of course, it can post a
dialog using postmessage, and the dialog can then call anything.
If using a dialog, exceptions within the dialog will result in an error message in the Uncaught exceptions result in unit test failure, and an error message
main program UI, rather than in the unit test result, since the unit test at the end of the unit test, but not in an error message in the
is over before the dialog runs. main program UI.
If using postmessage, execution of the code waits for the dialog to
return, data from the dialog can be used in the testbed code, and
uncaught exceptions in the dialog will result unit test failure
and be reported in the unit test log.
If using queumessage, the testbed code will complete while the dialog
is waiting, data cannot be returned for use in the testbed code,
and uncaught exceptions will appear in the main UI.
*/ */
void testbed() { void testbed() {
// queue_error_message("hello world"); // queue_error_message("hello world");
ascii2test(); // throw MyException("hello world exception", __LINE__, __func__, SrcFilename);
} }
} }

View File

@ -63,7 +63,7 @@ static bool EndUnitTest() {
unit_test_action = &noaction; unit_test_action = &noaction;
modal_dialog_hook.Unregister(); modal_dialog_hook.Unregister();
next_action = &unexpected_unit_test; next_action = &unexpected_unit_test;
static std::string intestbed("Testbed: "); std::string intestbed("Testbed: ");
try { try {
testbed::testbed(); testbed::testbed();
throw EndUnitTestOfExceptions(); throw EndUnitTestOfExceptions();
@ -71,7 +71,7 @@ static bool EndUnitTest() {
catch (const EndUnitTestOfExceptions&) {} catch (const EndUnitTestOfExceptions&) {}
catch (const MyException& e) { catch (const MyException& e) {
errorCode = e.what_num(); errorCode = e.what_num();
szError = e.what(); szError = intestbed + e.what();
ILogError(szError.c_str()); ILogError(szError.c_str());
} }
catch (const std::exception& e) { catch (const std::exception& e) {