Skip to content

Commit b664fef

Browse files
authored
Merge pull request #204 from cconlon/spotbugs
Add SpotBugs static analysis, fix bugs found
2 parents bb6d1bf + 440e7bf commit b664fef

21 files changed

Lines changed: 1047 additions & 67 deletions

.github/workflows/android_gradle.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,29 +65,29 @@ jobs:
6565
path: |
6666
~/.android/avd/*
6767
~/.android/adb*
68-
key: avd-wolfcryptjni-30-x86_64-atd-v1
68+
key: avd-wolfcryptjni-30-x86_64-google_apis-v1
6969

7070
# Create AVD and generate snapshot for caching
7171
- name: Create AVD and generate snapshot
7272
if: steps.avd-cache.outputs.cache-hit != 'true'
73-
uses: reactivecircus/android-emulator-runner@v2
73+
uses: reactivecircus/android-emulator-runner@v2.37.0
7474
with:
7575
api-level: 30
7676
arch: x86_64
77-
target: aosp_atd
77+
target: google_apis
7878
force-avd-creation: false
7979
emulator-options: -no-window -gpu swiftshader_indirect -noaudio -no-boot-anim -camera-back none
8080
disable-animations: true
8181
script: echo "Generated AVD snapshot for caching"
8282

8383
# Run instrumented tests on Android emulator
8484
- name: Run Android Instrumented Tests
85-
uses: reactivecircus/android-emulator-runner@v2
85+
uses: reactivecircus/android-emulator-runner@v2.37.0
8686
timeout-minutes: 15
8787
with:
8888
api-level: 30
8989
arch: x86_64
90-
target: aosp_atd
90+
target: google_apis
9191
force-avd-creation: false
9292
emulator-options: -no-snapshot-save -no-window -gpu swiftshader_indirect -noaudio -no-boot-anim -camera-back none
9393
disable-animations: true

.github/workflows/spotbugs.yml

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
name: SpotBugs Static Analysis
2+
3+
on:
4+
pull_request:
5+
branches: [ '*' ]
6+
paths:
7+
- '**/*.java'
8+
- 'build.xml'
9+
- 'spotbugs-exclude.xml'
10+
11+
jobs:
12+
spotbugs:
13+
runs-on: ubuntu-latest
14+
name: Run SpotBugs
15+
16+
steps:
17+
- name: Checkout code
18+
uses: actions/checkout@v4
19+
20+
- name: Set up Java 17
21+
uses: actions/setup-java@v4
22+
with:
23+
distribution: 'temurin'
24+
java-version: '17'
25+
26+
- name: Set up Ant
27+
run: |
28+
sudo apt-get update
29+
sudo apt-get install -y ant
30+
31+
- name: Download JUnit
32+
run: |
33+
mkdir -p /tmp/junit
34+
wget -q -P /tmp/junit \
35+
"https://repo1.maven.org/maven2/junit/junit/4.13.2/junit-4.13.2.jar"
36+
echo "JUNIT_HOME=/tmp/junit" >> $GITHUB_ENV
37+
38+
- name: Download and set up SpotBugs
39+
run: |
40+
SPOTBUGS_VERSION="4.9.3"
41+
wget -q "https://github.com/spotbugs/spotbugs/releases/download/${SPOTBUGS_VERSION}/spotbugs-${SPOTBUGS_VERSION}.tgz"
42+
tar xzf "spotbugs-${SPOTBUGS_VERSION}.tgz"
43+
echo "SPOTBUGS_HOME=${GITHUB_WORKSPACE}/spotbugs-${SPOTBUGS_VERSION}" >> $GITHUB_ENV
44+
45+
- name: Run SpotBugs
46+
run: |
47+
ant spotbugs
48+
49+
- name: Check for SpotBugs warnings
50+
if: always()
51+
run: |
52+
REPORT="build/reports/spotbugs.html"
53+
if [ ! -f "$REPORT" ]; then
54+
echo "SpotBugs report not found"
55+
exit 1
56+
fi
57+
58+
# Extract warning count from report
59+
COUNT=$(python3 -c "
60+
import re
61+
with open('$REPORT', 'r') as f:
62+
content = f.read()
63+
m = re.search(r'<b>Total Warnings</b>.*?<b>(\d+)</b>', content, re.DOTALL)
64+
print(m.group(1) if m else '0')
65+
")
66+
67+
if [ "$COUNT" -eq 0 ]; then
68+
echo "=================================="
69+
echo "SpotBugs: 0 warnings found"
70+
echo "=================================="
71+
exit 0
72+
fi
73+
74+
echo "=================================="
75+
echo "SpotBugs: $COUNT warning(s) found"
76+
echo "=================================="
77+
echo ""
78+
79+
# Print each warning
80+
python3 -c "
81+
import re
82+
with open('$REPORT', 'r') as f:
83+
content = f.read()
84+
pattern = r'priority-(\d)\">([\w]+)</span>.*?</td>\s*<td>(.*?)</td>.*?Bug type (\w+).*?</p>'
85+
warnings = re.findall(pattern, content, re.DOTALL)
86+
for pri, code, desc, full_type in warnings:
87+
d = re.sub(r'<[^>]+>', '', desc).strip()
88+
loc = ''
89+
m2 = re.search(r'At ([\w.]+:\[line \d+\])', d)
90+
print(f'[P{pri}] {full_type}')
91+
print(f' {d[:200]}')
92+
print()
93+
"
94+
95+
exit 1

build.xml

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,5 +534,82 @@
534534
description="Build library JAR (JNI + JCE classes)">
535535
</target>
536536

537+
<!-- SpotBugs Static Analysis:
538+
Run with 'ant spotbugs' to generate report.
539+
Requires SPOTBUGS_HOME environment variable to be set.
540+
Download from: https://spotbugs.github.io/
541+
Report generated at: build/reports/spotbugs.html -->
542+
543+
<!-- Only define SpotBugs task if SPOTBUGS_HOME is set -->
544+
<condition property="spotbugs.available">
545+
<and>
546+
<isset property="env.SPOTBUGS_HOME"/>
547+
<available
548+
file="${env.SPOTBUGS_HOME}/lib/spotbugs-ant.jar"/>
549+
</and>
550+
</condition>
551+
552+
<!-- Check if Java version is 11+ (required by SpotBugs 4.8+) -->
553+
<condition property="spotbugs.java.compatible">
554+
<not>
555+
<or>
556+
<equals arg1="${ant.java.version}" arg2="1.5"/>
557+
<equals arg1="${ant.java.version}" arg2="1.6"/>
558+
<equals arg1="${ant.java.version}" arg2="1.7"/>
559+
<equals arg1="${ant.java.version}" arg2="1.8"/>
560+
<equals arg1="${ant.java.version}" arg2="9"/>
561+
<equals arg1="${ant.java.version}" arg2="10"/>
562+
</or>
563+
</not>
564+
</condition>
565+
566+
<target name="spotbugs-taskdef" if="spotbugs.available">
567+
<taskdef
568+
resource="edu/umd/cs/findbugs/anttask/tasks.properties">
569+
<classpath>
570+
<fileset dir="${env.SPOTBUGS_HOME}/lib"
571+
includes="*.jar"/>
572+
</classpath>
573+
</taskdef>
574+
</target>
575+
576+
<target name="spotbugs-check">
577+
<fail unless="spotbugs.java.compatible">
578+
SpotBugs requires Java 11 or later to run.
579+
580+
Current Java version: ${ant.java.version}
581+
582+
To fix this, run ant with Java 11+:
583+
export JAVA_HOME=/path/to/jdk11
584+
ant spotbugs
585+
</fail>
586+
<fail unless="spotbugs.available">
587+
SpotBugs not found. Please set SPOTBUGS_HOME.
588+
589+
To install SpotBugs:
590+
1. Download from https://spotbugs.github.io/
591+
2. Extract (e.g., /opt/spotbugs-4.8.6)
592+
3. Set SPOTBUGS_HOME=/opt/spotbugs-4.8.6
593+
4. Run 'ant spotbugs'
594+
</fail>
595+
</target>
596+
597+
<target name="spotbugs"
598+
depends="build-jce-debug, spotbugs-check, spotbugs-taskdef"
599+
description="Run SpotBugs static analysis">
600+
<mkdir dir="${reports.dir}"/>
601+
<spotbugs home="${env.SPOTBUGS_HOME}"
602+
output="html"
603+
outputFile="${reports.dir}/spotbugs.html"
604+
effort="max"
605+
reportLevel="medium"
606+
excludeFilter="spotbugs-exclude.xml"
607+
failOnError="false">
608+
<sourcePath path="${src.dir}"/>
609+
<class location="${lib.dir}/wolfcrypt-jni.jar"/>
610+
</spotbugs>
611+
<echo message="SpotBugs report: ${reports.dir}/spotbugs.html"/>
612+
</target>
613+
537614
</project>
538615

jni/include/com_wolfssl_wolfcrypt_WolfCrypt.h

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)