Skip to content
The Chat Archive
gpt-4.1-2025-04-14· OpenAI LMArena Human Preference 140K

for the following C code: [code] the return line can be UB if the string were extremely…

  1. User

    for the following C code:

    size_t strlen(const char * str) {
      const char *s;
      for (s = str; *s; ++s) {}
      return s - str;
    }
    

    the return line can be UB if the string were extremely long (longer than PTRDIFF_MAX). would "return (uintptr_t )s - (uintptr_t )str;" solve this UB? Can we use this cast in any other pointer subtraction situation?

  2. Assistant

    Great question! Let's break it down:

    1. Why is s - str potentially UB?

    In C, pointer subtraction (s - str) yields a result of type ptrdiff_t, which is a signed integer type. The C standard (C99 §6.5.6/9) says:

    When two pointers are subtracted, both shall point to elements of the same array object, or one past the last element of the array object; the result is the difference of the subscripts of the two array elements.

    But, if the difference is greater than PTRDIFF_MAX, the result is undefined behavior (UB), because it cannot be represented in a ptrdiff_t.

    2. Would casting to uintptr_t solve this?

    Your suggestion:

    return (uintptr_t)s - (uintptr_t)str;
    

    This avoids UB from pointer subtraction overflow, because uintptr_t is an unsigned integer type capable of holding any pointer value. The subtraction is now done in the unsigned integer domain, so it will wrap around modulo UINTPTR_MAX + 1 if needed, which is well-defined.

    However, this is not a portable or standard-compliant way to compute the length of a string, because:

    • The C standard only guarantees that pointer subtraction is valid within the same array object (or one past the end).
    • Casting pointers to integers and subtracting them is not guaranteed to give the correct element count, especially if the pointers are not aligned or if the platform has "holes" in the address space.
    • The result is a byte difference, not an element count. For char *, this is fine, but for other types, it would be wrong.

    3. Can you use this cast in other pointer subtraction situations?

    No, not in general. This trick only works for char * (or unsigned char *, uint8_t *, etc.), where the size of the element is 1 byte. For other types, pointer subtraction gives you the number of elements, not the number of bytes. Casting to uintptr_t and subtracting gives you the number of bytes between the pointers, which is not the same as the number of elements for types larger than 1 byte.

    Example:

    int arr[100];
    int *a = &arr[10];
    int *b = &arr[0];
    ptrdiff_t diff = a - b; // 10 (number of elements)
    uintptr_t byte_diff = (uintptr_t)a - (uintptr_t)b; // 40 (if sizeof(int) == 4)
    

    4. Summary Table

    Situations - str(uintptr_t)s - (uintptr_t)str
    char *Number of chars, UB if > PTRDIFF_MAXNumber of bytes, always defined (modulo wrap)
    int *Number of ints, UB if > PTRDIFF_MAXNumber of bytes, not number of ints!
    Different arraysUBImplementation-defined, but not meaningful

    5. Conclusion

    • For strlen, casting to uintptr_t avoids UB from overflow, but is not standard C and may not be portable.
    • For other pointer types, this cast does not give the correct element count.
    • Pointer subtraction is only defined within the same array object (or one past the end).

    If you need to handle extremely long strings (longer than PTRDIFF_MAX), you need a different approach, such as returning a larger type (if your platform supports it), or using a loop that counts in chunks.


    TL;DR:
    (uintptr_t)s - (uintptr_t)str avoids UB for very long strings, but is not standard C and only gives the correct result for char *. Do not use this trick for other pointer types or in general pointer arithmetic.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.