Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[serial/VirtualSerial2] code is not concise enough #1194

Open
aspist opened this issue Jul 6, 2024 · 0 comments
Open

[serial/VirtualSerial2] code is not concise enough #1194

aspist opened this issue Jul 6, 2024 · 0 comments
Assignees

Comments

@aspist
Copy link

aspist commented Jul 6, 2024

in serial/VirtualSerial2/device.c line 166-207:

//
  // Manually create the symbolic link name. Length is the length in
  // bytes not including the NULL terminator.
  //
  // 6054 and 26035 are code analysis warnings that comPort.Buffer might
  // not be NULL terminated, while we know that they are. 
  //
  #pragma warning(suppress: 6054 26035)
  symbolicLinkName.Length = (USHORT)((wcslen(comPort.Buffer) * sizeof(wchar_t))
                              + sizeof(SYMBOLIC_LINK_NAME_PREFIX) - sizeof(UNICODE_NULL));
                              
  if (symbolicLinkName.Length >= symbolicLinkName.MaximumLength) {
      
      Trace(TRACE_LEVEL_ERROR, "Error: Buffer overflow when creating COM port name. Size"
          " is %d, buffer length is %d", symbolicLinkName.Length, symbolicLinkName.MaximumLength);
      status = STATUS_BUFFER_OVERFLOW;
      goto Exit;
  }
      
  errorNo = wcscpy_s(symbolicLinkName.Buffer,
                     SYMBOLIC_LINK_NAME_LENGTH,
                     SYMBOLIC_LINK_NAME_PREFIX);
                         
  if (errorNo != 0) {
      Trace(TRACE_LEVEL_ERROR, 
            "Failed to copy %ws to buffer with error %d",
            SYMBOLIC_LINK_NAME_PREFIX, errorNo);
      status = STATUS_INVALID_PARAMETER;
      goto Exit;
  }
      
  errorNo = wcscat_s(symbolicLinkName.Buffer,
                     SYMBOLIC_LINK_NAME_LENGTH,
                     comPort.Buffer);
                         
  if (errorNo != 0) {
      Trace(TRACE_LEVEL_ERROR, 
            "Failed to copy %ws to buffer with error %d",
            comPort.Buffer, errorNo);
      status = STATUS_INVALID_PARAMETER;
      goto Exit;
  }

replace it with the following code:

status = RtlUnicodeStringCopyString(&symbolicLinkName, SYMBOLIC_LINK_NAME_PREFIX);
if (!NT_SUCCESS(status)) 
{
	Trace(TRACE_LEVEL_ERROR,
		"Error: Cannot create symbolic link name");
	goto Exit;
}
status = RtlUnicodeStringCat(&symbolicLinkName, &comPort);
if (!NT_SUCCESS(status)) {
    Trace(TRACE_LEVEL_ERROR,
        "Error: Cannot create symbolic link name");
    goto Exit;
}
@NeoAdonis NeoAdonis changed the title This part of the code is not concise enough [serial/VirtualSerial2] code is not concise enough Jul 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants