Skip to content

Remove the call to 'abs' since unsigned values cannot be negative #55

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

juergenhoetzel
Copy link

The C11 standard states that for unsigned integers modulo wrapping is
the defined behavior and the term overflow never applies: "a
computation involving unsigned operands can never overflow."

Fixes warnings (clang):

ibbmp.c
../../home/juergen/shared/low-level-programming/sepia_filter/libbmp/libbmp.c:164:27: warning: taking the absolute value of unsigned type 'unsigned long' has no effect [-Wabsolute-value]
                fwrite (img->img_pixels[abs (offset - y)], sizeof (bmp_pixel), img->img_header.biWidth, img_file);
                                        ^
../../home/juergen/shared/low-level-programming/sepia_filter/libbmp/libbmp.c:164:27: note: remove the call to 'abs' since unsigned values cannot be negative
                fwrite (img->img_pixels[abs (offset - y)], sizeof (bmp_pixel), img->img_header.biWidth, img_file);
                                        ^~~
../../home/juergen/shared/low-level-programming/sepia_filter/libbmp/libbmp.c:210:30: warning: taking the absolute value of unsigned type 'unsigned long' has no effect [-Wabsolute-value]
                if (fread (img->img_pixels[abs (offset - y)], sizeof (bmp_pixel), items, img_file) != items)
                                           ^
../../home/juergen/shared/low-level-programming/sepia_filter/libbmp/libbmp.c:210:30: note: remove the call to 'abs' since unsigned values cannot be negative
                if (fread (img->img_pixels[abs (offset - y)], sizeof (bmp_pixel), items, img_file) != items)
                                           ^~~
2 warnings generated.

The C11 standard states that for unsigned integers modulo wrapping is
the defined behavior and the term overflow never applies: "a
computation involving unsigned operands can never overflow."
@@ -207,7 +207,7 @@ bmp_img_read (bmp_img *img,
for (size_t y = 0; y < h; y++)
{
// Read a whole row of pixels from the file:
if (fread (img->img_pixels[abs (offset - y)], sizeof (bmp_pixel), items, img_file) != items)
if (fread (img->img_pixels[offset - y], sizeof (bmp_pixel), items, img_file) != items)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't functionally correct. If offset = 0 then the value wraps around starting from the max value of size_t

Copy link

@0xBERNDOG 0xBERNDOG left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not functionally correct, see #62

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants