RTEMS | cpukit/dev/can/can-bittiming.c: Do not use abs() on unsigned values (!726)

Michal Lenc (@michallenc) gitlab at rtems.org
Fri Sep 19 21:37:19 UTC 2025




Michal Lenc commented: https://gitlab.rtems.org/rtems/rtos/rtems/-/merge_requests/726#note_132779


Hello, removing `abs` without any other change is not correct imo, this would automatically discard possible bittimings with `sample point > sample_point_nominal` as the subtraction' result is negative, thus largest error in unsigned arithmetic.

Calling `abs` leads to implicit conversion to signed value I think, so the original code is technically correct. For example the following code prints `0x2` instead of `0xff..fe` it would without `abs`.

```c
  unsigned int a = 0x7;
  unsigned int b = 0x9;

  unsigned int c = abs(a - b);
  printf("0x%x\n", c);
```

It's true `-Wabsolute-value` leads to a warning, interestingly it only detects `unsigned int`. If you replace it with `uint8_t`, it passes.

I think we can either make subtraction operands signed, or just remove `abs` and have something like

```c
sample_point_error = sample_point_nominal > sample_point ?
                     sample_point_nominal - sample_point :
                     sample_point - sample_point_nominal;
```

-- 
View it on GitLab: https://gitlab.rtems.org/rtems/rtos/rtems/-/merge_requests/726#note_132779
You're receiving this email because of your account on gitlab.rtems.org.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.rtems.org/pipermail/bugs/attachments/20250919/80e82723/attachment-0001.htm>


More information about the bugs mailing list