Skip to content

Commit ee51b85

Browse files
authored
Merge pull request #793 from LebedevRI/codechecker
Static analysis fixes
2 parents 24001e1 + 5e6d3bc commit ee51b85

File tree

3 files changed

+64
-24
lines changed

3 files changed

+64
-24
lines changed

.github/workflows/CI-linux.yml

+39-15
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ jobs:
184184
timeout-minutes: 1
185185
run: |
186186
set -xe
187-
git config --global --add safe.directory "$GITHUB_WORKSPACE"
187+
git config --global --add safe.directory "${GITHUB_WORKSPACE}"
188188
- name: Fetch/Checkout RawSpeed git repo
189189
timeout-minutes: 1
190190
uses: actions/checkout@v4
@@ -396,24 +396,36 @@ jobs:
396396
BUILD_DIR: ${{ github.workspace }}/rawspeed-build
397397
CC: ${{ inputs.compiler-CC }}
398398
CXX: ${{ inputs.compiler-CXX }}
399+
id: CC-CSA-plain
399400
run: |
400401
set -xe
401402
. "$PWD/codechecker/venv/bin/activate"
402403
export PATH="$PWD/codechecker/build/CodeChecker/bin:$PATH"
403404
set +e
404-
CodeChecker check --logfile "${BUILD_DIR}/compile_commands.json" --analyzers clangsa --jobs "$(nproc --all)" --file "${SRC_DIR}/*" --disable deadcode.DeadStores -o "${BUILD_DIR}/codechecker_results"
405+
CodeChecker check --logfile "${BUILD_DIR}/compile_commands.json" --analyzers clangsa --jobs "$(nproc --all)" --file "${SRC_DIR}/*" --disable deadcode.DeadStores --disable alpha.unix.Stream -o "${BUILD_DIR}/codechecker_results"
405406
bugs=$?
406-
CodeChecker parse -e html "${BUILD_DIR}/codechecker_results" -o "${BUILD_DIR}/codechecker_report"
407+
CodeChecker parse -e sarif "${BUILD_DIR}/codechecker_results" -o "${GITHUB_WORKSPACE}/codechecker_report.json"
408+
CodeChecker parse -e html "${BUILD_DIR}/codechecker_results" -o "${GITHUB_WORKSPACE}/codechecker_report"
407409
set -xe
408-
tar -cvJ -f "$GITHUB_WORKSPACE/codechecker_report.tar.xz" -C "${BUILD_DIR}" "codechecker_report"
409410
[ $bugs -eq 0 ] || exit 1
410-
- name: Upload results of clang static analysis (plain mode)
411+
- name: Upload results of clang static analysis [SARIF] (plain mode)
411412
timeout-minutes: 1
412-
if: failure()
413+
if: inputs.flavor == 'ClangStaticAnalysis' && failure() && steps.CC-CSA-plain.conclusion == 'failure'
414+
uses: github/codeql-action/upload-sarif@v3
415+
with:
416+
sarif_file: "${{ github.workspace }}/codechecker_report.json"
417+
checkout_path: "${{ github.workspace }}/rawspeed"
418+
category: CodeChecker-CSA-plain
419+
- name: Upload results of clang static analysis [HTML] (plain mode)
420+
timeout-minutes: 1
421+
if: inputs.flavor == 'ClangStaticAnalysis' && failure() && steps.CC-CSA-plain.conclusion == 'failure'
413422
uses: actions/upload-artifact@v4
414423
with:
415-
name: codechecker_report.tar.xz
416-
path: ${{ github.workspace }}/codechecker_report.tar.xz
424+
name: codechecker_report
425+
path: ${{ github.workspace }}/codechecker_report/
426+
if-no-files-found: error
427+
compression-level: 9
428+
overwrite: true
417429
- name: Perform clang static analysis (CTU mode)
418430
timeout-minutes: 25
419431
if: inputs.flavor == 'ClangCTUStaticAnalysis'
@@ -422,24 +434,36 @@ jobs:
422434
BUILD_DIR: ${{ github.workspace }}/rawspeed-build
423435
CC: ${{ inputs.compiler-CC }}
424436
CXX: ${{ inputs.compiler-CXX }}
437+
id: CC-CSA-CTU
425438
run: |
426439
set -xe
427440
. "$PWD/codechecker/venv/bin/activate"
428441
export PATH="$PWD/codechecker/build/CodeChecker/bin:$PATH"
429442
set +e
430-
CodeChecker check --ctu --ctu-reanalyze-on-failure --logfile "${BUILD_DIR}/compile_commands.json" --analyzers clangsa --jobs "$(nproc --all)" --file "${SRC_DIR}/*" --disable deadcode.DeadStores -o "${BUILD_DIR}/codechecker_ctu_results"
443+
CodeChecker check --ctu --ctu-reanalyze-on-failure --logfile "${BUILD_DIR}/compile_commands.json" --analyzers clangsa --jobs "$(nproc --all)" --file "${SRC_DIR}/*" --disable deadcode.DeadStores --disable alpha.unix.Stream -o "${BUILD_DIR}/codechecker_ctu_results"
431444
bugs=$?
432-
CodeChecker parse -e html "${BUILD_DIR}/codechecker_ctu_results" -o "${BUILD_DIR}/codechecker_ctu_report"
445+
CodeChecker parse -e sarif "${BUILD_DIR}/codechecker_ctu_results" -o "${GITHUB_WORKSPACE}/codechecker_ctu_report.json"
446+
CodeChecker parse -e html "${BUILD_DIR}/codechecker_ctu_results" -o "${GITHUB_WORKSPACE}/codechecker_ctu_report"
433447
set -xe
434-
tar -cvJ -f "$GITHUB_WORKSPACE/codechecker_ctu_report.tar.xz" -C "${BUILD_DIR}" "codechecker_ctu_report"
435448
[ $bugs -eq 0 ] || exit 1
436-
- name: Upload results of clang static analysis (CTU mode)
449+
- name: Upload results of clang static analysis [SARIF] (plain mode)
450+
timeout-minutes: 1
451+
if: inputs.flavor == 'ClangCTUStaticAnalysis' && failure() && steps.CC-CSA-CTU.conclusion == 'failure'
452+
uses: github/codeql-action/upload-sarif@v3
453+
with:
454+
sarif_file: "${{ github.workspace }}/codechecker_ctu_report.json"
455+
checkout_path: "${{ github.workspace }}/rawspeed"
456+
category: CodeChecker-CSA-ctu
457+
- name: Upload results of clang static analysis [HTML] (CTU mode)
437458
timeout-minutes: 1
459+
if: inputs.flavor == 'ClangCTUStaticAnalysis' && failure() && steps.CC-CSA-CTU.conclusion == 'failure'
438460
uses: actions/upload-artifact@v4
439-
if: failure()
440461
with:
441-
name: codechecker_ctu_report.tar.xz
442-
path: ${{ github.workspace }}/codechecker_ctu_report.tar.xz
462+
name: codechecker_ctu_report
463+
path: ${{ github.workspace }}/codechecker_ctu_report/
464+
if-no-files-found: error
465+
compression-level: 9
466+
overwrite: true
443467
- name: Perform CodeQL static analysis
444468
timeout-minutes: 11
445469
if: inputs.flavor == 'CodeQLAnalysis'

