Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Support # comment lines in --check files #838

Merged
merged 4 commits into from Jun 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions .github/workflows/ci.yml
Expand Up @@ -251,6 +251,10 @@ jobs:
run: |
make clean test-filename-escape

- name: test-cli-comment-line
run: |
make clean test-cli-comment-line

ubuntu-cmake-unofficial:
name: Linux x64 cmake unofficial build test
runs-on: ubuntu-latest
Expand Down
4 changes: 4 additions & 0 deletions Makefile
Expand Up @@ -310,6 +310,10 @@ test-xxhsum-c: xxhsum
test-filename-escape:
$(MAKE) -C tests test_filename_escape

.PHONY: test-cli-comment-line
test-cli-comment-line:
$(MAKE) -C tests test_cli_comment_line

.PHONY: armtest
armtest: clean
@echo ---- test ARM compilation ----
Expand Down
15 changes: 15 additions & 0 deletions cli/xxhsum.c
Expand Up @@ -488,6 +488,7 @@ static int XSUM_hashFiles(const char* fnList[], int fnTotal,

typedef enum {
GetLine_ok,
GetLine_comment,
GetLine_eof,
GetLine_exceedMaxLineLength,
GetLine_outOfMemory
Expand Down Expand Up @@ -549,6 +550,7 @@ typedef struct {
/*
* Reads a line from stream `inFile`.
* Returns GetLine_ok, if it reads line successfully.
* Returns GetLine_comment, if the line is beginning with '#'.
* Returns GetLine_eof, if stream reaches EOF.
* Returns GetLine_exceedMaxLineLength, if line length is longer than MAX_LINE_LENGTH.
* Returns GetLine_outOfMemory, if line buffer memory allocation failed.
Expand Down Expand Up @@ -598,6 +600,12 @@ static GetLineResult XSUM_getLine(char** lineBuf, int* lineMax, FILE* inFile)
}

(*lineBuf)[len] = '\0';

/* Ignore comment lines, which begin with a '#' character. */
if (result == GetLine_ok && len > 0 && ((*lineBuf)[0] == '#')) {
result = GetLine_comment;
}

return result;
}

Expand Down Expand Up @@ -794,12 +802,19 @@ static void XSUM_parseFile1(ParseFileArg* XSUM_parseFileArg, int rev)
{ GetLineResult const XSUM_getLineResult = XSUM_getLine(&XSUM_parseFileArg->lineBuf,
&XSUM_parseFileArg->lineMax,
XSUM_parseFileArg->inFile);

/* Ignore comment lines */
if (XSUM_getLineResult == GetLine_comment) {
continue;
}

if (XSUM_getLineResult != GetLine_ok) {
if (XSUM_getLineResult == GetLine_eof) break;

switch (XSUM_getLineResult)
{
case GetLine_ok:
case GetLine_comment:
case GetLine_eof:
/* These cases never happen. See above XSUM_getLineResult related "if"s.
They exist just for make gcc's -Wswitch-enum happy. */
Expand Down
4 changes: 4 additions & 0 deletions tests/Makefile
Expand Up @@ -104,6 +104,10 @@ endif
test_filename_escape: $(XXHSUM)
./filename-escape.sh

.PHONY: test_cli_comment_line
test_cli_comment_line: $(XXHSUM)
$(SHELL) ./cli-comment-line.sh

.PHONY: test_sanity
test_sanity: sanity_test.c
$(CC) $(CFLAGS) $(LDFLAGS) sanity_test.c -o sanity_test$(EXT)
Expand Down
32 changes: 32 additions & 0 deletions tests/cli-comment-line.sh
@@ -0,0 +1,32 @@
#!/bin/bash

# Exit immediately if any command fails.
# https://stackoverflow.com/a/2871034
set -euxo


# Default
./xxhsum ./Makefile > ./.test.xxh
echo '# Test comment line' | cat - ./.test.xxh > temp && mv temp ./.test.xxh
./xxhsum --check ./.test.xxh

# XXH32
./xxhsum -H32 ./Makefile > ./.test.xxh32
echo '# Test comment line' | cat - ./.test.xxh32 > temp && mv temp ./.test.xxh32
./xxhsum --check ./.test.xxh32

# XXH64
./xxhsum -H64 ./Makefile > ./.test.xxh64
echo '# Test comment line' | cat - ./.test.xxh64 > temp && mv temp ./.test.xxh64
./xxhsum --check ./.test.xxh64

# XXH128
./xxhsum -H128 ./Makefile > ./.test.xxh128
echo '# Test comment line' | cat - ./.test.xxh128 > temp && mv temp ./.test.xxh128
./xxhsum --check ./.test.xxh128


rm ./.test.xxh
rm ./.test.xxh32
rm ./.test.xxh64
rm ./.test.xxh128