Fix casts that should be using uintptr_t This is needed to support CHERI, and thus Arm's experimental Morello prototype, where pointers are implemented using unforgeable capabilities that include bounds and permissions metadata to provide fine-grained spatial and referential memory safety, as well as revocation by sweeping memory to provide heap temporal memory safety. On most systems (anything with a flat memory hierarchy rather than using segment-based addressing), size_t and uintptr_t are the same type. However, on CHERI, size_t is just an integer offset, whereas uintptr_t is still a capability as described above. Casting a pointer to size_t will strip the metadata and validity tag, and casting from size_t to a pointer will result in a null-derived capability whose validity tag is not set, and thus cannot be dereferenced without faulting. The audio and cursor casts were harmless as they intend to stuff an integer into a pointer, but using uintptr_t is the idiomatic way to do that and silences our compiler warnings (which our build tool makes fatal by default as they often indicate real problems). The iconv and egl casts were true positives as SDL_iconv_t and iconv_t are pointer types, as is NativeDisplayType on most OSes, so this would have trapped at run time when using the round-tripped pointers. The gles2 casts were also harmless; the OpenGL API defines this argument to be a pointer type (and uses the argument name "pointer"), but it in fact represents an integer offset, so like audio and cursor the additional idiomatic cast is needed to silence the warning.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
diff --git a/src/audio/SDL_audiocvt.c b/src/audio/SDL_audiocvt.c
index 7d6bade..34f505f 100644
--- a/src/audio/SDL_audiocvt.c
+++ b/src/audio/SDL_audiocvt.c
@@ -955,8 +955,8 @@ SDL_BuildAudioResampleCVT(SDL_AudioCVT * cvt, const int dst_channels,
if (cvt->filter_index >= (SDL_AUDIOCVT_MAX_FILTERS-2)) {
return SDL_SetError("Too many filters needed for conversion, exceeded maximum of %d", SDL_AUDIOCVT_MAX_FILTERS-2);
}
- cvt->filters[SDL_AUDIOCVT_MAX_FILTERS-1] = (SDL_AudioFilter) (size_t) src_rate;
- cvt->filters[SDL_AUDIOCVT_MAX_FILTERS] = (SDL_AudioFilter) (size_t) dst_rate;
+ cvt->filters[SDL_AUDIOCVT_MAX_FILTERS-1] = (SDL_AudioFilter) (uintptr_t) src_rate;
+ cvt->filters[SDL_AUDIOCVT_MAX_FILTERS] = (SDL_AudioFilter) (uintptr_t) dst_rate;
if (src_rate < dst_rate) {
const double mult = ((double) dst_rate) / ((double) src_rate);
diff --git a/src/audio/SDL_audiodev.c b/src/audio/SDL_audiodev.c
index 4236402..dc1daee 100644
--- a/src/audio/SDL_audiodev.c
+++ b/src/audio/SDL_audiodev.c
@@ -65,7 +65,7 @@ test_device(const int iscapture, const char *fname, int flags, int (*test) (int
* information, making this information inaccessible at
* enumeration time
*/
- SDL_AddAudioDevice(iscapture, fname, NULL, (void *) dummyhandle);
+ SDL_AddAudioDevice(iscapture, fname, NULL, (void *) (uintptr_t) dummyhandle);
}
}
}
diff --git a/src/render/opengles2/SDL_render_gles2.c b/src/render/opengles2/SDL_render_gles2.c
index f36b34f..4343afb 100644
--- a/src/render/opengles2/SDL_render_gles2.c
+++ b/src/render/opengles2/SDL_render_gles2.c
@@ -961,7 +961,7 @@ SetDrawState(GLES2_RenderData *data, const SDL_RenderCommand *cmd, const GLES2_I
}
if (texture) {
- data->glVertexAttribPointer(GLES2_ATTRIBUTE_TEXCOORD, 2, GL_FLOAT, GL_FALSE, 0, (const GLvoid *) (cmd->data.draw.first + (sizeof (GLfloat) * 8)));
+ data->glVertexAttribPointer(GLES2_ATTRIBUTE_TEXCOORD, 2, GL_FLOAT, GL_FALSE, 0, (const GLvoid *) (uintptr_t) (cmd->data.draw.first + (sizeof (GLfloat) * 8)));
}
if (GLES2_SelectProgram(data, imgsrc, texture ? texture->w : 0, texture ? texture->h : 0) < 0) {
@@ -1004,7 +1004,7 @@ SetDrawState(GLES2_RenderData *data, const SDL_RenderCommand *cmd, const GLES2_I
}
/* all drawing commands use this */
- data->glVertexAttribPointer(GLES2_ATTRIBUTE_POSITION, 2, GL_FLOAT, GL_FALSE, 0, (const GLvoid *) cmd->data.draw.first);
+ data->glVertexAttribPointer(GLES2_ATTRIBUTE_POSITION, 2, GL_FLOAT, GL_FALSE, 0, (const GLvoid *) (uintptr_t) cmd->data.draw.first);
if (is_copy_ex != was_copy_ex) {
if (is_copy_ex) {
@@ -1018,8 +1018,8 @@ SetDrawState(GLES2_RenderData *data, const SDL_RenderCommand *cmd, const GLES2_I
}
if (is_copy_ex) {
- data->glVertexAttribPointer(GLES2_ATTRIBUTE_ANGLE, 2, GL_FLOAT, GL_FALSE, 0, (const GLvoid *) (cmd->data.draw.first + (sizeof (GLfloat) * 16)));
- data->glVertexAttribPointer(GLES2_ATTRIBUTE_CENTER, 2, GL_FLOAT, GL_FALSE, 0, (const GLvoid *) (cmd->data.draw.first + (sizeof (GLfloat) * 24)));
+ data->glVertexAttribPointer(GLES2_ATTRIBUTE_ANGLE, 2, GL_FLOAT, GL_FALSE, 0, (const GLvoid *) (uintptr_t) (cmd->data.draw.first + (sizeof (GLfloat) * 16)));
+ data->glVertexAttribPointer(GLES2_ATTRIBUTE_CENTER, 2, GL_FLOAT, GL_FALSE, 0, (const GLvoid *) (uintptr_t) (cmd->data.draw.first + (sizeof (GLfloat) * 24)));
}
return 0;
diff --git a/src/stdlib/SDL_iconv.c b/src/stdlib/SDL_iconv.c
index 004e774..2defa6e 100644
--- a/src/stdlib/SDL_iconv.c
+++ b/src/stdlib/SDL_iconv.c
@@ -55,13 +55,13 @@ SDL_COMPILE_TIME_ASSERT(iconv_t, sizeof (iconv_t) <= sizeof (SDL_iconv_t));
SDL_iconv_t
SDL_iconv_open(const char *tocode, const char *fromcode)
{
- return (SDL_iconv_t) ((size_t) iconv_open(tocode, fromcode));
+ return (SDL_iconv_t) ((uintptr_t) iconv_open(tocode, fromcode));
}
int
SDL_iconv_close(SDL_iconv_t cd)
{
- return iconv_close((iconv_t) ((size_t) cd));
+ return iconv_close((iconv_t) ((uintptr_t) cd));
}
size_t
@@ -71,9 +71,9 @@ SDL_iconv(SDL_iconv_t cd,
{
size_t retCode;
#ifdef ICONV_INBUF_NONCONST
- retCode = iconv((iconv_t) ((size_t) cd), (char **) inbuf, inbytesleft, outbuf, outbytesleft);
+ retCode = iconv((iconv_t) ((uintptr_t) cd), (char **) inbuf, inbytesleft, outbuf, outbytesleft);
#else
- retCode = iconv((iconv_t) ((size_t) cd), inbuf, inbytesleft, outbuf, outbytesleft);
+ retCode = iconv((iconv_t) ((uintptr_t) cd), inbuf, inbytesleft, outbuf, outbytesleft);
#endif
if (retCode == (size_t) - 1) {
switch (errno) {
diff --git a/src/video/SDL_egl.c b/src/video/SDL_egl.c
index 4592735..87d693e 100644
--- a/src/video/SDL_egl.c
+++ b/src/video/SDL_egl.c
@@ -507,12 +507,12 @@ SDL_EGL_LoadLibrary(_THIS, const char *egl_path, NativeDisplayType native_displa
}
if (_this->egl_data->eglGetPlatformDisplay) {
- _this->egl_data->egl_display = _this->egl_data->eglGetPlatformDisplay(platform, (void *)(size_t)native_display, NULL);
+ _this->egl_data->egl_display = _this->egl_data->eglGetPlatformDisplay(platform, (void *)(uintptr_t)native_display, NULL);
} else {
if (SDL_EGL_HasExtension(_this, SDL_EGL_CLIENT_EXTENSION, "EGL_EXT_platform_base")) {
_this->egl_data->eglGetPlatformDisplayEXT = SDL_EGL_GetProcAddress(_this, "eglGetPlatformDisplayEXT");
if (_this->egl_data->eglGetPlatformDisplayEXT) {
- _this->egl_data->egl_display = _this->egl_data->eglGetPlatformDisplayEXT(platform, (void *)(size_t)native_display, NULL);
+ _this->egl_data->egl_display = _this->egl_data->eglGetPlatformDisplayEXT(platform, (void *)(uintptr_t)native_display, NULL);
}
}
}
diff --git a/src/video/x11/SDL_x11mouse.c b/src/video/x11/SDL_x11mouse.c
index e20b430..a87712a 100644
--- a/src/video/x11/SDL_x11mouse.c
+++ b/src/video/x11/SDL_x11mouse.c
@@ -77,7 +77,7 @@ X11_CreateDefaultCursor()
cursor = SDL_calloc(1, sizeof(*cursor));
if (cursor) {
/* None is used to indicate the default cursor */
- cursor->driverdata = (void*)None;
+ cursor->driverdata = (void*)(uintptr_t)None;
} else {
SDL_OutOfMemory();
}
@@ -216,7 +216,7 @@ X11_CreateCursor(SDL_Surface * surface, int hot_x, int hot_y)
if (x11_cursor == None) {
x11_cursor = X11_CreatePixmapCursor(surface, hot_x, hot_y);
}
- cursor->driverdata = (void*)x11_cursor;
+ cursor->driverdata = (void*)(uintptr_t)x11_cursor;
} else {
SDL_OutOfMemory();
}
@@ -257,7 +257,7 @@ X11_CreateSystemCursor(SDL_SystemCursor id)
x11_cursor = X11_XCreateFontCursor(GetDisplay(), shape);
- cursor->driverdata = (void*)x11_cursor;
+ cursor->driverdata = (void*)(uintptr_t)x11_cursor;
} else {
SDL_OutOfMemory();
}