Mercurial > hg > toybox
view toys/posix/time.c @ 1613:96aa7ec74936 draft
Fix yet another sed bug.
The s/// command would copy the \ of substitutions before deciding what to
do with them (generally overwriting the \ with the new data). When the
substitution was A) at the very end of the new string, B) resolved to nothing,
it could leave a trailing \ that didn't belong there and didn't get overwritten
because the "copy trailing data" part that copies the original string's null
terminator already happened before the \ overwrote it.
The ghostwheel() function restarts regexes after embedded NUL bytes, but
if the string it's passed is _longer_ than the length it's told then it
gets confused (and it means we're off the end of our allocation so segfaults
are likely).
Fix: test for \ first and move the "copy byte" logic into an else case.
author | Rob Landley <rob@landley.net> |
---|---|
date | Mon, 15 Dec 2014 03:34:55 -0600 |
parents | 85f297591693 |
children | cbb1aca81eca |
line wrap: on
line source
/* time.c - time a simple command * * Copyright 2013 Rob Landley <rob@landley.net> * * See http://pubs.opengroup.org/onlinepubs/9699919799/utilities/time.html USE_TIME(NEWTOY(time, "<1^p", TOYFLAG_USR|TOYFLAG_BIN)) config TIME bool "time" default y help usage: time [-p] COMMAND [ARGS...] Run command line and report real, user, and system time elapsed in seconds. (real = clock on the wall, user = cpu used by command's code, system = cpu used by OS on behalf of command.) -p posix mode (ignored) */ #include "toys.h" void time_main(void) { pid_t pid; struct timeval tv, tv2; gettimeofday(&tv, NULL); if (!(pid = xfork())) xexec_optargs(0); else { int stat; struct rusage ru; float r, u, s; wait4(pid, &stat, 0, &ru); gettimeofday(&tv2, NULL); if (tv.tv_usec > tv2.tv_usec) { tv2.tv_usec += 1000000; tv2.tv_sec--; } r = (tv2.tv_sec-tv.tv_sec)+((tv2.tv_usec-tv.tv_usec)/1000000.0); u = ru.ru_utime.tv_sec+(ru.ru_utime.tv_usec/1000000.0); s = ru.ru_stime.tv_sec+(ru.ru_stime.tv_usec/1000000.0); fprintf(stderr, "real %f\nuser %f\nsys %f\n", r, u, s); toys.exitval = WIFEXITED(stat) ? WEXITSTATUS(stat) : WTERMSIG(stat); } }