+BOOL WINAPI DnsHostnameToComputerNameA(LPCSTR hostname,
+    LPSTR computername, LPDWORD size)
...
+    DWORD len;
+
...
+    if (!hostname || !size) return FALSE;
+    len = lstrlenW(hostname);
+
+    if (len > MAX_COMPUTERNAME_LENGTH)
+        len = MAX_COMPUTERNAME_LENGTH;
+
+    if (*size < len)
+    {
+        *size = len;
+        return FALSE;
+    }
+    if (!computername) return FALSE;
+
+    memcpy( computername, hostname, len );
+    computername[len + 1] = 0;
+    return TRUE;
+ }

I have seen at lot of these where the \0 termination is unspecified for
the size parameter, but in this case msdn mentions it.

>From msdn:
--------
BOOL DnsHostnameToComputerName(
  LPCTSTR Hostname,
  LPTSTR ComputerName,
  LPDWORD nSize
);

Parameters

Hostname
[in] Pointer to a string that specifies the DNS name. If the DNS name is
not a valid, translatable name, the function fails.

ComputerName
[out] Pointer to a string buffer that receives the computer name. The
buffer size should be large enough to contain MAX_COMPUTERNAME_LENGTH + 1
characters.

nSize
[in, out] On input, specifies the size of the buffer, in TCHARs. On
output, receives the number of TCHARs copied to the destination buffer,
not including the terminating null character.

If the buffer is too small, the function fails, GetLastError returns
ERROR_MORE_DATA, and nSize receives the required buffer size, not
including the terminating null character.
-----------

So at least the check should be:
if (*size < len + 1)
{
   ...
}

or you risk the computername[len+1] is invalid.

Now the second question is, which might be a convention, where does is say
that I cant set computerName size to 3*MAX_COMPUTERNAME_LENGTH and expect
it to return the computername not truncated to MAX_COMPUTERNAME_LENGTH?

Peter



Reply via email to