Hi, I was looking at the Janitorial project of replacing strncpy with lstrcpyn{A,W}. I found the following code in iphlpapi a little strange. (Well, only question number 2, but then could as well ask about the 2 others.) I have marked where in the code the question belongs to with the question numbers.
1) I am wondering, why sizeof(ptr->Adaptername) is used instead of MAX_ADAPTER_NAME_LENGTH+1? Is there something special coded in the 3 last bytes, since they are hidden by the = '\0' the line below? if not then this be replaced with lstrcpyn(... , MAX_ADAPTER_NAME_LENGTH+1)?
2) Is 2a supposed to be saying 15 or is there someting about IpMask and Context that must be zero, when setting IpAddress, and IpMask?
3) Why suddenly the change to use memcpy? and not some strcpy function as the code above it?
Peter
typedef struct { char String[4 * 4]; } IP_ADDRESS_STRING, IP_MASK_STRING, ...;
typedef struct _IP_ADDR_STRING { struct _IP_ADDR_STRING* Next; IP_ADDRESS_STRING IpAddress; IP_MASK_STRING IpMask; DWORD Context; } IP_ADDR_STRING, *PIP_ADDR_STRING;
typedef struct _IP_ADAPTER_INFO { ... char AdapterName[MAX_ADAPTER_NAME_LENGTH + 4]; char Description[MAX_ADAPTER_DESCRIPTION_LENGTH + 4]; BYTE Address[MAX_ADAPTER_ADDRESS_LENGTH]; IP_ADDR_STRING IpAddressList; ... IP_ADDR_STRING PrimaryWinsServer; IP_ADDR_STRING SecondaryWinsServer; } IP_ADAPTER_INFO, *PIP_ADAPTER_INFO;
ipenum.c: 936 char *toIPAddressString(unsigned int addr, char string[16]) { if (string) { struct in_addr iAddr;
iAddr.s_addr = addr; /* extra-anal, just to make auditors happy */ strncpy(string, inet_ntoa(iAddr), 16); string[16] = '\0'; **** 2a **** } return string; }
iphlpapi_main.c: 584 DWORD WINAPI GetAdaptersInfo(PIP_ADAPTER_INFO pAdapterInfo, PULONG pOutBufLen) { ... PIP_ADAPTER_INFO ptr = &pAdapterInfo[ndx]; ... 1) /* on Win98 this is left empty, but whatever */ strncpy(ptr->AdapterName, getInterfaceNameByIndex(table->indexes[ndx]), sizeof(ptr->AdapterName)); ptr->AdapterName[MAX_ADAPTER_NAME_LENGTH] = '\0'; ... 2) toIPAddressString(getInterfaceIPAddrByIndex(table->indexes[ndx]), ptr->IpAddressList.IpAddress.String); toIPAddressString(getInterfaceMaskByIndex(table->indexes[ndx]), ptr->IpAddressList.IpMask.String);
... 3) memcpy(ptr->PrimaryWinsServer.IpAddress.String, primaryWINS.String, sizeof(primaryWINS.String)); memcpy(ptr->SecondaryWinsServer.IpAddress.String, secondaryWINS.String, sizeof(secondaryWINS.String)); ... }