Commit Graph

1742 Commits

Author SHA1 Message Date
Fabian Homborg
12d7c7feb6 Switch to readdir from readdir_r
It's deprecated in glibc, and does not work properly on Solaris.

Fixes #5458.
2019-01-03 11:19:45 +01:00
Mahmoud Al-Qudsi
3444e1db18 Fix unsafe locale usage in wcstod_l fallback
Using `setlocale` is both not thread-safe and not correct, as
a) The global locale is usually stored in static storage, so
   simultaneous calls to `setlocale` can result in corruption, and
b) `setlocale` changes the locale for the entire application, not
   just the calling thread. This means that even if we wrapped the
   `wcstod_l` in a mutex to prevent the previous point, the results
   would still be incorrect because this would incorrectly influence the
   results of locale-aware functions executed in other threads while
   this thread is executing.

The previous comment mentioned that `uselocale` hadn't worked. I'm not
sure what the failing implementation looked like, but `uselocale` can be
tricky. The committed implementation passes the tests for me under Linux
and FreeBSD.
2019-01-02 18:43:43 -06:00
Mahmoud Al-Qudsi
bc0a0b4bc8 Fix wcstod_l infinite recursion under FreeBSD
This was the actual issue leading to memory corruption under FreeBSD in
issue #5453, worked around by correcting the detection of `wcstod_l` so
that our version of the function is not called at all.

If we are 100% certain that `wcstod_l` does not exist, then then the
existing code is fine. But given that our checks have failed seperately
on two different platforms already (FreeBSD and Cygwin/newlib), it's a
good precaution to take.
2019-01-02 18:43:43 -06:00
Mahmoud Al-Qudsi
ef23923c8d Drop use of deprecated bzero(3)
Use `memset(__, 0, __)` instead. Also fixes #5461 by not needing `bzero`
from `strings.h` anymore.
2019-01-02 00:28:25 -06:00
Mahmoud Al-Qudsi
d1913f0df0 Add workaround for Cygwin process management and job control bugs
We cannot wait by pgroup under Cygwin for unknown reasons. Always
wait on jobs by individual processes. See code for more information.
2019-01-02 00:14:07 -06:00
Fabian Homborg
217486e547 math: Use simpler format string
It seems like musl's printf here fails on `%*lc`. So we use `%*ls`,
which we already use in string, so it should work.
2019-01-01 14:52:26 +01:00
Fabian Homborg
9b980f5e6e Use fstatvfs if ST_LOCAL is available
Allows us to sometimes use mmap on NetBSD (proper capitalization is
important).
2018-12-31 14:24:24 +01:00
Fabian Homborg
e9ad88d4b0 Don't set the title on NetBSD's wscon
We might want to check the terminfo "XT" capability here, but for now
let's do the quick fix.
2018-12-31 14:24:23 +01:00
Fabian Homborg
b77a909a4f Make a few variables const
These are then passed to tparm, but we explicitly cast the const
away.
2018-12-31 14:24:23 +01:00
Fabian Homborg
58ceb00781 Make a few methods const
This helps on netbsd, because enter_standout_mode et al are const
there.

These methods don't alter their argument, so they should have been
const to begin with.
2018-12-31 14:24:23 +01:00
Fabian Homborg
c5f9f59555 Always cast to non-const for tparm
This is non-const on macOS, but some of the args we pass are always
const on netbsd.

I have no idea why you'd ever want this to modify its argument, but whatever.
2018-12-31 14:24:23 +01:00
Fabian Homborg
3e03625113 Don't try to use fstatfs on netbsd
I can find a man page for it, but it doesn't seem to work.
2018-12-31 14:24:22 +01:00
Fabian Homborg
ee5e4cf8e2 Use int tputs_arg_t on netbsd 2018-12-31 14:24:22 +01:00
Fabian Homborg
a3085a3059 Use varargs tparm on netbsd
This needs to be defined _early_.
2018-12-31 14:24:21 +01:00
Fabian Homborg
aaee5dd32d Rename "lines" for netbsd's benefit
Netbsd's curses does a bit of a landgrab, and takes the names "lines"
and "newline" and a few others for itself.
2018-12-31 14:24:21 +01:00
Fabian Homborg
a615151d91 Revert "tinyexpr: Make te_expr a class"
Turns out this crashes on musl when doing te_expr::parameter.push_back(). For some reason.

This reverts commit 2e11e6c692.
2018-12-31 10:37:13 +01:00
Mahmoud Al-Qudsi
803619b19b Convert some old-school int booleans to bool 2018-12-31 00:46:31 -06:00
Mahmoud Al-Qudsi
0337588979 fixup! Do not use up the ~WNOHANG waitpid call on completed processes 2018-12-30 21:44:14 -06:00
Mahmoud Al-Qudsi
2ebdcf82ee Do not use up the ~WNOHANG waitpid call on completed processes
This is the more correct fix for #5447, as regardless of which process
in the job (be it the first or the last) finished first, once we have
waited on a process without ~WNOHANG we don't do that for any subsequent
processes in the job.

It is also a waste to call into the kernel to wait for a process we
already know is completed!
2018-12-30 20:53:25 -06:00
Mahmoud Al-Qudsi
bfe08a471d Remove fish_mutex_t wrapper around std::mutex
@ridiculousfish had introduced this in 3a45cad12e
to work around an issue with Coverity Scan where it couldn't tell the
mutex was correctly locked, but even with the `fish_mutex_t` hack, it
still emits the same warnings, so there's no pointing in keeping it.
2018-12-30 20:37:36 -06:00
Mahmoud Al-Qudsi
077d656b87 Allow redeclaration of main process via setup_fork_guards()
This is necessary for the history race condition test to succeed.

