Skip to content

Commit

Permalink
test gufi_stat_bin format width without format sequences
Browse files Browse the repository at this point in the history
  • Loading branch information
calccrypto committed Feb 14, 2025
1 parent cfa2550 commit 3e90213
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
9 changes: 8 additions & 1 deletion src/gufi_stat.c
Original file line number Diff line number Diff line change
Expand Up @@ -230,12 +230,14 @@ static int print_callback(void * args, int count, char **data, char **columns) {
}
}
else {
const char *start = f;

f++;

/* if the first character starts a number */
int width = 0;
if (*f && (((*f == '-') || (*f == '+') ||
(('0' <= *f) && (*f <= '9'))))) {
(('0' <= *f) && (*f <= '9'))))) {
int multiplier = 1;
if (*f == '-') {
multiplier = -1;
Expand All @@ -254,6 +256,11 @@ static int print_callback(void * args, int count, char **data, char **columns) {
width *= multiplier;
}

if (!*f) {
fprintf(stderr, "gufi_stat: bad format sequence: \"%s\"\n", start);
continue;
}

char format[MAXPATH] = "%";
if (width) {
SNPRINTF(format, sizeof(format), "%%%d", width);
Expand Down
10 changes: 10 additions & 0 deletions test/regression/gufi_stat_bin.expected
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,18 @@ prefix/unusual#? directory
'prefix/unusual#? directory ,/unusual, name?#' prefix/unusual#? directory ,/unusual, name?# 'prefix/unusual#? directory ,/unusual, name?#' 664 -rw-rw-r-- ? ? 81b4 regular file 15 - 0 1970-01-01 00:00:15 +0000 15 1970-01-01 00:00:15 +0000 15

# bad print format specifier
$ gufi_stat_bin -f %e prefix
?

$ gufi_stat_bin -f %-60 prefix
gufi_stat: bad format sequence: "%-60"

$ gufi_stat_bin -f %+ prefix
gufi_stat: bad format sequence: "%+"

$ gufi_stat_bin -f % prefix
gufi_stat: bad format sequence: "%"

'prefix/directory/subdirectory/directory_symlink' -> 'prefix/directory/subdirectory' prefix/directory/subdirectory/directory_symlink 777 lrwxrwxrwx -- -- ? ? a1ff symbolic link 'prefix/directory/subdirectory/directory_symlink' -> 'prefix/directory/subdirectory' - 0 1970-01-01 00:00:04 +0000 4 1970-01-01 00:00:04 +0000 4
'prefix/file_symlink' -> 'prefix/1KB' prefix/file_symlink 777 lrwxrwxrwx -- -- ? ? a1ff symbolic link 'prefix/file_symlink' -> 'prefix/1KB' - 0 1970-01-01 00:00:09 +0000 9 1970-01-01 00:00:09 +0000 9

Expand Down
6 changes: 4 additions & 2 deletions test/regression/gufi_stat_bin.sh.in
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,10 @@ echo "# custom print format"
echo

echo "# bad print format specifier"
"${GUFI_STAT_BIN}" -f '%e\n' "${INDEXROOT}"
echo
run_no_sort "${GUFI_STAT_BIN}" -f '%e' "${INDEXROOT}"; echo
run_no_sort "${GUFI_STAT_BIN}" -f '%-60' "${INDEXROOT}"
run_no_sort "${GUFI_STAT_BIN}" -f '%+' "${INDEXROOT}"
run_no_sort "${GUFI_STAT_BIN}" -f '%' "${INDEXROOT}"

for entry in "${symlinks[@]}"
do
Expand Down

0 comments on commit 3e90213

Please sign in to comment.