Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Prefer ptrdiff_t over ssize_t #1448

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

Merged

Conversation

jchadwick-buf
Copy link
Contributor

@jchadwick-buf jchadwick-buf commented Apr 18, 2025

This resolves a portability issue for Windows/MSVC.

ssize_t is a POSIX-defined type, not a C or C++ standard. ssize_t is not as useful of a type as it initially seems, as it only actually guarantees a range of [-1, SSIZE_MAX], where SSIZE_MAX is at least _POSIX_SSIZE_MAX (32767).

ptrdiff_t is defined to be the type that results from taking the difference of two pointers. It is not guaranteed for it to be possible for all pointer differences to be represented as a ptrdiff_t; however, despite not theoretically having a much more stringent definition, it typically either meets or exceeds the utility of ssize_t on top of being a part of the C standard, thereby being usable outside of POSIX platforms. See, for example, this thread on the emacs-devel list:

https://lists.gnu.org/archive/html/emacs-devel/2014-10/msg00019.html

Therefore, I suggest adopting ptrdiff_t instead. For any of the obvious platforms, it should be exactly the same as ssize_t but more portable.

Neither ssize_t nor ptrdiff_t are guaranteed to have a superset of the range of size_t, so code that uses ssize_t OR ptrdiff_t to store size_t values should tread carefully; this is not safe and not guaranteed to work (e.g. loops that iterate backwards should probably be adjusted.)

One usage of ssize_t is replaced with int64_t instead since it is more appropriate in that circumstance.

Related issue: #768

`ssize_t` is a POSIX-defined type, not a C or C++ standard. `ssize_t` is not as useful of a type as it initially seems, as it only actually guarantees a range of [-1, `SSIZE_MAX`], where `SSIZE_MAX` is at least `_POSIX_SSIZE_MAX` (32767).

`ptrdiff_t` is defined to be the type that results from taking the difference of two pointers. It is not guaranteed for it to be possible for all pointer differences to be represented as a `ptrdiff_t`; however, despite not theoretically having a much more stringent definition, it typically either meets or exceeds the utility of `ssize_t` on top of being a part of the C standard, thereby being usable outside of POSIX platforms. See, for example, this thread on the emacs-devel list:

https://lists.gnu.org/archive/html/emacs-devel/2014-10/msg00019.html

Therefore, I suggest adopting `ptrdiff_t` instead. For any of the obvious platforms, it should be exactly the same as `ssize_t` but more portable.

Neither `ssize_t` nor `ptrdiff_t` are guaranteed to have a superset of the range of `size_t`, so code that uses `ssize_t` OR `ptrdiff_t` to store `size_t` values should tread carefully; this is not safe and not guaranteed to work (e.g. loops that iterate backwards should probably be adjusted.)

One usage of `ssize_t` is replaced with `int64_t` instead since it is more appropriate in that circumstance.
@copybara-service copybara-service bot merged commit 939905e into google:master May 2, 2025
1 check passed
@jchadwick-buf jchadwick-buf deleted the prefer-ptrdiff-over-ssize branch May 8, 2025 17:56
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