DRAFT lib/m2: time: Fix segfault by allocating timeval struct.

Suggested by Meghan Denny <hello@nektro.net>.

* lib/m2/time.c (__tv): New global variable.
(time): Use it to allocate timeval struct.
This commit is contained in:
Jan (janneke) Nieuwenhuizen 2023-01-24 07:59:49 +01:00
parent e465eb8eb9
commit ee352a7b5c
1 changed files with 7 additions and 4 deletions

View File

@ -1,6 +1,6 @@
/* -*-comment-start: "//";comment-end:""-*- /* -*-comment-start: "//";comment-end:""-*-
* GNU Mes --- Maxwell Equations of Software * GNU Mes --- Maxwell Equations of Software
* Copyright © 2018,2019,2022 Jan (janneke) Nieuwenhuizen <janneke@gnu.org> * Copyright © 2018,2019,2022,2023 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
* *
* This file is part of GNU Mes. * This file is part of GNU Mes.
* *
@ -22,13 +22,16 @@
#include <syscall.h> #include <syscall.h>
#include <time.h> #include <time.h>
char *__tv;
long long
time (long* result) time (long* result)
{ {
int r; int r;
struct timeval tv; if (__tv == 0)
struct timezone tz; __tv = malloc (sizeof (struct timeval));
r = _sys_call2 (SYS_gettimeofday, tv, tz); struct timeval *tv = __tv;
r = _sys_call2 (SYS_gettimeofday, tv, 0);
if (r != 0) if (r != 0)
return -1; return -1;
if (result != 0) if (result != 0)