Partager via


Common WWSAPI errors: property value set incorrectly

As mentioned in my previous post, WWSAPI follows a common pattern to set properties (which in most cases are just configuration settings). Each property structure consists of three fields: id, value and valueSize. The field id is an enum value of the property. The enum value has a corresponding type whose size should be set in the field valueSize. The value field is defined as void* and should always be a pointer to the actual type of the property. A common mistake is to assign the actual type to the value field. The result of that is typically Access Violation that brings down the process.

 

For example, WS_XML_SECURITY_TOKEN_PROPERTY is defined this way:

typedef struct _WS_XML_SECURITY_TOKEN_PROPERTY {

    WS_XML_SECURITY_TOKEN_PROPERTY_ID id;

    __field_bcount(valueSize) void* value;

    ULONG valueSize;

} WS_XML_SECURITY_TOKEN_PROPERTY;

 

The WS_XML_SECURITY_TOKEN_PROPERTY_ID enum contains a value WS_XML_SECURITY_TOKEN_PROPERTY_ATTACHED_REFERENCE with a corresponding type WS_XML_BUFFER*. The correct way to use this property is:

WS_XML_SECURITY_TOKEN_PROPERTY tokenProperty[1];

WS_XML_BUFFER* buffer = GetAttachedReference();

tokenProperty[0].id = WS_XML_SECURITY_TOKEN_PROPERTY_ATTACHED_REFERENCE;

tokenProperty[0].value = & buffer;

tokenProperty[0].valueSize = sizeof(buffer);

 

Assigning buffer to tokenProperty[0].value will not give you a compiler error, but will probably lead to AV when you run the program. If the property type is a non-pointer type (BOOL, ULONG, WS_DATETIME, etc), the compiler will catch the error if the value is directly assigned, but the error is less obvious when the type is a pointer type.