Commit Graph
37 Commits
Author SHA1 Message Date
Emanuele Torre ab47880b4c Make jq_get_lib_dirs return an empty array if JQ_LIBRARY_PATH is not set
For the jq_state used by the jq utility, the JQ_LIBRARY_PATH attribute
will always be set, but, in general, it is possible that it might not
be.

If it is not set, jq_get_lib_dirs() will return jv_invalid().

That is not good, because some code in linker.c expects it to always
returns an array.

This patch makes jq_get_lib_dirs() return an empty array if
JQ_LIBRARY_PATH is not set to prevent problems.

This issue made OSS fuzz trigger failed assertions every time it tried
to compile a script that uses "include".

Fixes https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=61796
2023-08-27 00:37:54 -05:00
Emanuele Torre 6436f1e0f8 Fix memory leak in find_lib for some invalid inputs 2023-08-26 17:17:29 -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 f94a9d463f Let error(null) throw null
This patch removes the weird behaviour of jv_invalid_with_msg(jv_null())
that returns jv_invalid() (i.e. empty), instead of a boxed jv_null().

The previous behaviour of  null|error  was obviously unintentional, and
allowing is jv_invalid_with_msg() to return values on which you can't
call jv_invalid_get_msg() is only error prone.
2023-08-04 17:38:52 -05:00
Emanuele Torre 3b6e7ddd72 Constant fold all kinds of constants
This patch exports all the binary operator builtins functions from
builtin.c and uses them for constant folding in the parser, allowing
constant folding to work will all kinds and combinations of constants.

Now string*number,  $ARGS+$ARGS,  string/string,  etc will also be
constant folded and the implementation of constant folded operators and
runtime operators will be the same.