src/librawspeed/io/FileReader.cpp

+14-9
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,12 @@ FileReader::readFile() const {
6060
if (file == nullptr)
6161
ThrowFIE("Could not open file \"%s\".", fileName);
6262

63-
fseek(file.get(), 0, SEEK_END);
63+
if (fseek(file.get(), 0, SEEK_END) == -1)
64+
ThrowFIE("Could not rewind to the end of the file");
65+
6466
const auto size = ftell(file.get());
67+
if (size == -1)
68+
ThrowFIE("Could not obtain the file size");
6569

6670
if (size <= 0)
6771
ThrowFIE("File is 0 bytes.");
@@ -72,20 +76,21 @@ FileReader::readFile() const {
7276

7377
fileSize = size;
7478

75-
fseek(file.get(), 0, SEEK_SET);
79+
if (fseek(file.get(), 0, SEEK_SET) == -1)
80+
ThrowFIE("Could not rewind to the beginning of the file");
7681

7782
auto dest = std::make_unique<std::vector<
7883
uint8_t,
7984
DefaultInitAllocatorAdaptor<uint8_t, AlignedAllocator<uint8_t, 16>>>>(
8085
fileSize);
8186

82-
if (auto bytes_read = fread(dest->data(), 1, fileSize, file.get());
83-
fileSize != bytes_read) {
84-
ThrowFIE("Could not read file, %s.",
85-
feof(file.get()) ? "reached end-of-file"
86-
: (ferror(file.get()) ? "file reading error"
87-
: "unknown problem"));
88-
}
87+
auto bytes_read = fread(dest->data(), 1, fileSize, file.get());
88+
if (ferror(file.get()))
89+
ThrowFIE("Could not read file, file reading error");
90+
if (feof(file.get()))
91+
ThrowFIE("Could not read file, reached end-of-file");
92+
if (fileSize != bytes_read)
93+
ThrowFIE("Could not read file, unknown problem");
8994

9095
#else // __unix__
9196

src/utilities/rstest/rstest.cpp

+11
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "adt/Casts.h"
2626
#include "adt/DefaultInitAllocatorAdaptor.h"
2727
#include "adt/NotARational.h"
28+
#include "io/FileIOException.h"
2829
#include "md5.h"
2930
#include <array>
3031
#include <bit>
@@ -280,6 +281,8 @@ void writePPM(const RawImage& raw, const std::string& fn) {
280281

281282
// Write PPM header
282283
fprintf(f.get(), "%s\n%d %d\n65535\n", format.c_str(), width, height);
284+
if (ferror(f.get()))
285+
ThrowFIE("Could not write file");
283286

284287
width *= raw->getCpp();
285288

@@ -291,6 +294,8 @@ void writePPM(const RawImage& raw, const std::string& fn) {
291294
img(y, x) = getU16BE(&img(y, x));
292295

293296
fwrite(&img(y, 0), sizeof(decltype(img)::value_type), width, f.get());
297+
if (ferror(f.get()))
298+
ThrowFIE("Could not write file");
294299
}
295300
}
296301

@@ -304,6 +309,8 @@ void writePFM(const RawImage& raw, const std::string& fn) {
304309

305310
// Write PFM header. if scale < 0, it is little-endian, if >= 0 - big-endian
306311
int len = fprintf(f.get(), "%s\n%d %d\n-1.0", format.c_str(), width, height);
312+
if (ferror(f.get()))
313+
ThrowFIE("Could not write file");
307314

308315
// make sure that data starts at aligned offset. for sse
309316
static const auto dataAlignment = 16;
@@ -323,6 +330,8 @@ void writePFM(const RawImage& raw, const std::string& fn) {
323330

324331
// and actually write padding + new line
325332
len += fprintf(f.get(), "%0*i\n", padding, 0);
333+
if (ferror(f.get()))
334+
ThrowFIE("Could not write file");
326335
assert(paddedLen == len);
327336

328337
// did we write a multiple of an alignment value?
@@ -343,6 +352,8 @@ void writePFM(const RawImage& raw, const std::string& fn) {
343352
img(row_in, x) = std::bit_cast<float>(getU32LE(&img(row_in, x)));
344353

345354
fwrite(&img(row_in, 0), sizeof(decltype(img)::value_type), width, f.get());
355+
if (ferror(f.get()))
356+
ThrowFIE("Could not write file");
346357
}
347358
}
348359

0 commit comments

Comments
 (0)