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
41 changes: 39 additions & 2 deletions service/diskoperation/DeviceStorage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -608,9 +608,9 @@
return;
}

void DeviceStorage::updateForHWDevice(const QString &devicePath)
void DeviceStorage::updateForHWDevice(const QString &/*devicePath*/)

Check warning on line 611 in service/diskoperation/DeviceStorage.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

The function 'updateForHWDevice' is never used.
{
if (m_model != Utils::readContent("/proc/bootdevice/product_name").trimmed())
if (Utils::readContent("/proc/bootdevice/product_name").trimmed().isEmpty())
return;

// m_firmwareVersion
Expand All @@ -622,6 +622,43 @@
// hide mode and vendor
m_model = "";
m_vendor = "";

// Normalize disk size
if (!m_size.isEmpty()) {
// Convert size string to bytes
qint64 bytes = 0;
QString sizeStr = m_size.toLower();

if (sizeStr.contains("bytes")) {
sizeStr = sizeStr.split("bytes").first().trimmed();
bytes = sizeStr.toLongLong();
Comment on lines +632 to +634
Copy link

Choose a reason for hiding this comment

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

issue (bug_risk): Potential parsing issue for non-numeric byte strings.

Add validation to handle cases where non-numeric characters precede 'bytes', as toLongLong() may not parse these correctly.

Comment on lines +632 to +634
Copy link

Choose a reason for hiding this comment

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

issue: No error handling for non-numeric size strings.

If sizeStr is not a valid number, toLongLong() returns 0, which could cause incorrect results. Please add validation for the conversion.

} else {
// Handle GB/TB directly
double value = sizeStr.split(" ").first().toDouble();
Copy link

Choose a reason for hiding this comment

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

suggestion: No fallback for unrecognized units or malformed size strings.

Consider adding error handling or logging for unrecognized or malformed units to prevent silent failures.

if (sizeStr.contains("gb")) {
bytes = value * 1000 * 1000 * 1000;
} else if (sizeStr.contains("tb")) {
bytes = value * 1000 * 1000 * 1000 * 1000;
}
Comment on lines +636 to +642
Copy link

Choose a reason for hiding this comment

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

suggestion: Potential locale issue with toDouble conversion.

If size strings may use different decimal separators, explicitly set the locale in toDouble() or validate the input format to avoid conversion errors.

Suggested change
// Handle GB/TB directly
double value = sizeStr.split(" ").first().toDouble();
if (sizeStr.contains("gb")) {
bytes = value * 1000 * 1000 * 1000;
} else if (sizeStr.contains("tb")) {
bytes = value * 1000 * 1000 * 1000 * 1000;
}
// Handle GB/TB directly
QLocale locale(QLocale::C); // Use C locale for consistent decimal separator
double value = locale.toDouble(sizeStr.split(" ").first());
if (sizeStr.contains("gb")) {
bytes = value * 1000 * 1000 * 1000;
} else if (sizeStr.contains("tb")) {
bytes = value * 1000 * 1000 * 1000 * 1000;
}

Comment on lines +635 to +642
Copy link

Choose a reason for hiding this comment

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

suggestion: No handling for MB or other units.

Consider updating the logic to support MB and other units if they may be encountered.

Suggested change
} else {
// Handle GB/TB directly
double value = sizeStr.split(" ").first().toDouble();
if (sizeStr.contains("gb")) {
bytes = value * 1000 * 1000 * 1000;
} else if (sizeStr.contains("tb")) {
bytes = value * 1000 * 1000 * 1000 * 1000;
}
} else {
// Handle KB/MB/GB/TB directly
QStringList parts = sizeStr.split(" ");
if (parts.size() == 2) {
double value = parts[0].toDouble();
QString unit = parts[1].trimmed();
if (unit == "kb") {
bytes = value * 1000;
} else if (unit == "mb") {
bytes = value * 1000 * 1000;
} else if (unit == "gb") {
bytes = value * 1000 * 1000 * 1000;
} else if (unit == "tb") {
bytes = value * 1000 * 1000 * 1000 * 1000;
} else {
// Unknown unit, fallback to value as bytes
bytes = value;
}
}

}

// Convert to standardized format
if (bytes > 0) {
// Convert to GB and round to standard sizes
int gb = qRound(bytes / (1000.0 * 1000 * 1000));
if (gb < 200) {
return;
} else if (gb <= 300) {
m_size = "256 GB";
} else if (gb <= 600) {
m_size = "512 GB";
} else if (gb <= 1200){
m_size = "1 TB";
} else if (gb <= 2200) {
m_size = "2 TB";
}
Comment on lines +646 to +659
Copy link

Choose a reason for hiding this comment

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

suggestion: No fallback for sizes above 2 TB.

For disk sizes over 2 TB, m_size remains unset. Please add a fallback for these cases or clearly document this behavior.

Suggested change
if (bytes > 0) {
// Convert to GB and round to standard sizes
int gb = qRound(bytes / (1000.0 * 1000 * 1000));
if (gb < 200) {
return;
} else if (gb <= 300) {
m_size = "256 GB";
} else if (gb <= 600) {
m_size = "512 GB";
} else if (gb <= 1200){
m_size = "1 TB";
} else if (gb <= 2200) {
m_size = "2 TB";
}
if (bytes > 0) {
// Convert to GB and round to standard sizes
int gb = qRound(bytes / (1000.0 * 1000 * 1000));
if (gb < 200) {
return;
} else if (gb <= 300) {
m_size = "256 GB";
} else if (gb <= 600) {
m_size = "512 GB";
} else if (gb <= 1200){
m_size = "1 TB";
} else if (gb <= 2200) {
m_size = "2 TB";
} else {
m_size = "Over 2 TB";
}

}
}
}

void DeviceStorage::getMapInfoFromSmartctl(QMap<QString, QString> &mapInfo, const QString &info, const QString &ch)
Expand Down