(That test is permanently disabled under WSL (as it always fails) so I
didn't catch this on my end.)
2018-12-30 20:23:39 -06:00
Mahmoud Al-Qudsi
b4301ff54f Drop initial_pid and optimize debug_shared() fast case
If we are running on the main thread, don't call `getpid()`
unnecessarily from `debug_shared()`.
2018-12-30 19:55:24 -06:00
Mahmoud Al-Qudsi
8dddc62aeb Optimize ASSERT_IS_NOT_FORKED_CHILD()
Use `pthread_atfork()` to mark child processes as dirty when `fork()` is
invoked rather than needing to call into the kernel each time
`ASSERT_IS_NOT_FORKED_CHILD()` is called.

This makes simple test cases that hit `ASSERT_IS_NOT_FORKED_CHILD()` 1.8x faster.

                        ------------------------

With a7998c4829 reverted but before this optimization:

```
mqudsi@ZBOOK ~/r/fish-shell> hyperfine -S build/fish 'for i in (seq 100000); test 1 = 1; end'
Benchmark #1: for i in (seq 100000); test 1 = 1; end

  Time (mean ± σ):     717.8 ms ±  14.9 ms    [User: 503.4 ms, System: 216.2 ms]
  Range (min … max):   692.3 ms … 740.2 ms
```

With a7998c4829 reverted and with this optimization:

```
mqudsi@ZBOOK ~/r/fish-shell> hyperfine -S build/fish 'for i in (seq 100000); test 1 = 1; end'
Benchmark #1: for i in (seq 100000); test 1 = 1; end

  Time (mean ± σ):     397.2 ms ±  22.3 ms    [User: 322.1 ms, System: 79.3 ms]
  Range (min … max):   376.0 ms … 444.0 ms
```

Without a7998c4829 reverted and with this optimization:

mqudsi@ZBOOK ~/r/fish-shell> hyperfine -S build/fish 'for i in (seq 100000); test 1 = 1; end'
Benchmark #1: for i in (seq 100000); test 1 = 1; end

  Time (mean ± σ):     423.4 ms ±  51.6 ms    [User: 363.2 ms, System: 61.3 ms]
  Range (min … max):   378.4 ms … 541.1 ms
```
2018-12-30 19:55:24 -06:00
Mahmoud Al-Qudsi
840619197e Optimize ASSERT_IS_MAIN_THREAD()
By using a user-land thread-local integer and lock-free (at least under
x86/x64) atomics, we can implement a safe `assert_is_main_thread()`
without calling into the kernel. Thread-local variables are part of
C++11.

This is called a lot in some performance-sensitive areas, so it is worth
optimizing.
2018-12-30 19:25:50 -06:00
Mahmoud Al-Qudsi
259cf02aac Wait on individual processes in a job in reverse order
This fixes #5438 by having fish block while waiting on a foreground job
via its individual processes by enumerating the procs in reverse order,
such that we hang waiting for the last job in the IO chain to terminate,
rather than the first.
2018-12-30 19:02:38 -06:00
Fabian Homborg
b0072482e4 Only warn on exec for background jobs
If it's a foreground job, it is related to the currently running exec.

This fixes exec in functions, i.e.

    function reload
        exec fish
    end

would previously always ask about the "function reload" job.

Fixes #5449.

Fixes oh-my-fish/oh-my-fish#664.
2018-12-30 22:32:07 +01:00
Fabian Homborg
e33d29a5d8 tinyexpr: Reserve arity parameters
This somehow fixes heap-buffer-overflow? I thought this was supposed
to be safe.
2018-12-30 20:34:13 +01:00
Fabian Homborg
2e11e6c692 tinyexpr: Make te_expr a class
Removes some #define weirdness.
2018-12-30 19:34:46 +01:00
Fabian Homborg
b8697e7795 tinyexpr: Rename te_variable to te_builtin
Variables aren't a thing here anymore.
2018-12-30 19:34:46 +01:00
Fabian Homborg
a433868363 tinyexpr: Make parameters te_expr* instead of void* 2018-12-30 19:34:46 +01:00
Fabian Homborg
e504faeb38 tinyexpr: Add Comments 2018-12-30 19:34:46 +01:00
Fabian Homborg
c3c1ae18c6 tinyexpr: C++ify find_builtin 2018-12-30 19:34:45 +01:00
Fabian Homborg
b193df8b42 tinyexpr: Move enums together and stop explicit numbering
We should _not_ be doing bit-fiddling with these, so there's no reason
to care about the number.

This also removes the unused "TE_VARIABLE" symbol.
2018-12-30 19:34:45 +01:00
Fabian Homborg
3bbec871e4 tinyexpr: Free all parameters again
This used implicit fallthrough to free all.

We still iterate back-to-front (i--) because maybe that's important?
2018-12-30 19:34:45 +01:00
Fabian Homborg
84ca265b48 tinyexpr: Unfiddle the bits
Mainly this removes the "TYPE_MASK" macro that just masks off the
higher bits, which I don't think were ever actually used.

Much of this seems like anticipation of future direction, but we're
going somewhere else.
2018-12-30 19:34:44 +01:00
Fabian Homborg
61e7f84e29 tinyexpr: Remove PURE flag
This was unused because all functions were marked as pure. We don't
have any plans to add any that aren't, and if we did we'd still have
this in git.
2018-12-30 19:34:41 +01:00
Fabian Homborg
cbc25d7829 [tinyexpr] Port to C++
This removes the need to run c-compilation on one file, and allows us
to in future c++-ify this a bit.

There's a lot of bit-fiddling here that is quite unnecessary, better
error-handling would be nice...

So far this removes a few more unused things (because I would have had
to port them), including:

- Functions with ARITY > 3 (even 3 isn't used, but just so we don't
get complacent)

- Variables

- Most functions moved out of the header, because only te_interp is used.

- The te_print function
2018-12-30 19:34:06 +01:00
Fabian Homborg
a7998c4829 Don't ASSERT_IS_NOT_FORKED_CHILD so much
This is hammered sooo much that it actually hurts performance.

    for i in (seq 100000); test 1 = 1; end

is about 40% (!) slower with it.
2018-12-30 18:59:41 +01:00
Mahmoud Al-Qudsi
040d921fa1 Fix check for valid disowned pgids
The function `add_disowned_pgid` adds process *group* ids and not
process ids. It multiplies the value by negative 1 to indicate a wait
on a process group, so the original value must be positive.
2018-12-30 10:15:07 -06:00
Fabian Homborg
4a3ac6e91e Don't wait for disowned pgids if they are special
If a job is disowned that, for some reason, has a pgid that is special
to waitpid, like 0 (process with pgid of the calling process), -1 (any
process), or our actual pgid, that would lead to us waiting for too
many processes when we later try to reap the disowned processes (to
stop zombies from appearing).

And that means we'd snag away the processes we actually do want to
wait for, which would end with us in a waiting loop.

This is tough to reproduce, the easiest I've found was

    fish -ic 'sleep 5 &; disown; set -g __fish_git_prompt_showupstream auto; __fish_git_prompt'

in a git repo.

What we do is to not allow special pgids in the disowned_pids list.
That means we might leave a zombie around (though we probably wait on
0 somewhere), but that's preferable to infinitely looping.

See #5426.
2018-12-30 16:04:57 +01:00
Fabian Homborg
742fde0dd6 Don't use less in highlighting test
It doesn't have to be installed.

`cat` is in our dependencies, so we can assume it's there.

Fixes #5436.
2018-12-28 17:57:53 +01:00
David Adam
05222a055a Merge branch 'Integration_3.0.0' 2018-12-28 22:10:49 +08:00
Fabian Homborg
14ee19cc1b Use HAVE_WCSTOD_L also in header 2018-12-18 11:03:33 +01:00
Aaron Gyes
57d6124e6e builtin_test: don't exit 1 for eval errors, add tests for big args
Return STATUS_INVALID_ARGS when failing due to evaluation errors,
so we can tell the difference between an error and falseness.

Add a test for the ERANGE error
2018-12-16 14:51:26 -08:00
Aaron Gyes
cf2b40040a STATUS_INVALID_ARGS = 2
The rest of the high-numbered exit codes are not values used by scripts
or builtins, they are internal to fish and come out of
the parser for example.

Prior to adding STATUS_INVALID_ARGS, builtins were usually exiting 2
if they had a special exit status for the situation of bad arguments.

Set it to 2.
2018-12-16 14:51:18 -08:00
Aaron Gyes
b404b9392c builtin_test.cpp: split a long line, add braces 2018-12-16 14:51:10 -08:00
Aaron Gyes
1f871c4d0c builtin_test.cpp: check for ERANGE and special fish_wcstoll errno
We were not parsing an in-range number when we claimed we were,
and were thus failing to error with invalid numbers and returned
a wrong test result. Fixed #5414

Also, provide the detail we can for the other error cases.
2018-12-16 14:51:02 -08:00
Aaron Gyes
1adcd2d591 builtin_test: don't exit 1 for eval errors, add tests for big args
Return STATUS_INVALID_ARGS when failing due to evaluation errors,
so we can tell the difference between an error and falseness.

Add a test for the ERANGE error
2018-12-15 22:05:19 -08:00
Aaron Gyes
b8113f8e97 STATUS_INVALID_ARGS = 2
The rest of the high-numbered exit codes are not values used by scripts
or builtins, they are internal to fish and come out of
the parser for example.

Prior to adding STATUS_INVALID_ARGS, builtins were usually exiting 2
if they had a special exit status for the situation of bad arguments.

Set it to 2.
2018-12-15 21:05:27 -08:00
Mahmoud Al-Qudsi
3855c2217f Remove scripted XDG_CONFIG_HOME uses
Cleaned up the code to no longer replicate in fishscript what fish
already does (and caches to boot) in C++ in setting up the paths to the
user configuration directory.

Also introduced a `$__fish_user_data_dir` instead of the sporadic
definitions of `$userdatadir` that may or may not go through
`XDG_DATA_HOME`.
2018-12-14 22:09:29 -06:00