Skip to content

Command substitution as an argument to xargs(1) is racy #3354

@alejandro-colomar

Description

@alejandro-colomar

For new checks and feature suggestions

Here's a snippet or screenshot that shows a potential problem:

#!/bin/bash

set -o pipefail;

find . -type f -print0 \
| xargs -0 pcre2grep -nM -f <(grepc_c -tfp strcpy || true);

Here's what shellcheck currently says:

$ shellcheck -o all script 
$ 

Here's what I wanted to see:

Process substitution as an argument to xargs(1) is racy.
xargs(1) will invoke more than one processes if the argument list is too long. Such processes will race for the output of the process substitution. Quite often, the first process will consume all of the output, and the rest will see an empty file.

A solution could be to use command substitution instead, but that needs changing the command (in this case, I removed the -f option):

#!/bin/bash

set -o pipefail;

find . -type f -print0 \
| xargs -0 pcre2grep -nM "$(grepc_c -tfp strcpy || true)";

Alternatively, use a temporary file:

#!/bin/bash

set -o pipefail;

my_file="$(mktemp -t my_file.XXXXXX)";
grepc_c -tfp strcpy >"$my_file";

find . -type f -print0 \
| xargs -0 pcre2grep -nM -f $my_file;

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions