DISK: Use memset and strlcpy rather than strncpy for fixed size strings

This commit is contained in:
Mark Pizzolato 2020-10-11 15:33:23 -07:00
parent 4db4f90429
commit 187ee83f07

View file

@ -2079,8 +2079,8 @@ switch (DK_GET_FMT (uptr)) { /* case on format */
/* Construct a pseudo simh disk footer*/
memcpy (f->Signature, "simh", 4);
f->DriveType[sizeof (f->DriveType) - 1] = '\0'; /* Force NUL termination */
strncpy ((char *)f->DriveType, sim_vhd_disk_get_dtype (uptr->fileref, &f->SectorSize, &f->TransferElementSize, (char *)f->CreatingSimulator, &creation_time), sizeof (f->DriveType) - 1);
memset (f->DriveType, 0, sizeof (f->DriveType));
strlcpy ((char *)f->DriveType, sim_vhd_disk_get_dtype (uptr->fileref, &f->SectorSize, &f->TransferElementSize, (char *)f->CreatingSimulator, &creation_time), sizeof (f->DriveType));
f->SectorSize = NtoHl (f->SectorSize);
f->TransferElementSize = NtoHl (f->TransferElementSize);
if ((f->SectorSize == 0) || (NtoHl (f->SectorSize) == 0x00020000)) { /* Old or mangled format VHD footer */
@ -2089,7 +2089,8 @@ switch (DK_GET_FMT (uptr)) { /* case on format */
f->SectorSize = NtoHl (f->SectorSize);
f->TransferElementSize = NtoHl (f->TransferElementSize);
}
strncpy ((char*)f->CreationTime, ctime (&creation_time), sizeof (f->CreationTime) - 1);
memset (f->CreationTime, 0, sizeof (f->CreationTime));
strlcpy ((char*)f->CreationTime, ctime (&creation_time), sizeof (f->CreationTime));
container_size = sim_vhd_disk_size (uptr->fileref);
f->SectorCount = NtoHl ((uint32)(container_size / NtoHl (f->SectorSize)));
container_size += sizeof (*f); /* Adjust since it is removed below */
@ -2146,13 +2147,15 @@ f = (struct simh_disk_footer *)calloc (1, sizeof (*f));
f->AccessFormat = DK_GET_FMT (uptr);
total_sectors = (((t_offset)uptr->capac) * ctx->capac_factor * ((dptr->flags & DEV_SECTORS) ? 512 : 1)) / ctx->sector_size;
memcpy (f->Signature, "simh", 4);
f->CreatingSimulator[sizeof (f->CreatingSimulator) - 1] = '\0'; /* Force NUL termination */
strncpy ((char *)f->CreatingSimulator, sim_name, sizeof (f->CreatingSimulator) - 1);
strncpy ((char *)f->DriveType, dtype, sizeof (f->DriveType) - 1);
memset (f->CreatingSimulator, 0, sizeof (f->CreatingSimulator));
strlcpy ((char *)f->CreatingSimulator, sim_name, sizeof (f->CreatingSimulator));
memset (f->DriveType, 0, sizeof (f->DriveType));
strlcpy ((char *)f->DriveType, dtype, sizeof (f->DriveType));
f->SectorSize = NtoHl (ctx->sector_size);
f->SectorCount = NtoHl ((uint32)total_sectors);
f->TransferElementSize = NtoHl (ctx->xfer_element_size);
strncpy ((char*)f->CreationTime, ctime (&now), sizeof (f->CreationTime) - 1);
memset (f->CreationTime, 0, sizeof (f->CreationTime));
strlcpy ((char*)f->CreationTime, ctime (&now), sizeof (f->CreationTime));
f->Checksum = NtoHl (eth_crc32 (0, f, sizeof (*f) - sizeof (f->Checksum)));
free (ctx->footer);
ctx->footer = f;
@ -2529,7 +2532,7 @@ if ((DK_GET_FMT (uptr) == DKUF_F_VHD) || (ctx->footer)) {
if (ctx->footer) {
sector_size = NtoHl (ctx->footer->SectorSize);
xfer_element_size = NtoHl (ctx->footer->TransferElementSize);
strncpy (created_name, (char *)ctx->footer->CreatingSimulator, sizeof (created_name));
strlcpy (created_name, (char *)ctx->footer->CreatingSimulator, sizeof (created_name));
}
if ((DK_GET_FMT (uptr) == DKUF_F_VHD) && created && dtype) {
sim_vhd_disk_set_dtype (uptr->fileref, dtype, ctx->sector_size, ctx->xfer_element_size);
@ -4738,7 +4741,8 @@ memcpy (hVHD->Footer.DriveType, dtype, ((1+strlen (dtype)) < sizeof (hVHD->Foote
hVHD->Footer.DriveSectorSize = NtoHl (SectorSize);
hVHD->Footer.DriveTransferElementSize = NtoHl (xfer_element_size);
hVHD->Footer.CreatingSimulator[sizeof (hVHD->Footer.CreatingSimulator) - 1] = '\0'; /* Force NUL termination */
strncpy ((char *)hVHD->Footer.CreatingSimulator, sim_name, sizeof (hVHD->Footer.CreatingSimulator) - 1);
memset (hVHD->Footer.CreatingSimulator, 0, sizeof (hVHD->Footer.CreatingSimulator));
strlcpy ((char *)hVHD->Footer.CreatingSimulator, sim_name, sizeof (hVHD->Footer.CreatingSimulator));
hVHD->Footer.Checksum = 0;
hVHD->Footer.Checksum = NtoHl (CalculateVhdFooterChecksum (&hVHD->Footer, sizeof(hVHD->Footer)));