Static analysis cppcheck

This commit is contained in:
Doyle Thai 2017-07-13 12:50:01 +10:00
parent 979679470b
commit be0b84a244
2 changed files with 22 additions and 21 deletions

40
dqn.h
View File

@ -723,7 +723,8 @@ DQN_FILE_SCOPE DqnV2 DqnV2_Normalise (DqnV2 a);
DQN_FILE_SCOPE bool DqnV2_Overlaps (DqnV2 a, DqnV2 b); DQN_FILE_SCOPE bool DqnV2_Overlaps (DqnV2 a, DqnV2 b);
DQN_FILE_SCOPE DqnV2 DqnV2_Perpendicular(DqnV2 a); DQN_FILE_SCOPE DqnV2 DqnV2_Perpendicular(DqnV2 a);
DQN_FILE_SCOPE DqnV2 DqnV2_ConstrainToRatio(DqnV2 dim, DqnV2 ratio); // Resize the dimension to fit the aspect ratio provided. Downscale only. DQN_FILE_SCOPE DqnV2 DqnV2_ResizeKeepAspectRatio(DqnV2 srcSize, DqnV2 targetSize);
DQN_FILE_SCOPE DqnV2 DqnV2_ConstrainToRatio (DqnV2 dim, DqnV2 ratio); // Resize the dimension to fit the aspect ratio provided. Downscale only.
#ifdef DQN_CPP_MODE #ifdef DQN_CPP_MODE
DQN_FILE_SCOPE inline DqnV2 operator- (DqnV2 a, DqnV2 b) { return DqnV2_Sub (a, b); } DQN_FILE_SCOPE inline DqnV2 operator- (DqnV2 a, DqnV2 b) { return DqnV2_Sub (a, b); }
@ -1985,11 +1986,7 @@ DQN_FILE_SCOPE void *DqnMem_Realloc(void *memory, const size_t newSize)
DQN_FILE_SCOPE void DqnMem_Free(void *memory) DQN_FILE_SCOPE void DqnMem_Free(void *memory)
{ {
if (memory) if (memory) free(memory);
{
free(memory);
memory = NULL;
}
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
@ -2658,8 +2655,7 @@ DQN_FILE_SCOPE DqnV2 DqnV2_Normalise(DqnV2 a)
f32 magnitude = DqnV2_Length(DqnV2_2f(0, 0), a); f32 magnitude = DqnV2_Length(DqnV2_2f(0, 0), a);
if (magnitude == 0) return DqnV2_1f(0.0f); if (magnitude == 0) return DqnV2_1f(0.0f);
DqnV2 result = DqnV2_2f(a.x, a.y); DqnV2 result = DqnV2_Scalef(a, 1 / magnitude);
result = DqnV2_Scalef(a, 1 / magnitude);
return result; return result;
} }
@ -2693,6 +2689,15 @@ DQN_FILE_SCOPE DqnV2 DqnV2_Perpendicular(DqnV2 a)
} }
DQN_FILE_SCOPE DqnV2 DqnV2_ResizeKeepAspectRatio(DqnV2 srcSize, DqnV2 targetSize)
{
f32 ratioA = srcSize.w / targetSize.w;
f32 ratioB = srcSize.h / targetSize.h;
f32 ratio = DQN_MIN(ratioA, ratioB);
DqnV2 result = DqnV2_Scalef(targetSize, ratio);
return result;
}
DQN_FILE_SCOPE DqnV2 DqnV2_ConstrainToRatio(DqnV2 dim, DqnV2 ratio) DQN_FILE_SCOPE DqnV2 DqnV2_ConstrainToRatio(DqnV2 dim, DqnV2 ratio)
{ {
DqnV2 result = {0}; DqnV2 result = {0};
@ -6164,6 +6169,7 @@ DQN_FILE_SCOPE char **DqnDirInternal_PlatformRead(const char *const dir,
// TODO(doyle): Logging // TODO(doyle): Logging
DQN_ASSERT(DQN_INVALID_CODE_PATH); DQN_ASSERT(DQN_INVALID_CODE_PATH);
*numFiles = 0; *numFiles = 0;
closedir(dirHandle);
return NULL; return NULL;
} }
@ -6178,6 +6184,7 @@ DQN_FILE_SCOPE char **DqnDirInternal_PlatformRead(const char *const dir,
// TODO(doyle): Logging // TODO(doyle): Logging
*numFiles = 0; *numFiles = 0;
closedir(dirHandle);
return NULL; return NULL;
} }
} }
@ -6318,12 +6325,11 @@ DQN_FILE_SCOPE bool DqnFile_ReadEntireFile(const char *const path, u8 *const buf
{ {
if (!path || !buffer) return false; if (!path || !buffer) return false;
bool result = true;
size_t bytesReadInternal; size_t bytesReadInternal;
DqnFile file = {};
bool result = DqnFile_Open(path, &file, DqnFilePermissionFlag_Read, DqnFileAction_OpenOnly);
// TODO(doyle): Logging // TODO(doyle): Logging
DqnFile file = {};
result = DqnFile_Open(path, &file, DqnFilePermissionFlag_Read, DqnFileAction_OpenOnly);
if (!result) goto cleanup; if (!result) goto cleanup;
if (file.size > bufferSize) if (file.size > bufferSize)
@ -6405,15 +6411,9 @@ DQN_FILE_SCOPE bool DqnFile_GetFileSize(const char *const path, size_t *const si
if (*size == 0) if (*size == 0)
{ {
// If stat fails, then do a manual byte count // If stat fails, then do a manual byte count
DqnFile file = {}; FILE *handle = fopen(path, "r");
if (!DqnFileInternal_UnixOpen(path, &file, DqnFilePermissionFlag_Read, *size = DqnFileInternal_UnixGetFileSizeManual(handle, false);
DqnFileAction_OpenOnly)) fclose(handle);
{
return false;
}
*size = DqnFileInternal_UnixGetFileSizeManual((FILE *)file.handle, false);
DqnFile_Close(&file);
} }
return true; return true;

View File

@ -1563,6 +1563,7 @@ void FileTest()
if (1) if (1)
{ {
printf("start file tests");
// Test file open // Test file open
if (1) if (1)
{ {
@ -1708,7 +1709,7 @@ void FileTest()
// Read using the ReadEntireFile api which doesn't need a file handle as an argument // Read using the ReadEntireFile api which doesn't need a file handle as an argument
{ {
size_t reqSize; size_t reqSize = 0;
DQN_ASSERT(DqnFile_GetFileSize(fileNames[i], &reqSize)); DQN_ASSERT(DqnFile_GetFileSize(fileNames[i], &reqSize));
u8 *buffer = (u8 *)DqnMemStack_Push(&memStack, reqSize); u8 *buffer = (u8 *)DqnMemStack_Push(&memStack, reqSize);