error: SDL's allocators now call SDL_OutOfMemory on error.

This means the allocator's caller doesn't need to use SDL_OutOfMemory directly
if the allocation fails.

This applies to the usual allocators: SDL_malloc, SDL_calloc, SDL_realloc
(all of these regardless of if the app supplied a custom allocator or we're
using system malloc() or an internal copy of dlmalloc under the hood),
SDL_aligned_alloc, SDL_small_alloc, SDL_strdup, SDL_asprintf, SDL_wcsdup...
probably others. If it returns something you can pass to SDL_free, it should
work.

The caller might still need to use SDL_OutOfMemory if something that wasn't
SDL allocated the memory: operator new in C++ code, Objective-C's alloc
message, win32 GlobalAlloc, etc.

Fixes #8642.
This commit is contained in:
Ryan C. Gordon
2023-11-30 00:14:27 -05:00
parent 70b65d4170
commit 447b508a77
197 changed files with 313 additions and 742 deletions

View File

@@ -125,7 +125,6 @@ SDL_Haptic *SDL_HapticOpen(int device_index)
/* Create the haptic device */
haptic = (SDL_Haptic *)SDL_malloc(sizeof(*haptic));
if (!haptic) {
SDL_OutOfMemory();
return NULL;
}
@@ -297,7 +296,6 @@ SDL_Haptic *SDL_HapticOpenFromJoystick(SDL_Joystick *joystick)
/* Create the haptic device */
haptic = (SDL_Haptic *)SDL_malloc(sizeof(*haptic));
if (!haptic) {
SDL_OutOfMemory();
SDL_UnlockJoysticks();
return NULL;
}

View File

