]> git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - sys_shared.c
Fix uses of strlcpy on unterminated source strings
[xonotic/darkplaces.git] / sys_shared.c
index 78bb56e3914eaf38cbbe5ae016b6c53f02e4d06d..f9f118bf5180e88a79d936e1a9421acc276d5bdc 100644 (file)
 #include "thread.h"
 #include "libcurl.h"
 
+#ifdef WIN32
+       // Microsoft's compiler complains about portable code
+       #pragma warning(disable : 4996)
+#endif
+
 sys_t sys;
 
 static char sys_timestring[128];
@@ -71,10 +76,13 @@ void Sys_Quit (int returnvalue)
 #ifdef __ANDROID__
        Sys_AllowProfiling(false);
 #endif
+
 #ifndef WIN32
-       fcntl (0, F_SETFL, fcntl (0, F_GETFL, 0) & ~O_NONBLOCK);
+       fcntl(fileno(stdout), F_SETFL, fcntl(fileno(stdout), F_GETFL, 0) & ~O_NONBLOCK);
+       fcntl(fileno(stderr), F_SETFL, fcntl(fileno(stderr), F_GETFL, 0) & ~O_NONBLOCK);
 #endif
        fflush(stdout);
+       fflush(stderr);
 
        exit(returnvalue);
 }
@@ -205,13 +213,13 @@ notfound:
        if (!dllhandle && strrchr(sys.argv[0], '/'))
        {
                char path[MAX_OSPATH];
-               strlcpy(path, sys.argv[0], sizeof(path));
+               dp_strlcpy(path, sys.argv[0], sizeof(path));
                strrchr(path, '/')[1] = 0;
                for (i = 0; dllnames[i] != NULL; i++)
                {
                        char temp[MAX_OSPATH];
-                       strlcpy(temp, path, sizeof(temp));
-                       strlcat(temp, dllnames[i], sizeof(temp));
+                       dp_strlcpy(temp, path, sizeof(temp));
+                       dp_strlcat(temp, dllnames[i], sizeof(temp));
                        Con_DPrintf (" \"%s\"", temp);
 
                        if(Sys_LoadLibrary(temp, &dllhandle))
@@ -700,9 +708,10 @@ void Sys_Error (const char *error, ...)
        va_list argptr;
        char string[MAX_INPUTLINE];
 
-// change stdin to non blocking
+       // set output to blocking stderr
+       sys.outfd = fileno(stderr);
 #ifndef WIN32
-       fcntl (0, F_SETFL, fcntl (0, F_GETFL, 0) & ~O_NONBLOCK);
+       fcntl(sys.outfd, F_SETFL, fcntl(sys.outfd, F_GETFL, 0) & ~O_NONBLOCK);
 #endif
 
        va_start (argptr,error);
@@ -716,6 +725,8 @@ void Sys_Error (const char *error, ...)
 
        Sys_SDL_Dialog("Engine Error", string);
 
+       fflush(stderr);
+
        exit (1);
 }
 
@@ -941,10 +952,16 @@ static void Sys_HandleCrash(int sig)
                default:      sigdesc = "Yo dawg, we hit a bug while hitting a bug";
        }
 
+       // set output to blocking stderr
+       sys.outfd = fileno(stderr);
+#ifndef WIN32
+       fcntl(sys.outfd, F_SETFL, fcntl(sys.outfd, F_GETFL, 0) & ~O_NONBLOCK);
+#endif
+
        fprintf(stderr, "\n\n\e[1;37;41m    Engine Crash: %s (%d)    \e[m\n", sigdesc, sig);
 #if __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 1
        // the first two addresses will be in this function and in signal() in libc
-       backtrace_symbols_fd(stackframes + 2, framecount - 2, fileno(stderr));
+       backtrace_symbols_fd(stackframes + 2, framecount - 2, sys.outfd);
 #endif
        fprintf(stderr, "\e[1m%s\e[m\n", engineversion);
 
@@ -956,6 +973,8 @@ static void Sys_HandleCrash(int sig)
 
        Sys_SDL_Dialog("Engine Crash", sigdesc);
 
+       fflush(stderr);
+
        exit (sig);
 }