1
0
mirror of https://github.com/stedolan/jq.git synced 2024-05-11 05:55:39 +00:00
Commit Graph

24 Commits

Author SHA1 Message Date
itchyny
f85c9fcb78 Revert "Allow .[-1] in path expressions"
This reverts commit 086a156ec3.

This commit leads to negative indexing wraps twice.
2023-08-16 17:49:44 -05:00
Emanuele Torre
4cf1408e0b Cast function pointers without prototype before calling them (#2842)
clang complained that this is deprecated in all versions of standard C,
and unsupported in C2x.
2023-08-13 22:39:54 +09:00
Emanuele Torre
5fc5453409 Add ERRORK opcode to trigger constant errors 2023-08-04 11:47:34 -05:00
Emanuele Torre
6716e23ae6 Declare cfunction.fptr as jv (*)() to avoid having to cast everywhere
You only need to specify the return type in a function pointer
declaration in C.

If you use () in the declaration, the function pointer can be called
with any arguments, and the type of the arguments is decided for each
function call based on the types of the arguments used for the call.
(To declare a function pointer for a function with no arguments, you use
`(void)'.)

Since all the cfunction structs have a  fptr  that points to a functions
that return jv, not void, we can we can just declare cfunction.fptr as
jv (*)()  and avoid having those annoying and technically not C-standard
compliant casts everywhere.
2023-07-31 15:28:01 -05:00
Nicolas Williams
c8b30dff4a Add JQ_FALLTHROUGH and use it to quiet warnings 2023-07-30 04:25:54 +02:00
Nicolas Williams
086a156ec3 Allow .[-1] in path expressions 2023-07-28 12:34:26 -05:00
Nico Williams
1cf6515c06 Fix try/catch catches more than it should #1859
Close #1885, #2140, #2011, #2220, #2485, #2073

Rename the FORK_OPT opcode to TRY_BEGIN, add a TRY_END opcode, and wrap
errors when raising through a TRY_END so that they will not be caught by
the matching TRY_BEGIN.

Now a `try exp catch handler` expression generates code like:

    TRY_BEGIN handler
    <exp>
    TRY_END
    JUMP past_handler
    handler: <handler>
    past_handler:
    ...

On backtrack through TRY_BEGIN it just backtracks.

If anything past the whole thing raises when <exp> produced a value,
then the TRY_END will catch the error, wrap it in another, and
backtrack.  The TRY_BEGIN will see a wrapped error and then it will
unwrap and re-raise the error.

If <exp> raises, then TRY_BEGIN will catch the error and jump to the
handler, but the TRY_BEGIN will not stack_save() in that case, so on
raise/backtrack the TRY_BEGIN will not execute again (nor will the
TRY_END).
2023-07-24 13:20:21 +02:00
itchyny
98f709d0c1 Fix stderr to output string with no decoration (fix #2063) 2023-07-23 17:06:00 -05:00
Emanuele Torre
bb0f898c01 Initialise jq_state->debug_cb{,_data} to NULL
To make debug/0 not call an uninitialised function pointer when using
--run-tests or when using a jq_state on which jq_set_debug_cb() has not
been called.
2023-07-18 12:12:50 -05:00
Emanuele Torre
e79335e3a5 Initialise jq_state->input_cb{,_data} to NULL in jq_init()
To avoid causing segmentation faults when  input/1  is called in a
jq_state on which  jq_set_input_cb()  has not been called; e.g. the one
used by  jq --run-tests.

That segfault could also be fixed in run_jq_tests() by calling:

  jq_set_input_cb(jq, NULL, NULL);

But I think it makes sense to just make jq_init() initialise those
values to NULL.

Ref: https://github.com/jqlang/jq/pull/2717#discussion_r1264338841
2023-07-16 00:03:46 -05:00
Nicolas Williams
1a1804afcb Fix LOADV/LOADVN refcount printing 2021-10-24 16:23:26 -05:00
Nicolas Williams
582717a7b4 Fix crash in LOADVN when stack grows
This `stack_push()` call in LOADVN invalidates `var`:

       jv* var = frame_local_var(jq, v, level);
       jv_free(stack_popn(jq));
------>stack_push(jq, *var);
       *var = jv_null();
       ^^^^^^

We have to re-compute `var`:

       jv* var = frame_local_var(jq, v, level);
       jv_free(stack_popn(jq));
       stack_push(jq, *var);
------>var = frame_local_var(jq, v, level);
       *var = jv_null();
2021-10-24 16:23:26 -05:00
Leonid S. Usov
cf4b48c7ba Save literal value of the parsed number to preserve it for the output
Extend jv_number to use decNumber for storing number literals. Any math
operations on the numbers will truncate them to double precision.
Comparisons when both numbers are literal numbers will compare them
without truncation.

Delay conversion of numbers to doubles until a math operation is performed,
to preserve precision. A literal jv_number will only need conversion to
double once, and will reuse the resultant double on subsequent
conversions.

Outputting literal jv_numbers preserves the original precision.

Add strong pthread requirement to manage contexts/allocations for
converting numbers between their decNumber, string, and double formats.
2019-10-22 14:11:04 -04:00
Nicolas Williams
b52fc1043b Fix assert in generator subexpressions (fix #1875)
Expressions of the form `path(EXPR) | select(GENERATOR)`, where `EXPR`
is a path expression and `GENERATOR` is a generator conditional
expression (e.g., `has("a"), has("b")`) cause an assertion if the
jq_state VM is torn down too soon.  That assert() was only correct if
assuming that the conditional is not a generator.

If the conditional is generator, then what we see is that when
backtracking a SUBEXP_END is executed without a corresponding
SUBEXP_BEGIN because the entire conditional is bracketed with
SUBEXP_BEGIN and SUBEXP_END, and since it's resumed in the middle, in
between the brackets.

Rather than assert that the jq->path_len being restored has some
particular value, we can simply re-compute it from the restored
jq->path.
2019-03-26 18:58:55 -05:00
William Langford
dd4cec664c fix memory leak 2018-08-30 21:09:04 -04:00
William Langford
0673dee1b3 Fix destructuring alternation
Attempting to use the existing FORK_OPT opcode resulted in difficulty
knowing when to pop an error message off the stack and when not to. This
commit makes DESTRUCTURE_ALT a real opcode that is identical to
FORK_OPT, except for never pushing the error message onto the stack when
continuing from an error backtrack.

Some small changes were necessary to the DUP/POP behavior surrounding
destructuring to accomodate this.
2018-08-17 23:15:48 -04:00
William Langford
94035be3fa Add --debug-trace=all for detailed debug output
When tracing execution, print the whole stack, not just the items used by the
current opcode
2017-03-26 05:36:22 -05:00
William Langford
3a8c8f4747 Add alternation destructuring operator ?//
This is a first pass to show the implementation.
It needs tests and evaluation, but doesn't break any existing tests.

NOT READY FOR MERGING
2017-03-26 05:36:22 -05:00
Nicolas Williams
2fcb257168 getpath/1 should be a path expression (fix #1358)
It needs to be possible to do something like

    getpath($paths[]) += 1

meaning: increment all the paths in . that are listed in $paths[].

In order to do this getpath() needs to update the jq->path and
jq->value_at_path as necessary.
2017-02-28 21:54:30 -06:00
Nicolas Williams
8ea21a54ad Add halt, halt_error builtins (fix #386) 2017-02-26 16:34:56 -06:00
Nicolas Williams
a03ae02f44 Fix memory leak 2017-02-26 16:33:50 -06:00
Nicolas Williams
b279713e47 fixup 2017-02-26 16:12:57 -06:00
Nicolas Williams
e8abc0a2f6 jq_compile_args(): allow object args to be object 2017-02-25 19:13:28 -06:00
David Tolnay
0c93eb3379 Move source files to src/ 2015-08-23 20:36:11 -07:00