@@ -107,7 +107,6 @@ static SDL_hapticlist_item *OpenHaptic(SDL_Haptic *haptic, SDL_hapticlist_item *
haptic->nplaying = haptic->neffects;
haptic->effects = (struct haptic_effect *)SDL_malloc(sizeof(struct haptic_effect) * haptic->neffects);
if (!haptic->effects) {
SDL_OutOfMemory();
return NULL;
}
SDL_memset(haptic->effects, 0, sizeof(struct haptic_effect) * haptic->neffects);

View File

@@ -473,13 +473,10 @@ static int SDL_SYS_HapticOpenFromService(SDL_Haptic *haptic, io_service_t servic
int ret2;
/* Allocate the hwdata */
haptic->hwdata = (struct haptic_hwdata *)
SDL_malloc(sizeof(*haptic->hwdata));
haptic->hwdata = (struct haptic_hwdata *) SDL_calloc(1, sizeof(*haptic->hwdata));
if (!haptic->hwdata) {
SDL_OutOfMemory();
goto creat_err;
}
SDL_memset(haptic->hwdata, 0, sizeof(*haptic->hwdata));
/* Open the device */
ret = FFCreateDevice(service, &haptic->hwdata->device);
@@ -513,7 +510,6 @@ static int SDL_SYS_HapticOpenFromService(SDL_Haptic *haptic, io_service_t servic
haptic->effects = (struct haptic_effect *)
SDL_malloc(sizeof(struct haptic_effect) * haptic->neffects);
if (!haptic->effects) {
SDL_OutOfMemory();
goto open_err;
}
/* Clear the memory */
@@ -700,7 +696,7 @@ static int SDL_SYS_SetDirection(FFEFFECT *effect, SDL_HapticDirection *dir, int
/* Has axes. */
rglDir = SDL_malloc(sizeof(LONG) * naxes);
if (!rglDir) {
return SDL_OutOfMemory();
return -1;
}
SDL_memset(rglDir, 0, sizeof(LONG) * naxes);
effect->rglDirection = rglDir;
@@ -771,11 +767,10 @@ static int SDL_SYS_ToFFEFFECT(SDL_Haptic *haptic, FFEFFECT *dest, SDL_HapticEffe
dest->dwFlags = FFEFF_OBJECTOFFSETS; /* Seems obligatory. */
/* Envelope. */
envelope = SDL_malloc(sizeof(FFENVELOPE));
envelope = SDL_calloc(1, sizeof(FFENVELOPE));
if (!envelope) {
return SDL_OutOfMemory();
return -1;
}
SDL_memset(envelope, 0, sizeof(FFENVELOPE));
dest->lpEnvelope = envelope;
envelope->dwSize = sizeof(FFENVELOPE); /* Always should be this. */
@@ -788,7 +783,7 @@ static int SDL_SYS_ToFFEFFECT(SDL_Haptic *haptic, FFEFFECT *dest, SDL_HapticEffe
if (dest->cAxes > 0) {
axes = SDL_malloc(sizeof(DWORD) * dest->cAxes);
if (!axes) {
return SDL_OutOfMemory();
return -1;
}
axes[0] = haptic->hwdata->axes[0]; /* Always at least one axis. */
if (dest->cAxes > 1) {
@@ -804,11 +799,10 @@ static int SDL_SYS_ToFFEFFECT(SDL_Haptic *haptic, FFEFFECT *dest, SDL_HapticEffe
switch (src->type) {
case SDL_HAPTIC_CONSTANT:
hap_constant = &src->constant;
constant = SDL_malloc(sizeof(FFCONSTANTFORCE));
constant = SDL_calloc(1, sizeof(FFCONSTANTFORCE));
if (!constant) {
return SDL_OutOfMemory();
return -1;
}
SDL_memset(constant, 0, sizeof(FFCONSTANTFORCE));
/* Specifics */
constant->lMagnitude = CONVERT(hap_constant->level);
@@ -846,11 +840,10 @@ static int SDL_SYS_ToFFEFFECT(SDL_Haptic *haptic, FFEFFECT *dest, SDL_HapticEffe
case SDL_HAPTIC_SAWTOOTHUP:
case SDL_HAPTIC_SAWTOOTHDOWN:
hap_periodic = &src->periodic;
periodic = SDL_malloc(sizeof(FFPERIODIC));
periodic = SDL_calloc(1, sizeof(FFPERIODIC));
if (!periodic) {
return SDL_OutOfMemory();
return -1;
}
SDL_memset(periodic, 0, sizeof(FFPERIODIC));
/* Specifics */
periodic->dwMagnitude = CONVERT(SDL_abs(hap_periodic->magnitude));
@@ -891,11 +884,10 @@ static int SDL_SYS_ToFFEFFECT(SDL_Haptic *haptic, FFEFFECT *dest, SDL_HapticEffe
case SDL_HAPTIC_FRICTION:
hap_condition = &src->condition;
if (dest->cAxes > 0) {
condition = SDL_malloc(sizeof(FFCONDITION) * dest->cAxes);
condition = SDL_calloc(dest->cAxes, sizeof(FFCONDITION));
if (!condition) {
return SDL_OutOfMemory();
return -1;
}
SDL_memset(condition, 0, sizeof(FFCONDITION));
/* Specifics */
for (i = 0; i < dest->cAxes; i++) {
@@ -934,11 +926,10 @@ static int SDL_SYS_ToFFEFFECT(SDL_Haptic *haptic, FFEFFECT *dest, SDL_HapticEffe
case SDL_HAPTIC_RAMP:
hap_ramp = &src->ramp;
ramp = SDL_malloc(sizeof(FFRAMPFORCE));
ramp = SDL_calloc(1, sizeof(FFRAMPFORCE));
if (!ramp) {
return SDL_OutOfMemory();
return -1;
}
SDL_memset(ramp, 0, sizeof(FFRAMPFORCE));
/* Specifics */
ramp->lStart = CONVERT(hap_ramp->start);
@@ -972,11 +963,10 @@ static int SDL_SYS_ToFFEFFECT(SDL_Haptic *haptic, FFEFFECT *dest, SDL_HapticEffe
case SDL_HAPTIC_CUSTOM:
hap_custom = &src->custom;
custom = SDL_malloc(sizeof(FFCUSTOMFORCE));
custom = SDL_calloc(1, sizeof(FFCUSTOMFORCE));
if (!custom) {
return SDL_OutOfMemory();
return -1;
}
SDL_memset(custom, 0, sizeof(FFCUSTOMFORCE));
/* Specifics */
custom->cChannels = hap_custom->channels;
@@ -1107,9 +1097,8 @@ int SDL_SYS_HapticNewEffect(SDL_Haptic *haptic, struct haptic_effect *effect,
/* Alloc the effect. */
effect->hweffect = (struct haptic_hweffect *)
SDL_malloc(sizeof(struct haptic_hweffect));
SDL_calloc(1, sizeof(struct haptic_hweffect));
if (!effect->hweffect) {
SDL_OutOfMemory();
goto err_hweffect;
}

View File

@@ -375,12 +375,10 @@ static int SDL_SYS_HapticOpenFromFD(SDL_Haptic *haptic, int fd)
{
/* Allocate the hwdata */
haptic->hwdata = (struct haptic_hwdata *)
SDL_malloc(sizeof(*haptic->hwdata));
SDL_calloc(1, sizeof(*haptic->hwdata));
if (!haptic->hwdata) {
SDL_OutOfMemory();
goto open_err;
}
SDL_memset(haptic->hwdata, 0, sizeof(*haptic->hwdata));
/* Set the data. */
haptic->hwdata->fd = fd;
@@ -397,7 +395,6 @@ static int SDL_SYS_HapticOpenFromFD(SDL_Haptic *haptic, int fd)
haptic->effects = (struct haptic_effect *)
SDL_malloc(sizeof(struct haptic_effect) * haptic->neffects);
if (!haptic->effects) {
SDL_OutOfMemory();
goto open_err;
}
/* Clear the memory */
@@ -903,9 +900,9 @@ int SDL_SYS_HapticNewEffect(SDL_Haptic *haptic, struct haptic_effect *effect,
/* Allocate the hardware effect */
effect->hweffect = (struct haptic_hweffect *)
SDL_malloc(sizeof(struct haptic_hweffect));
SDL_calloc(1, sizeof(struct haptic_hweffect));
if (!effect->hweffect) {
return SDL_OutOfMemory();
return -1;
}
/* Prepare the ff_effect */

View File

@@ -167,7 +167,7 @@ int SDL_DINPUT_HapticMaybeAddDevice(const DIDEVICEINSTANCE *pdidInstance)
item = (SDL_hapticlist_item *)SDL_calloc(1, sizeof(SDL_hapticlist_item));
if (!item) {
return SDL_OutOfMemory();
return -1;
}
item->name = WIN_StringToUTF8(pdidInstance->tszProductName);
@@ -286,11 +286,10 @@ static int SDL_DINPUT_HapticOpenFromDevice(SDL_Haptic *haptic, LPDIRECTINPUTDEVI
DIPROPDWORD dipdw;
/* Allocate the hwdata */
haptic->hwdata = (struct haptic_hwdata *)SDL_malloc(sizeof(*haptic->hwdata));
haptic->hwdata = (struct haptic_hwdata *)SDL_calloc(1, sizeof(*haptic->hwdata));
if (!haptic->hwdata) {
return SDL_OutOfMemory();
return -1;
}
SDL_memset(haptic->hwdata, 0, sizeof(*haptic->hwdata));
/* We'll use the device8 from now on. */
haptic->hwdata->device = device8;
@@ -402,7 +401,6 @@ static int SDL_DINPUT_HapticOpenFromDevice(SDL_Haptic *haptic, LPDIRECTINPUTDEVI
haptic->effects = (struct haptic_effect *)
SDL_malloc(sizeof(struct haptic_effect) * haptic->neffects);
if (!haptic->effects) {
SDL_OutOfMemory();
goto acquire_err;
}
/* Clear the memory */
@@ -541,7 +539,7 @@ static int SDL_SYS_SetDirection(DIEFFECT *effect, SDL_HapticDirection *dir, int
/* Has axes. */
rglDir = SDL_malloc(sizeof(LONG) * naxes);
if (!rglDir) {
return SDL_OutOfMemory();
return -1;
}
SDL_memset(rglDir, 0, sizeof(LONG) * naxes);
effect->rglDirection = rglDir;
@@ -613,11 +611,10 @@ static int SDL_SYS_ToDIEFFECT(SDL_Haptic *haptic, DIEFFECT *dest,
dest->dwFlags = DIEFF_OBJECTOFFSETS; /* Seems obligatory. */
/* Envelope. */
envelope = SDL_malloc(sizeof(DIENVELOPE));
envelope = SDL_calloc(1, sizeof(DIENVELOPE));
if (!envelope) {
return SDL_OutOfMemory();
return -1;
}
SDL_memset(envelope, 0, sizeof(DIENVELOPE));
dest->lpEnvelope = envelope;
envelope->dwSize = sizeof(DIENVELOPE); /* Always should be this. */
@@ -630,7 +627,7 @@ static int SDL_SYS_ToDIEFFECT(SDL_Haptic *haptic, DIEFFECT *dest,
if (dest->cAxes > 0) {
axes = SDL_malloc(sizeof(DWORD) * dest->cAxes);
if (!axes) {
return SDL_OutOfMemory();
return -1;
}
axes[0] = haptic->hwdata->axes[0]; /* Always at least one axis. */
if (dest->cAxes > 1) {
@@ -646,11 +643,10 @@ static int SDL_SYS_ToDIEFFECT(SDL_Haptic *haptic, DIEFFECT *dest,
switch (src->type) {
case SDL_HAPTIC_CONSTANT:
hap_constant = &src->constant;
constant = SDL_malloc(sizeof(DICONSTANTFORCE));
constant = SDL_calloc(1, sizeof(DICONSTANTFORCE));
if (!constant) {
return SDL_OutOfMemory();
return -1;
}
SDL_memset(constant, 0, sizeof(DICONSTANTFORCE));
/* Specifics */
constant->lMagnitude = CONVERT(hap_constant->level);
@@ -688,11 +684,10 @@ static int SDL_SYS_ToDIEFFECT(SDL_Haptic *haptic, DIEFFECT *dest,
case SDL_HAPTIC_SAWTOOTHUP:
case SDL_HAPTIC_SAWTOOTHDOWN:
hap_periodic = &src->periodic;
periodic = SDL_malloc(sizeof(DIPERIODIC));
periodic = SDL_calloc(1, sizeof(DIPERIODIC));
if (!periodic) {
return SDL_OutOfMemory();
return -1;
}
SDL_memset(periodic, 0, sizeof(DIPERIODIC));
/* Specifics */
periodic->dwMagnitude = CONVERT(SDL_abs(hap_periodic->magnitude));
@@ -732,11 +727,10 @@ static int SDL_SYS_ToDIEFFECT(SDL_Haptic *haptic, DIEFFECT *dest,
case SDL_HAPTIC_INERTIA:
case SDL_HAPTIC_FRICTION:
hap_condition = &src->condition;
condition = SDL_malloc(sizeof(DICONDITION) * dest->cAxes);
condition = SDL_calloc(dest->cAxes, sizeof(DICONDITION));
if (!condition) {
return SDL_OutOfMemory();
return -1;
}
SDL_memset(condition, 0, sizeof(DICONDITION));
/* Specifics */
for (i = 0; i < (int)dest->cAxes; i++) {
@@ -773,11 +767,10 @@ static int SDL_SYS_ToDIEFFECT(SDL_Haptic *haptic, DIEFFECT *dest,
case SDL_HAPTIC_RAMP:
hap_ramp = &src->ramp;
ramp = SDL_malloc(sizeof(DIRAMPFORCE));
ramp = SDL_calloc(1, sizeof(DIRAMPFORCE));
if (!ramp) {
return SDL_OutOfMemory();
return -1;
}
SDL_memset(ramp, 0, sizeof(DIRAMPFORCE));
/* Specifics */
ramp->lStart = CONVERT(hap_ramp->start);
@@ -811,11 +804,10 @@ static int SDL_SYS_ToDIEFFECT(SDL_Haptic *haptic, DIEFFECT *dest,
case SDL_HAPTIC_CUSTOM:
hap_custom = &src->custom;
custom = SDL_malloc(sizeof(DICUSTOMFORCE));
custom = SDL_calloc(1, sizeof(DICUSTOMFORCE));
if (!custom) {
return SDL_OutOfMemory();
return -1;
}
SDL_memset(custom, 0, sizeof(DICUSTOMFORCE));
/* Specifics */
custom->cChannels = hap_custom->channels;

View File

@@ -291,13 +291,10 @@ int SDL_SYS_HapticNewEffect(SDL_Haptic *haptic, struct haptic_effect *effect,
int result;
/* Alloc the effect. */
effect->hweffect = (struct haptic_hweffect *)
SDL_malloc(sizeof(struct haptic_hweffect));
effect->hweffect = (struct haptic_hweffect *) SDL_calloc(1, sizeof(struct haptic_hweffect));
if (!effect->hweffect) {
SDL_OutOfMemory();
return -1;
}
SDL_zerop(effect->hweffect);
if (haptic->hwdata->bXInputHaptic) {
result = SDL_XINPUT_HapticNewEffect(haptic, effect, base);

View File

@@ -78,13 +78,11 @@ int SDL_XINPUT_HapticMaybeAddDevice(const DWORD dwUserid)
return -1; /* no force feedback on this device. */
}
item = (SDL_hapticlist_item *)SDL_malloc(sizeof(SDL_hapticlist_item));
item = (SDL_hapticlist_item *)SDL_calloc(1, sizeof(SDL_hapticlist_item));
if (!item) {
return SDL_OutOfMemory();
return -1;
}
SDL_zerop(item);
/* !!! FIXME: I'm not bothering to query for a real name right now (can we even?) */
{
char buf[64];
@@ -174,19 +172,18 @@ static int SDL_XINPUT_HapticOpenFromUserIndex(SDL_Haptic *haptic, const Uint8 us
haptic->effects = (struct haptic_effect *)
SDL_malloc(sizeof(struct haptic_effect) * haptic->neffects);
if (!haptic->effects) {
return SDL_OutOfMemory();
return -1;
}
/* Clear the memory */
SDL_memset(haptic->effects, 0,
sizeof(struct haptic_effect) * haptic->neffects);
haptic->hwdata = (struct haptic_hwdata *)SDL_malloc(sizeof(*haptic->hwdata));
haptic->hwdata = (struct haptic_hwdata *)SDL_calloc(1, sizeof(*haptic->hwdata));
if (!haptic->hwdata) {
SDL_free(haptic->effects);
haptic->effects = NULL;
return SDL_OutOfMemory();
return -1;
}
SDL_memset(haptic->hwdata, 0, sizeof(*haptic->hwdata));
haptic->hwdata->bXInputHaptic = 1;
haptic->hwdata->userid = userid;