Skip to content

Integer overflow in numSamplesSqr calculation in ecmProcessSet #45

@FredM67

Description

@FredM67

Description

In src/emon_CM.c at line 789, the calculation of numSamplesSqr can overflow:

const int32_t numSamples    = accumProcessing->numSamples;
const int64_t numSamplesSqr = numSamples * numSamples;

The multiplication numSamples * numSamples is performed as int32_t * int32_t, yielding an int32_t result before being assigned to the int64_t variable. If numSamples exceeds ~46340, the multiplication overflows.

Expected behavior

The multiplication should be performed in 64-bit to prevent overflow:

const int64_t numSamplesSqr = (int64_t)numSamples * numSamples;

Or using unsigned types:

const uint32_t numSamples    = accumProcessing->numSamples;
const uint64_t numSamplesSqr = (uint64_t)numSamples * numSamples;

Fix

This issue will be fixed along with PR #40.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions