Fix potential segfaults at restore related to ctime().

In some games, restore() passes the result of ctime() to mvprintw() or
some other variadic message-formatting function.  If ctime() has not
been declared properly, its return type is inferred to be int instead
of char *.  This does not cause a warning because the compiler does not
know the correct type of variadic arguments.

On platforms where ints and pointers are not the same size, this can,
probably depending on alignment, result in a segfault that is not easy
to trace.

Including time.h fixes the problem.  Some games manually declared
ctime() and avoided the bug.  These declarations have also been
replaced with the include.
This commit is contained in:
John "Elwin" Edwards 2017-09-15 20:51:10 -04:00
parent c661fd79d4
commit ab25967717
7 changed files with 8 additions and 12 deletions

View file

@ -21,6 +21,7 @@
#include <sys/types.h>
#include <sys/stat.h>
#include <signal.h>
#include <time.h>
#include "rogue.h"
bool save_file(FILE *savef);

View file

@ -24,6 +24,7 @@
#include <sys/types.h>
#include <sys/stat.h>
#include <signal.h>
#include <time.h>
#include "rogue.h"
#include <fcntl.h>
#include <errno.h>
@ -237,7 +238,7 @@ restore(char *file, char *envp[])
mpos = 0;
msg("");
/*msg("%s: %s", file, ctime(&sbuf2.st_mtime));*/
msg("%s: %s", file, ctime(&sbuf2.st_mtime));
/*
* defeat multiple restarting from the same place

View file

@ -54,8 +54,6 @@ extern WINDOW *hw;
* Function types
*/
char *ctime(), *getenv();
void tstp(), endit(int);
long lseek();

View file

@ -16,6 +16,7 @@
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#define KERNEL
#include <signal.h>
#undef KERNEL

View file

@ -268,24 +268,18 @@ wait_for(WINDOW *win, char ch)
#ifdef NEED_GETTIME
#include <stdio.h>
#include <time.h>
/*
* gettime:
* This routine returns the current time as a string
*/
#ifdef ATT
#include <time.h>
#endif
#ifdef BSD
#include <sys/time.h>
#endif
char *
gettime()
{
register char *timeptr;
char *ctime();
long int now, time();
long int now;
time(&now); /* get current time */
timeptr = ctime(&now); /* convert to string */

View file

@ -18,6 +18,7 @@
#include <string.h>
#include <sys/types.h>
#include <fcntl.h>
#include <time.h>
#include "rogue.h"
#include "rogue.ext"

View file

@ -22,6 +22,7 @@
#include <sys/types.h>
#include <sys/stat.h>
#include <signal.h>
#include <time.h>
#include <errno.h>
#if !defined(_WIN32)
#include <unistd.h>
@ -30,7 +31,6 @@
#include "rogue.ext"
EXTCHAR version[];
EXTCHAR *ctime();
bool dosave(void);
void save_file(FILE *savef);