Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stop using C++ auto to assign results of arithmetic operations #1386

Merged
merged 2 commits into from Apr 30, 2024

Conversation

ibc
Copy link
Member

@ibc ibc commented Apr 30, 2024

Details

  • Fixes FeedbackRtpTransport.cpp may have an overflow mistake #1385
  • We MUST NOT use auto to assign the result of var1 - var2. Despite what the type of var1 and var2 are, the compiler may choose a different type for the resulting vartiable. See below for a real example.
  • I've also changed some other auto usages (those related to RTC::SeqManager<XXX>::IsSeqXxxxThan()). But probably there are more.
  • My opinion is that we should STOP using auto.
  • Also, using uint16_t result = var1 - var2 looks way better than auto result = static_cast<uint16_t>(var1 -var2), espite the latter makes sense sometimes when casting in operands is needed.

Real Example

#include <cinttypes>  // PRIu64, etc
#include <cstddef>    // size_t
#include <cstdint>    // uint8_t, etc
#include <cstdio>

int main() {

    uint16_t sequenceNumber = 0;
    uint16_t latestSequenceNumber = 65535;

    auto missingPackets = sequenceNumber - (latestSequenceNumber + 1);

    printf("missingPackets: %" PRIu16 "\n", missingPackets);

    return 0;
}
missingPackets: 4294901760
#include <cinttypes>  // PRIu64, etc
#include <cstddef>    // size_t
#include <cstdint>    // uint8_t, etc
#include <cstdio>

int main() {

    uint16_t sequenceNumber = 0;
    uint16_t latestSequenceNumber = 65535;

    uint16_t missingPackets = sequenceNumber - (latestSequenceNumber + 1);

    printf("missingPackets: %" PRIu16 "\n", missingPackets);

    return 0;
}
missingPackets: 0

### Details

- Fixes #1385
- We MUST NOT use `auto` to assign the result of `var1` - `var2`. Despite what the type of `var1` and `var2` are, the compiler may choose a different type for the resulting vartiable. See below for a real example.
- I've also changed some other `auto` usages (those related to `RTC::SeqManager<XXX>::IsSeqXxxxThan()`). But probably there are more.
- My opinion is that we should STOP using `auto`.
- Also, using `uint16_t result = var1 - var2` looks way better than `auto result = static_cast<uint16_t>(var1 -var2)`, espite the latter makes sense sometimes when casting in operands is needed.

### Real Example

```c++
#include <cinttypes>  // PRIu64, etc
#include <cstddef>    // size_t
#include <cstdint>    // uint8_t, etc
#include <cstdio>

int main() {

    uint16_t sequenceNumber = 0;
    uint16_t latestSequenceNumber = 65535;

    auto missingPackets = sequenceNumber - (latestSequenceNumber + 1);

    printf("missingPackets: %" PRIu16 "\n", missingPackets);

    return 0;
}
```

```
missingPackets: 4294901760
```

```c++
#include <cinttypes>  // PRIu64, etc
#include <cstddef>    // size_t
#include <cstdint>    // uint8_t, etc
#include <cstdio>

int main() {

    uint16_t sequenceNumber = 0;
    uint16_t latestSequenceNumber = 65535;

    uint16_t missingPackets = sequenceNumber - (latestSequenceNumber + 1);

    printf("missingPackets: %" PRIu16 "\n", missingPackets);

    return 0;
}
```

```
missingPackets: 0
```
@ibc ibc requested a review from jmillan April 30, 2024 11:12
@ibc ibc merged commit 0311073 into v3 Apr 30, 2024
35 checks passed
@ibc ibc deleted the fix-wrong-cpp-auto-usage-issue-1385 branch April 30, 2024 12:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

FeedbackRtpTransport.cpp may have an overflow mistake
2 participants