String.prototype.match with a global RegExp returns [] instead of null when nothing matches

Open Beginner friendly
#1,152 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
78/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
c, javascript
Domain
backend

Research direction

Start by reading njs_string_match_multiple() in the njs source and trace where it assigns the result after the match loop. Reproduce the issue with the provided script using ./build/njs; the expected output is null for all three cases. Check the project’s relevant tests and add coverage for global expressions with no matches; done means those cases return null while existing match behavior still passes.

Written by the indexing model from the issue text.

Description

Describe the bug

String.prototype.match(re) with a global regular expression returns [] instead of null when nothing matches.

  • The bug is reproducible with the latest version of njs.
  • I minimized the code and NGINX configuration to the smallest possible to reproduce the issue.
To reproduce

Steps to reproduce the behavior:

  • JS script
console.log(JSON.stringify("abc".match(/x/g)));
console.log(JSON.stringify("abc".match(/(x)/gi)));
console.log(JSON.stringify("abc".match(/x/)));
  • Exact steps to reproduce the behavior

Run ./build/njs on the script above. It prints:

[]
[]
null
Expected behavior

All three cases should evaluate to null and print:

null
null
null
Your environment
  • Version of njs or specific commit: 1.0.3
  • Version of NGINX if applicable: N/A (standalone njs CLI)
  • List of other enabled nginx modules if applicable: N/A
  • OS: Linux
Additional context

njs_string_match_multiple() initializes retval to null, but sets retval to the match array after the match loop even when array->length == 0.

Credit: https://ucsbplse.github.io/

Dominant language
C
Stars
1.6k
Forks
245
Avg merge
2d 5h
Merged PRs (30d)
6

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from nginx/njs

All issues in nginx/njs

Similar issues

More C issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.