Always check malloc result; Less calloc;

Some more questions:

- Should there be some kind of allocation helper so you don't need
  to always check the result?
- Throwing an error on OOM is convenient and is probably correct 90%
  of the time, it even provides a nice error message instead of a
  nil dereference somewhere.  But it's inflexible.  Is it the right
  thing to do?
This commit is contained in:
bjorn 2019-01-29 02:49:09 -08:00 committed by Bjorn Swenson
parent 5d29ec2db1
commit 6291a5ad5a
11 changed files with 29 additions and 5 deletions

View File

@ -14,15 +14,18 @@ static int l_lovrDataNewBlob(lua_State* L) {
if (type == LUA_TNUMBER) {
size = lua_tonumber(L, 1);
data = calloc(1, size);
lovrAssert(data, "Out of memory");
} else if (type == LUA_TSTRING) {
const char* str = luaL_checklstring(L, 1, &size);
data = malloc(size + 1);
lovrAssert(data, "Out of memory");
memcpy(data, str, size);
data[size] = '\0';
} else {
Blob* blob = luax_checktype(L, 1, Blob);
size = blob->size;
data = malloc(size);
lovrAssert(data, "Out of memory");
}
const char* name = luaL_optstring(L, 2, "");
Blob* blob = lovrBlobCreate(data, size, name);

View File

@ -36,8 +36,10 @@ void luax_checkvariant(lua_State* L, int index, Variant* variant) {
variant->type = TYPE_STRING;
size_t length;
const char* string = lua_tolstring(L, index, &length);
variant->value.string = calloc(1, length + 1);
variant->value.string = malloc(length + 1);
lovrAssert(variant->value.string, "Out of memory");
memcpy(variant->value.string, string, length);
variant->value.string[length] = '\0';
break;
case LUA_TUSERDATA:

View File

@ -30,6 +30,7 @@ int l_lovrCurveRender(lua_State* L) {
float t1 = luaL_optnumber(L, 3, 0.);
float t2 = luaL_optnumber(L, 4, 1.);
float* points = malloc(3 * n * sizeof(float));
lovrAssert(points, "Out of memory");
lovrCurveRender(curve, t1, t2, points, n);
lua_createtable(L, n, 0);
for (int i = 0; i < 3 * n; i++) {

View File

@ -16,6 +16,7 @@ AudioStream* lovrAudioStreamInit(AudioStream* stream, Blob* blob, size_t bufferS
stream->decoder = decoder;
stream->bufferSize = stream->channelCount * bufferSize * sizeof(short);
stream->buffer = malloc(stream->bufferSize);
lovrAssert(stream->buffer, "Out of memory");
stream->blob = blob;
lovrRetain(blob);

View File

@ -9,6 +9,7 @@ SoundData* lovrSoundDataInit(SoundData* soundData, int samples, int sampleRate,
soundData->channelCount = channelCount;
soundData->blob.size = samples * channelCount * (bitDepth / 8);
soundData->blob.data = calloc(1, soundData->blob.size);
lovrAssert(soundData->blob.data, "Out of memory");
return soundData;
}
@ -19,6 +20,7 @@ SoundData* lovrSoundDataInitFromAudioStream(SoundData* soundData, AudioStream* a
soundData->channelCount = audioStream->channelCount;
soundData->blob.size = audioStream->samples * audioStream->channelCount * (audioStream->bitDepth / 8);
soundData->blob.data = calloc(1, soundData->blob.size);
lovrAssert(soundData->blob.data, "Out of memory");
int samples;
short* buffer = soundData->blob.data;

View File

@ -144,7 +144,9 @@ TextureData* lovrTextureDataInit(TextureData* textureData, int width, int height
textureData->height = height;
textureData->format = format;
textureData->blob.size = size;
textureData->blob.data = memset(malloc(size), value, size);
textureData->blob.data = malloc(size);
lovrAssert(textureData->blob.data, "Out of memory");
memset(textureData->blob.data, value, size);
vec_init(&textureData->mipmaps);
return textureData;
}

View File

@ -39,6 +39,7 @@ bool lovrFilesystemInit(const char* argExe, const char* argGame, const char* arg
}
state.source = malloc(LOVR_PATH_MAX * sizeof(char));
lovrAssert(state.source, "Out of memory");
state.identity = NULL;
state.isFused = true;
vec_init(&state.requirePattern[0]);
@ -259,6 +260,7 @@ int lovrFilesystemSetIdentity(const char* identity) {
} else {
state.savePathRelative = malloc(LOVR_PATH_MAX);
state.savePathFull = malloc(LOVR_PATH_MAX);
lovrAssert(state.savePathRelative && state.savePathFull, "Out of memory");
if (!state.savePathRelative || !state.savePathFull) {
return 1;
}

View File

@ -86,10 +86,12 @@ Model* lovrModelInit(Model* model, ModelData* modelData) {
if (modelData->textures.length > 0) {
model->textures = calloc(modelData->textures.length, sizeof(Texture*));
lovrAssert(model->textures, "Out of memory");
}
if (modelData->materialCount > 0) {
model->materials = calloc(modelData->materialCount, sizeof(Material*));
lovrAssert(model->materials, "Out of memory");
for (int i = 0; i < modelData->materialCount; i++) {
ModelMaterial* materialData = &modelData->materials[i];
Material* material = lovrMaterialCreate();
@ -130,6 +132,7 @@ Model* lovrModelInit(Model* model, ModelData* modelData) {
}
model->nodeTransforms = malloc(16 * modelData->nodeCount * sizeof(float));
lovrAssert(model->nodeTransforms, "Out of memory");
for (int i = 0; i < modelData->nodeCount; i++) {
ModelNode* node = &model->modelData->nodes[i];
mat4 transform = model->nodeTransforms[i];

View File

@ -1552,7 +1552,8 @@ Buffer* lovrBufferInit(Buffer* buffer, size_t size, void* data, BufferType type,
buffer->data = glMapBufferRange(glType, 0, size, flags | GL_MAP_FLUSH_EXPLICIT_BIT);
} else {
#endif
buffer->data = calloc(1, size);
buffer->data = malloc(size);
lovrAssert(buffer->data, "Out of memory");
glBufferData(glType, size, data, convertBufferUsage(usage));
if (data) {
@ -1608,6 +1609,7 @@ static GLuint compileShader(GLenum type, const char** sources, int count) {
int logLength;
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &logLength);
char* log = malloc(logLength);
lovrAssert(log, "Out of memory");
glGetShaderInfoLog(shader, logLength, &logLength, log);
lovrThrow("Could not compile shader:\n%s", log);
}
@ -1624,6 +1626,7 @@ static GLuint linkProgram(GLuint program) {
int logLength;
glGetProgramiv(program, GL_INFO_LOG_LENGTH, &logLength);
char* log = malloc(logLength);
lovrAssert(log, "Out of memory");
glGetProgramInfoLog(program, logLength, &logLength, log);
lovrThrow("Could not link shader:\n%s", log);
}
@ -1765,22 +1768,26 @@ static void lovrShaderSetupUniforms(Shader* shader) {
case UNIFORM_FLOAT:
uniform.size = uniform.components * uniform.count * sizeof(float);
uniform.value.data = calloc(1, uniform.size);
lovrAssert(uniform.value.data, "Out of memory");
break;
case UNIFORM_INT:
uniform.size = uniform.components * uniform.count * sizeof(int);
uniform.value.data = calloc(1, uniform.size);
lovrAssert(uniform.value.data, "Out of memory");
break;
case UNIFORM_MATRIX:
uniform.size = uniform.components * uniform.components * uniform.count * sizeof(float);
uniform.value.data = calloc(1, uniform.size);
lovrAssert(uniform.value.data, "Out of memory");
break;
case UNIFORM_SAMPLER:
case UNIFORM_IMAGE:
uniform.size = uniform.count * (uniform.type == UNIFORM_SAMPLER ? sizeof(Texture*) : sizeof(Image));
uniform.value.data = calloc(1, uniform.size);
lovrAssert(uniform.value.data, "Out of memory");
// Use the value for ints to bind texture slots, but use the value for textures afterwards.
for (int i = 0; i < uniform.count; i++) {

View File

@ -226,6 +226,7 @@ char* lovrShaderBlockGetShaderCode(ShaderBlock* block, const char* blockName, si
// Allocate
char* code = malloc(size + 1);
lovrAssert(code, "Out of memory");
// Concatenate
char* s = code;

View File

@ -29,8 +29,8 @@ void lovrThrow(const char* format, ...) {
}
void* _lovrAlloc(const char* type, size_t size, void (*destructor)(void*)) {
Ref* ref = calloc(1, size);
if (!ref) return lovrThrow("Out of memory"), NULL;
Ref* ref = malloc(size);
lovrAssert(ref, "Out of memory");
ref->destructor = destructor;
ref->type = type;
ref->count = 1;