And thanks to the new ERRORK bytecode operation, errors are constant
folded too! (e.g. 1 / 0   [] * {}   etc)
2023-08-04 11:47:34 -05:00
Emanuele Torre 5fc5453409 Add ERRORK opcode to trigger constant errors 2023-08-04 11:47:34 -05:00
Emanuele Torre dcaf701ef6 Simplify String rule in parser (#2805)
Use a StringStart component that is either FORMAT QQSTRING_START or
QQSTRING_START instead of having two similar rules for String.

This is simpler and avoids having to use an untyped mid-rule action
component to copy FORMAT at the top of the stack before QQString, and
having to use jv_free($<literal>3) instead of jv_free($1) just to make
bison not complain about the "unused" mid-rule component.
2023-08-04 08:21:25 +09:00
Emanuele Torre 96263f2e4b Don't unnecessarily ignore missing-field-initializers warnings 2023-07-31 23:16:02 -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
Emanuele Torre a6eb055c47 Fix typo in manual: "-seq" => "--seq" 2023-07-29 18:27:59 -05:00
Emanuele Torre 17889a1a85 Fix memory leak for { $foo: bar }
{ BINDING: ExpD } wasn't freeing BINDING.

Fixes https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=60865
2023-07-27 20:59:35 -05:00
Emanuele Torre 3a1ba0c42d Only care about RS when parsing with --seq
RS should only only have special meaning when parsing json-seq.
Before this fix, RS was sometimes treated as a value terminator, causing
weird results.

Bug discovered by OSS fuzz.
Ref: https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=60835
2023-07-25 16:42:32 -05:00
Emanuele Torre ae95864b6b Make sure to free default @text format on error
It seems that bison doesn't call destructors for mid-rule action
components on error, since it does not know their type.

A mid-rule action was used to allocate the "text" string used as format
by string literals without a format, which would leak on error.
This patch replaces it with a new NoFormat component of type <literal>.
Now bison will call jv_free() on that string after a syntax error.

Fixes https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=60836

  $ ./jq '"'
  jq: error: syntax error, unexpected end of file, expecting QQSTRING_TEXT or QQSTRING_INTERP_START or QQSTRING_END (Unix shell quoting issues?) at <top-level>, line 1:
  "
  jq: 1 compile error

  =================================================================
  ==1495450==ERROR: LeakSanitizer: detected memory leaks

  Direct leak of 21 byte(s) in 1 object(s) allocated from:
      #0 0x7fc21aee1359 in __interceptor_malloc /usr/src/debug/gcc/gcc/libsanitizer/asan/asan_malloc_linux.cpp:69
      #1 0x557ccb6ff608 in jv_mem_alloc src/jv_alloc.c:141

  SUMMARY: AddressSanitizer: 21 byte(s) leaked in 1 allocation(s).
2023-07-25 14:51:58 -05:00
Emanuele Torre 330559643a Run autoconf -i in the Build step of the scanbuild workflow
Fixup from 54fef09ac3
2023-07-24 18:55:48 +02:00
Emanuele Torre 8a4f246876 Remove all #define _GNU_SOURCE in source files
This is unnecessary since we are using AC_USE_SYSTEM_EXTENSIONS in
configure.ac, which makes the compiler always run with -D_GNU_SOURCE=1
when building for GNU/Linux.

Also, the  #define _GNU_SOURCE  in src/util.c, and in
src/inject_errors.c (--enable-inject-errors), were not guarded by a
#ifndef _GNU_SOURCE  so they were just triggering redefinition warnings.
2023-07-24 15:44:29 +02:00
Emanuele Torre 62d65a8a9c Fix memory leaks in --with-oniguruma=no implementation of f_match
Found by LeakSanitizer running  ./jq -n '"" | test("")'  in a
--with-oniguruma=no --enable-ubsan --enable-asan  build.
2023-07-18 14:45:20 -05:00
Emanuele Torre 7ffb7943d4 Fix signed integer overflow undefined behaviour in decNumber.c
Fixes https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=60691

  $ ./jq <<< 0E2202012202
  src/decNumber/decNumber.c:598:18: runtime error: signed integer overflow: 440402440 + 1761609760 cannot be represented in type 'int'
  0E+999999999

Fixes https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=60683

  $ ./jq <<< 5E7657765755
  src/decNumber/decNumber.c:598:18: runtime error: left shift of 765776575 by 3 places cannot be represented in type 'int'
  src/decNumber/decNumber.c:598:18: runtime error: signed integer overflow: 1531553150 + 1831245304 cannot be represented in type 'int'
  1.7976931348623157e+308
2023-07-18 14:03:40 -05:00
Emanuele Torre 15fd31de78 Define examples in the "examples" field instead of "example"
Only examples defined in the "examples" field are generated, if they are
defined in "example", they are just ignored.

Also fix a bunch of incorrect tests.
2023-07-18 12:12:50 -05:00
Emanuele Torre 01dfd8b86d Populate captures also for zero-width matches
Instead of just using {"captures":[]}.

sub functions are use captures for replacement expressions.
If we don't populate, captures for empty matches, the replacement
expression is run with an empty object as input instead of an object
containing the named captures with "" as value:

* before:

  $ jq -n '"123foo456bar" | gsub("[^a-z]*(?<x>[a-z]*)"; "Z\(.x)")'
  "ZfooZbarZnull"

* after:

  $ jq -n '"123foo456bar" | gsub("[^a-z]*(?<x>[a-z]*)"; "Z\(.x)")'
  "ZfooZbarZ"

---

I also removed a redundant

  result = NULL;
  if (result) {
    ...
  }
2023-07-18 12:12:50 -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 4b3090a9ab Make stream parser error when a non-empty object or array is used as key
Fixes #2463
2023-07-17 13:15:55 -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
Emanuele Torre 1915b689f7 Fix indices/1 and rindex/1 in case of overlapping matches in strings
Fixes #2433
2023-07-14 23:40:37 -05:00
Emanuele Torre bf4390562f Add workflow that runs tests with valgrind on linux
To hopefully detect when we introduce new memory leaks. :-)
2023-07-15 01:29:13 +02:00
Emanuele Torre 4b1ac7c95f Parse nan in JSON as NaN instead of triggering a parse error (#2712)
* Fix memory leak for has(nan)

jv_array_get() used to be responsible of freeing the input array, but
since b5c4c3d67d, it is no longer called
if the key is nan. We need to free it manually to avoid leaking the
array.

* Parse  nan  in JSON as NaN instead of triggering a parse error

Fixes #2021
2023-07-14 07:12:27 +09:00
Emanuele Torre 55249e29be Also run manpage.yml workflow on pull requests that modify the manual 2023-07-13 06:20:11 +02:00
Emanuele Torre ab2779e2e5 Mark src/decNumber/** as vendored files
This excludes those files from the Languages statistics on github.
2023-07-12 11:14:39 -05:00
Emanuele Torre c99981c5b2 Update "tests/man.test" and remove superflous whitespace in manual.yml
tests/man.test was generated incorrectly and had some duplicate tests,
the new CI workflow didn't like that.
2023-07-11 00:24:50 -05:00
Emanuele Torre c049061084 Fix build_manpage.py with newer markdown versions
The markdown python changed its API for markdown extensions.

Bumping the markdown package version in previous commits broke
docs/build_manpage.py since it was using registering an extension with
the old API.
2023-07-11 00:24:50 -05:00
Emanuele Torre 83d4dd16ad Add CI workflow to make sure the man page and man.test can be built
And that man.test is up to date.
2023-07-11 00:24:50 -05:00
Emanuele Torre 2377972dfe Use jinja2 ==3.0.2 for now
We are using some deprecated jinja2 features that have been removed in
newer versions.
2023-07-10 16:19:52 -05:00
Emanuele Torre 7fba85095b Update docs/Pipfile.lock 2023-07-10 15:23:00 -05:00
Emanuele Torre fc1bef0261 Mark generated manual files as linguist-generated=true 2023-07-10 15:23:00 -05:00
Emanuele Torre 6ca7973d49 Allow using { $__loc__ } as shorthand for { __loc__: $__loc__ }
n.b.: { $__loc__: "foo" }  is still not allowed.

$__loc__ cannot be overwritten, and it is always an object.
Using { $x: "foo" } where $x is not a string is an error, so there is
not much of a point in supporting that.
2023-07-10 12:04:54 -05:00
Emanuele Torre 193f432e08 Lex $foo as a single token, instead of using '$' IDENT
Previously, bindings were parsed as the combination of two tokens:
  '$' IDENT

This meant that using a keyword as variable name (e.g. $then, $label)
did not work.
Attempts were made to allow $keyword to work by adding some '$' Keyword
rules in the parser, but this did not allow $keyword in all places:

  jq --arg label foo -n '$label'  # ok

  jq -n '"foo" as $label | .'     # error

Treating $foo as a single token is much simpler, in my opinion.

This patch also changes how LOC is lexed: "$__loc__" instead of as
"__loc__" that gets combined with '$' in the parser.

This patch also disallows having spaces after '$' when recalling a
variable  `$ foo'  since that was probably just an unintentional side
effect of the implementation, and it was not documented.

Fixes #2675
2023-07-09 17:23:19 -05:00
Emanuele Torre c08ecbaf23 Mark src/{parser,lexer}.{c,h} as generated files in .gitattributes
This should hide edit to those files in PR diffs making PRs easier to
review.
2023-07-09 14:38:17 -05:00
Emanuele Torre cd8898d5b9 exit immediately after halt or halt_error is called
Instead of continuing to the next input, and only exiting if there are
no more inputs.

Closes #2533
2023-07-07 11:13:28 -05:00