Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 deletions basestruct/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@
#include <algorithm>
#include <math.h>
#include <QFile>
#include <QDir>

Check warning on line 18 in basestruct/utils.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QDir> not found. Please note: Cppcheck does not need standard library headers to get proper results.

#include <cstdlib>

Check warning on line 20 in basestruct/utils.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <cstdlib> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <climits>

Check warning on line 21 in basestruct/utils.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <climits> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <cstring>

Check warning on line 22 in basestruct/utils.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <cstring> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <errno.h>

Check warning on line 23 in basestruct/utils.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <errno.h> not found. Please note: Cppcheck does not need standard library headers to get proper results.

Utils::Utils()
{
Expand Down Expand Up @@ -473,16 +474,20 @@
QString Utils::formatSize(Sector sectors, Byte_Value sectorSize)
{
QString res;
if ((sectors * sectorSize) < KIBIBYTE) {
Byte_Value totalBytes;
if (__builtin_mul_overflow(sectors, sectorSize, &totalBytes)) {
totalBytes = LLONG_MAX;
}
if (totalBytes < KIBIBYTE) {
res = res.setNum(sectorToUnit(sectors, sectorSize, UNIT_BYTE), 'f', 2);
res.append(" B");
} else if ((sectors * sectorSize) < MEBIBYTE) {
} else if (totalBytes < MEBIBYTE) {
res = res.setNum(sectorToUnit(sectors, sectorSize, UNIT_KIB), 'f', 2);
res.append(" KiB");
} else if ((sectors * sectorSize) < GIBIBYTE) {
} else if (totalBytes < GIBIBYTE) {
res = res.setNum(sectorToUnit(sectors, sectorSize, UNIT_MIB), 'f', 2);
res.append(" MiB");
} else if ((sectors * sectorSize) < TEBIBYTE) {
} else if (totalBytes < TEBIBYTE) {
res = res.setNum(sectorToUnit(sectors, sectorSize, UNIT_GIB), 'f', 2);
res.append(" GiB");
} else {
Expand Down Expand Up @@ -535,12 +540,20 @@

Byte_Value Utils::floorSize(Byte_Value value, Byte_Value roundingSize)
{
if (roundingSize <= 0)
return value;
return value / roundingSize * roundingSize;
}

Byte_Value Utils::ceilSize(Byte_Value value, Byte_Value roundingSize)

Check warning on line 548 in basestruct/utils.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

The function 'ceilSize' is never used.
{
return (value + roundingSize - 1LL) / roundingSize * roundingSize;
if (roundingSize <= 0)
return value;
Byte_Value numerator;
if (__builtin_add_overflow(value, roundingSize - 1LL, &numerator)) {
numerator = LLONG_MAX;
}
return numerator / roundingSize * roundingSize;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Medium · Trailing bytes remain unerased

When the numerator addition overflows and roundingSize does not divide LLONG_MAX, ceilSize substitutes LLONG_MAX for the overflowing numerator, then division and multiplication round it down to a multiple below the requested value. When PartedCore::eraseFilesystemSignatures() uses that result to calculate the end of a write range, trailing bytes are not erased.

Detect an unrepresentable rounded-up size and report failure instead of returning a smaller multiple.

Prompt for AI agents
In `basestruct/utils.cpp` at line 556:

**Trailing bytes remain unerased**

When the numerator addition overflows and `roundingSize` does not divide `LLONG_MAX`, `ceilSize` substitutes `LLONG_MAX` for the overflowing numerator, then division and multiplication round it down to a multiple below the requested value. When `PartedCore::eraseFilesystemSignatures()` uses that result to calculate the end of a write range, trailing bytes are not erased.

Detect an unrepresentable rounded-up size and report failure instead of returning a smaller multiple.

}

QString Utils::createUuid()
Expand Down
Loading