Skip to content

Pointer arithmetic unsuitable to detect arithmetic overflow #316

Description

@ellerh

The mps_reserve macro uses the expression

(char *)(_mps_ap)->alloc + (_size) > (char *)(_mps_ap)->alloc

to detect if _size is so large that the addition overflows/wraps around. A similar expression is used in the BufferFill function:

AddrAdd(buffer->ap_s.alloc, size) < (Addr)buffer->ap_s.alloc

It seems that per the C standard, overflows for pointer arithmetic are undefined behavior. The attached file, when compiled with GCC 14, illustrates the problem.

It would probably be more portable to use unsigned arithmetic in this case.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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