diff --git a/Makefile.am b/Makefile.am index 75086ca3..058098f6 100644 --- a/Makefile.am +++ b/Makefile.am @@ -132,6 +132,7 @@ DIALECT_NEUTRAL_TESTS = tests/case-00-hello.bash \ tests/case-20-handle-missing-files.bash \ tests/case-20-offset-field.bash \ tests/case-20-repeat-count.bash \ + tests/case-20-uid-empty-entry.bash \ tests/case-21-exit-Q-status.bash \ tests/case-22-empty-process-name.bash \ tests/case-23-multi-address-hostname.bash \ diff --git a/src/arg.c b/src/arg.c index 9bb09b9f..2005051c 100644 --- a/src/arg.c +++ b/src/arg.c @@ -2019,7 +2019,7 @@ int enter_uid(struct lsof_context *ctx, /* context */ char *s, *st; uid_t uid; - if (!us) { + if (!us || !*us) { (void)fprintf(stderr, "%s: no UIDs specified\n", Pn); return (1); } @@ -2063,6 +2063,17 @@ int enter_uid(struct lsof_context *ctx, /* context */ s++; if (j) continue; + if (!lnml) { + + /* + * An empty entry (e.g. from ",99" or "^") would otherwise be + * taken as UID 0. + */ + (void)fprintf(stderr, "%s: empty -u UID or login name: ", Pn); + safestrprt(us, stderr, 1); + err = 1; + continue; + } if (nn) { lnm[lnml++] = '\0'; if ((pw = getpwnam(lnm)) == NULL) { diff --git a/tests/case-20-uid-empty-entry.bash b/tests/case-20-uid-empty-entry.bash new file mode 100755 index 00000000..0149cdcc --- /dev/null +++ b/tests/case-20-uid-empty-entry.bash @@ -0,0 +1,19 @@ +#!/usr/bin/env bash +# An empty entry in a -u list must not be taken as UID 0. +source tests/common.bash + +{ + for spec in "" ",99999" "^"; do + output=$($lsof -u "$spec" -F u 2>/dev/null) + status=$? + if echo "$output" | grep -q '^u0$'; then + echo "lsof -u $spec selected UID 0" >> $report + exit 1 + fi + if [ $status -eq 0 ]; then + echo "lsof -u $spec should fail" >> $report + exit 1 + fi + done + exit 0 +} > $report 2>&1