]> sigrok.org Git - pulseview.git/commitdiff
LogicSegment::pow2_ceil(): Fix potentіal integer overflow.
authorUwe Hermann <redacted>
Mon, 21 May 2018 14:30:57 +0000 (16:30 +0200)
committerUwe Hermann <redacted>
Mon, 21 May 2018 14:30:57 +0000 (16:30 +0200)
Reported by Coverity (CID 50925):

  Unintentional integer overflow (OVERFLOW_BEFORE_WIDEN)
    overflow_before_widen: Potentially overflowing expression 1 << power
    with type int (32 bits, signed) is evaluated using 32-bit arithmetic,
    and then used in a context that expects an expression of type
    uint64_t const (64 bits, unsigned).

pv/data/logicsegment.cpp

index 707f797d294accad64b5ee12f1ecd1551049344d..5d6faf8f8fe1a5333f8130cfcb17725db99a07ab 100644 (file)
@@ -25,6 +25,7 @@
 #include <cmath>
 #include <cstdlib>
 #include <cstring>
+#include <cstdint>
 
 #include "logic.hpp"
 #include "logicsegment.hpp"
@@ -480,7 +481,7 @@ uint64_t LogicSegment::get_subsample(int level, uint64_t offset) const
 
 uint64_t LogicSegment::pow2_ceil(uint64_t x, unsigned int power)
 {
-       const uint64_t p = 1 << power;
+       const uint64_t p = UINT64_C(1) << power;
        return (x + p - 1) / p * p;
 }