[esnacc-dev] [PATCH] c++ examples: fix narrowing conversions

Uri Blumenthal uri at mit.edu
Tue Sep 6 19:57:21 UTC 2016


ACK - patch works. ;-)

> On Sep 6, 2016, at 14:06 , Aaron Conole <aconole at bytheb.org> wrote:
> 
> A number of test suites in esnacc have improperly narrowed conversions which
> trip more stringent checks.  Compilers known to trigger these warnings are
> clang and g++ 6.  This commit reduces the narrowing conversion errors by
> explicitly casting downward, and using wider fields for the initial bit
> storage.
> 
> Reported-by: Uri Blumenthal <uri at mit.edu>
> Fixes: https://github.com/esnacc/esnacc-ng/issues/35
> Signed-off-by: Aaron Conole <aconole at bytheb.org>
> ---
> NOTE: This patch will be backported to branch-1.8, to fix compilation issues
>      under newer versions of fedora.
> 
> cxx-examples/src/automatic.cpp | 6 +++---
> cxx-examples/src/bitstest.cpp  | 8 ++++----
> cxx-examples/src/inttest.cpp   | 6 +++---
> 3 files changed, 10 insertions(+), 10 deletions(-)
> 
> diff --git a/cxx-examples/src/automatic.cpp b/cxx-examples/src/automatic.cpp
> index 7c9b34b..b35f5aa 100644
> --- a/cxx-examples/src/automatic.cpp
> +++ b/cxx-examples/src/automatic.cpp
> @@ -22,7 +22,7 @@ getHuman(const char *name, int age, bool isBiblical,
> int automaticTests()
> {
>     struct Tests {
> -        const char bytes[256];
> +        const unsigned char bytes[256];
>         size_t byte_len;
>         const char *enc_bytes;
>         SNACC::Human *h;
> @@ -66,7 +66,7 @@ int automaticTests()
>         bool fail_name = false;
>         bool fail_age = false;
>         bool fail_firstwords = false;
> -        SNACC::AsnBuf b(t[i].bytes, t[i].byte_len);
> +        SNACC::AsnBuf b((const char *)(t[i].bytes), t[i].byte_len);
>         SNACC::AsnLen l;
>         SNACC::Human h;
>         try {
> @@ -103,7 +103,7 @@ int automaticTests()
>         }
> 
>         SNACC::AsnBuf benc;
> -        SNACC::AsnBuf expected(t[i].bytes, t[i].byte_len);
> +        SNACC::AsnBuf expected((const char *)(t[i].bytes), t[i].byte_len);
>         try {
>             h.BEnc(benc);
>             if (!(benc == expected)) {
> diff --git a/cxx-examples/src/bitstest.cpp b/cxx-examples/src/bitstest.cpp
> index 10792bd..a1f0a2a 100644
> --- a/cxx-examples/src/bitstest.cpp
> +++ b/cxx-examples/src/bitstest.cpp
> @@ -5,7 +5,7 @@ using namespace SNACC;
> typedef struct test_table
> {
>    char *input;
> -   char result[20];
> +   unsigned char result[20];
>    bool nblFlag;
> } AsnBitTestTable;
> 
> @@ -93,8 +93,8 @@ void bittests(void)
>    {
>       AsnBits asnBits(gBitTestTable[index].input);
>       asnBits.UseNamedBitListRules(gBitTestTable[index].nblFlag);
> -      AsnBuf expectedResult(&gBitTestTable[index].result[0], 
> -         DecTagLen((unsigned char *)gBitTestTable[index].result+1) + 2);
> +      AsnBuf expectedResult((const char *)&gBitTestTable[index].result[0], 
> +         DecTagLen(gBitTestTable[index].result+1) + 2);
>       AsnBuf result;
>       AsnLen bytesEncoded;
> 
> @@ -138,4 +138,4 @@ void bittests(void)
>       }
>    }
>    std::cout << "*** End of AsnBits tests ***\n";
> -}
> \ No newline at end of file
> +}
> diff --git a/cxx-examples/src/inttest.cpp b/cxx-examples/src/inttest.cpp
> index 8927cf8..4645c0e 100644
> --- a/cxx-examples/src/inttest.cpp
> +++ b/cxx-examples/src/inttest.cpp
> @@ -5,7 +5,7 @@ using namespace SNACC;
> typedef struct test_table
> {
>    char *input;
> -   char result[20];
> +   unsigned char result[20];
>    bool unsignedFlag;
> } AsnIntTestTable;
> 
> @@ -131,8 +131,8 @@ void inttests(void)
> 
>          AsnInt asnInt (gIntTestTable[testIndex].input,
>             gIntTestTable[testIndex].unsignedFlag);
> -         AsnBuf expectedResult(gIntTestTable[testIndex].result,
> -            DecTagLen((unsigned char *)gIntTestTable[testIndex].result + 1) + 2);
> +         AsnBuf expectedResult((const char *)gIntTestTable[testIndex].result,
> +            DecTagLen(gIntTestTable[testIndex].result + 1) + 2);
>          AsnBuf result;
> 
>          try
> -- 
> 2.7.4
> 

--
Uri the Great
uri at mit.edu







More information about the dev mailing list