Ensure null termination with strncpy

This commit is contained in:
Alexander Babikov
2022-04-14 07:02:27 +05:00
parent 911821434e
commit ae11a2f887
2 changed files with 5 additions and 3 deletions

View File

@@ -188,7 +188,7 @@ void MediaMenu::cassetteMount(const QString& filename, bool wp) {
if (! filename.isEmpty()) {
QByteArray filenameBytes = filename.toUtf8();
strncpy(cassette_fname, filenameBytes.data(), sizeof(cassette_fname));
strncpy(cassette_fname, filenameBytes.data(), sizeof(cassette_fname) - 1);
pc_cas_set_fname(cassette, cassette_fname);
}

View File

@@ -151,7 +151,7 @@ void SettingsHarddisks::save() {
hdd[i].spt = idx.siblingAtColumn(ColumnSectors).data().toUInt();
QByteArray fileName = idx.siblingAtColumn(ColumnFilename).data(Qt::UserRole).toString().toUtf8();
strncpy(hdd[i].fn, fileName.data(), sizeof(hdd[i].fn));
strncpy(hdd[i].fn, fileName.data(), sizeof(hdd[i].fn) - 1);
hdd[i].priv = nullptr;
}
}
@@ -247,12 +247,14 @@ static void addDriveFromDialog(Ui::SettingsHarddisks* ui, const HarddiskDialog&
QByteArray fn = dlg.fileName().toUtf8();
hard_disk_t hd;
memset(&hd, 0, sizeof(hd));
hd.bus = dlg.bus();
hd.channel = dlg.channel();
hd.tracks = dlg.cylinders();
hd.hpc = dlg.heads();
hd.spt = dlg.sectors();
strncpy(hd.fn, fn.data(), sizeof(hd.fn));
strncpy(hd.fn, fn.data(), sizeof(hd.fn) - 1);
addRow(ui->tableView->model(), &hd);
ui->tableView->resizeColumnsToContents();