From 95ebde24703eaf1f240d7a8e039a7eb53ff7be72 Mon Sep 17 00:00:00 2001 From: Natalia Portillo Date: Sat, 24 Oct 2020 14:40:11 +0100 Subject: [PATCH] Use sprintf_s in Windows instead of snprintf. --- win32/hello.c | 66 ++++++++++++++++++++++---------------------- win32/list_devices.c | 2 +- win32/usb.c | 12 +++++--- worker.c | 7 +++++ 4 files changed, 49 insertions(+), 38 deletions(-) diff --git a/win32/hello.c b/win32/hello.c index 5b7f20b..56fe1fd 100644 --- a/win32/hello.c +++ b/win32/hello.c @@ -81,7 +81,7 @@ AaruPacketHello* GetHello() if(osvi.dwPlatformId == VER_PLATFORM_WIN32s) { strncpy(pkt_server_hello->sysname, win, 255); - snprintf(pkt_server_hello->release, 255, "%d.%02d", osvi.dwMajorVersion, osvi.dwMinorVersion); + sprintf_s(pkt_server_hello->release, 255, "%d.%02d", osvi.dwMajorVersion, osvi.dwMinorVersion); strncpy(pkt_server_hello->machine, "x86", 255); return pkt_server_hello; @@ -105,12 +105,12 @@ AaruPacketHello* GetHello() default: strncpy(pkt_server_hello->sysname, win, 255); break; } - snprintf(pkt_server_hello->release, - 255, - "%d.%02d.%d", - osvi.dwMajorVersion, - osvi.dwMinorVersion, - osvi.dwBuildNumber & 0xFFFF); + sprintf_s(pkt_server_hello->release, + 255, + "%d.%02d.%d", + osvi.dwMajorVersion, + osvi.dwMinorVersion, + osvi.dwBuildNumber & 0xFFFF); strncpy(pkt_server_hello->machine, "x86", 255); return pkt_server_hello; @@ -139,20 +139,20 @@ AaruPacketHello* GetHello() } if(strlen(osvi.szCSDVersion) > 0) - snprintf(pkt_server_hello->release, - 255, - "%d.%d.%d (%s)", - osvi.dwMajorVersion, - osvi.dwMinorVersion * 10, - osvi.dwBuildNumber, - osvi.szCSDVersion); + sprintf_s(pkt_server_hello->release, + 255, + "%d.%d.%d (%s)", + osvi.dwMajorVersion, + osvi.dwMinorVersion * 10, + osvi.dwBuildNumber, + osvi.szCSDVersion); else - snprintf(pkt_server_hello->release, - 255, - "%d.%d.%d", - osvi.dwMajorVersion, - osvi.dwMinorVersion * 10, - osvi.dwBuildNumber); + sprintf_s(pkt_server_hello->release, + 255, + "%d.%d.%d", + osvi.dwMajorVersion, + osvi.dwMinorVersion * 10, + osvi.dwBuildNumber); if(osvi.dwMajorVersion < 5) { @@ -243,20 +243,20 @@ AaruPacketHello* GetHello() osvi.dwBuildNumber = HIWORD(fileVerInfo->dwProductVersionLS); if(strlen(osvi.szCSDVersion) > 0) - snprintf(pkt_server_hello->release, - 255, - "%d.%d.%d (%s)", - osvi.dwMajorVersion, - osvi.dwMinorVersion * 10, - osvi.dwBuildNumber, - osvi.szCSDVersion); + sprintf_s(pkt_server_hello->release, + 255, + "%d.%d.%d (%s)", + osvi.dwMajorVersion, + osvi.dwMinorVersion * 10, + osvi.dwBuildNumber, + osvi.szCSDVersion); else - snprintf(pkt_server_hello->release, - 255, - "%d.%d.%d", - osvi.dwMajorVersion, - osvi.dwMinorVersion * 10, - osvi.dwBuildNumber); + sprintf_s(pkt_server_hello->release, + 255, + "%d.%d.%d", + osvi.dwMajorVersion, + osvi.dwMinorVersion * 10, + osvi.dwBuildNumber); } free(verData); diff --git a/win32/list_devices.c b/win32/list_devices.c index d5ec26d..d506672 100644 --- a/win32/list_devices.c +++ b/win32/list_devices.c @@ -63,7 +63,7 @@ DeviceInfoList* ListDevices() if((strncmp(pos, "PhysicalDrive", 13) != 0 && strncmp(pos, "CdRom", 5) != 0) && strncmp(pos, "Tape", 4) != 0) { continue; } - snprintf(physId, 4096, "\\\\.\\%s", pos); + sprintf_s(physId, 4096, "\\\\.\\%s", pos); printf("%s\n", physId); diff --git a/win32/usb.c b/win32/usb.c index cb40ab2..bdc0179 100644 --- a/win32/usb.c +++ b/win32/usb.c @@ -177,7 +177,9 @@ UsbHub_t* GetRootHub(UsbController_t* controller) // get the Hub Name if(DeviceIoControl(h, IOCTL_USB_GET_ROOT_HUB_NAME, &hubName, nBytes, &hubName, nBytes, &k, NULL)) - { snprintf(root->HubDevicePath, USB_BUFFER_SIZE, "\\\\.\\%ws", hubName->RootHubName); } + { + sprintf_s(root->HubDevicePath, USB_BUFFER_SIZE, "\\\\.\\%ws", hubName->RootHubName); + } // TODO: Get DriverKeyName for Root Hub @@ -333,7 +335,7 @@ UsbDevice_t* GetPortDevice(UsbPort_t* port) // by the structure allocation, we're forced to zero out this // chunk of memory by using the StringToHGlobalAuto() hack above descriptor = (PUSB_STRING_DESCRIPTOR)(request + sizeof(USB_DESCRIPTOR_REQUEST)); - snprintf(device->Product, USB_BUFFER_SIZE, "%ws", descriptor->bString); + sprintf_s(device->Product, USB_BUFFER_SIZE, "%ws", descriptor->bString); } } if(port->PortDeviceDescriptor.iSerialNumber > 0) @@ -354,7 +356,7 @@ UsbDevice_t* GetPortDevice(UsbPort_t* port) // by the structure allocation, we're forced to zero out this // chunk of memory by using the StringToHGlobalAuto() hack above descriptor = (PUSB_STRING_DESCRIPTOR)(request + sizeof(USB_DESCRIPTOR_REQUEST)); - snprintf(device->SerialNumber, USB_BUFFER_SIZE, "%ws", descriptor->bString); + sprintf_s(device->SerialNumber, USB_BUFFER_SIZE, "%ws", descriptor->bString); } } @@ -418,7 +420,9 @@ UsbHub_t* GetPortHub(UsbPort_t* port) // Use an IOCTL call to request the Node Name if(DeviceIoControl(h, IOCTL_USB_GET_NODE_CONNECTION_NAME, nodeName, nBytes, nodeName, nBytes, &k, NULL)) - { snprintf(hub->HubDevicePath, USB_BUFFER_SIZE, "\\\\.\\%ws", nodeName->NodeName); } + { + sprintf_s(hub->HubDevicePath, USB_BUFFER_SIZE, "\\\\.\\%ws", nodeName->NodeName); + } // Now let's open the Hub (based upon the HubName we got above) h2 = CreateFile(hub->HubDevicePath, GENERIC_WRITE, FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL); diff --git a/worker.c b/worker.c index d81f21b..2eb3f02 100644 --- a/worker.c +++ b/worker.c @@ -1069,10 +1069,17 @@ void* WorkingLoop(void* arguments) default: pkt_nop->reason_code = AARUREMOTE_PACKET_NOP_REASON_NOT_RECOGNIZED; memset(&pkt_nop->reason, 0, 256); +#ifdef _WIN32 + sprintf_s(pkt_nop->reason, + 256, + "Received unrecognized packet with type %d, skipping...", + pkt_hdr->packet_type); +#else snprintf(pkt_nop->reason, 256, "Received unrecognized packet with type %d, skipping...", pkt_hdr->packet_type); +#endif NetWrite(cli_ctx, pkt_nop, sizeof(AaruPacketNop)); printf("%s...\n", pkt_nop->reason); skip_next_hdr = 1;