Skip to content

Commit 7914b3d

Browse files
committed
fix: address Sonar-flagged reliability bugs
- ToStringBuilder: release the ThreadLocal registry entry once it unwinds back to empty instead of retaining it for the thread's lifetime (S5164). - IndexData/UsageMap: mask promoted byte values with & 0xFF before the bit-test shift, making the sign-extension safety explicit (S3034). - IndexPageCache.updateEntry: getParentPage() can return null; fail fast instead of silently wrapping null into a CacheDataPage that would NPE downstream (javabugs:S2259). - AbstractBaseTest: merge the two @beforeeach methods into one, since JUnit doesn't guarantee their relative execution order (S8745). A few other Sonar bug findings (ThreadLocal reuse buffer in GeneralLegacyIndexCodes, BigDecimal(double, MathContext) in NumberFormatter, Duration.between(LocalDateTime) in ColumnImpl) are intentional and documented, resolved as Won't Fix in SonarCloud rather than changed in code.
1 parent 0c4aaad commit 7914b3d

5 files changed

Lines changed: 16 additions & 9 deletions

File tree

‎src/main/java/io/github/spannm/jackcess/impl/IndexData.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1162,7 +1162,7 @@ protected void readDataPage(DataPage dataPage) throws IOException {
11621162
for (int i = 0; i < entryMaskLength; i++) {
11631163
byte entryMask = buffer.get(entryMaskPos + i);
11641164
for (int j = 0; j < 8; j++) {
1165-
if ((entryMask & 1 << j) != 0) {
1165+
if ((entryMask & 0xFF & 1 << j) != 0) {
11661166
int length = i * 8 + j - lastStart;
11671167
buffer.position(entryPos + lastStart);
11681168

‎src/main/java/io/github/spannm/jackcess/impl/IndexPageCache.java‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,14 @@ private Entry updateEntry(CacheDataPage cacheDataPage, int entryIdx, Entry newEn
309309

310310
// note, it's slightly ucky, but we need to load the parent page before we
311311
// start mucking with our entries because our parent may use our entries.
312-
CacheDataPage parentDataPage = !dpMain.isRoot() ? new CacheDataPage(dpMain.getParentPage()) : null;
312+
CacheDataPage parentDataPage = null;
313+
if (!dpMain.isRoot()) {
314+
DataPageMain parentMain = dpMain.getParentPage();
315+
if (parentMain == null) {
316+
throw new IllegalStateException(withErrorContext("Could not find parent of non-root index page " + dpMain.pageNumber));
317+
}
318+
parentDataPage = new CacheDataPage(parentMain);
319+
}
313320

314321
Entry oldLastEntry = dpExtra.entryView.getLast();
315322
Entry oldEntry = null;

‎src/main/java/io/github/spannm/jackcess/impl/UsageMap.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ protected void processMap(ByteBuffer buffer, int bufferStartPage) {
256256
byte b = buffer.get();
257257
if (b != (byte) 0) {
258258
for (int i = 0; i < 8; i++) {
259-
if ((b & 1 << i) != 0) {
259+
if ((b & 0xFF & 1 << i) != 0) {
260260
int pageNumberOffset = byteCount * 8 + i + bufferStartPage;
261261
int pageNumber = bitIndexToPageNumber(pageNumberOffset, PageChannel.INVALID_PAGE_NUMBER);
262262
if (!isPageWithinRange(pageNumber)) {

‎src/main/java/io/github/spannm/jackcess/util/ToStringBuilder.java‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,10 @@ void appendInternal(StringBuilder _buffer, String _fieldName, Object _value) {
172172
}
173173
} finally {
174174
registry.remove(_value); // unregister object
175+
if (registry.isEmpty()) {
176+
// avoid retaining an (empty) Set in this thread's ThreadLocal storage once unwound
177+
OBJ_REGISTRY.remove();
178+
}
175179
}
176180
}
177181

‎src/test/java/io/github/spannm/jackcess/test/AbstractBaseTest.java‎

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,6 @@ protected static final Logger getStaticLogger() {
7070
return Logger.getLogger(AbstractBaseTest.class.getName());
7171
}
7272

73-
@BeforeEach
74-
public final void setTestMethodName(TestInfo _testInfo) {
75-
lastTestInfo = _testInfo;
76-
}
77-
7873
protected final String getTestMethodName() {
7974
if (lastTestInfo != null && lastTestInfo.getTestClass().isPresent()) {
8075
return lastTestInfo.getTestClass().get().getName() + '.' + lastTestInfo.getTestMethod().get().getName();
@@ -88,7 +83,8 @@ protected final String getShortTestMethodName() {
8883
}
8984

9085
@BeforeEach
91-
public final void logTestBegin(TestInfo _testInfo) {
86+
public final void beforeEachTest(TestInfo _testInfo) {
87+
lastTestInfo = _testInfo;
9288
if (!_testInfo.getTestMethod().isPresent() || _testInfo.getDisplayName().startsWith(_testInfo.getTestMethod().get().getName())) {
9389
getLogger().log(Level.FINE, ">>>> TEST: {0} <<<<", _testInfo.getDisplayName());
9490
} else {

0 commit comments

Comments
 (0)