fixed possible segfault in writeLog

This would happen if initLogger returned INVALID_FILE and you still tried using writeLog
This commit is contained in:
Václav Šmejkal 2022-12-06 18:14:34 +01:00
parent 385f39b05d
commit ee886f0237
No known key found for this signature in database
GPG Key ID: FD749A97DF2D5E19

View File

@ -86,6 +86,12 @@ logFile initLogger(char *directoryPath)
void writeLog(int loggerFile, char *logMessage) void writeLog(int loggerFile, char *logMessage)
{ {
//CHECK IF loggerFile IS CREATED
if (loggerFile == INVALID_FILE)
{
return; //TODO: Add some kind of error message
}
//VARIABLES //VARIABLES
char *buffer = malloc(strlen(WRITE_FORMAT) + strlen(logMessage) + 2); char *buffer = malloc(strlen(WRITE_FORMAT) + strlen(logMessage) + 2);
time_t timeL = time(NULL); time_t timeL = time(NULL);
@ -93,7 +99,7 @@ void writeLog(int loggerFile, char *logMessage)
sprintf(buffer, WRITE_FORMATTING, tm.tm_hour, tm.tm_min, tm.tm_sec, logMessage); //LOAD MESSAGE sprintf(buffer, WRITE_FORMATTING, tm.tm_hour, tm.tm_min, tm.tm_sec, logMessage); //LOAD MESSAGE
if (write(loggerFile, buffer, strlen(buffer)) == 0) abort(); //WRITE (YAY) //TODO: Make it safe write(loggerFile, buffer, strlen(buffer)); //WRITE (YAY)
//DEALLOCATION //DEALLOCATION
free(buffer); free(buffer);