Repository: dotnet/runtime
File: src/coreclr/vm/callconvbuilder.cpp (C++)

Code before:
    uint32_t sigCallConv = 0;
    IfFailRet(sigPtr.GetCallingConvInfo(&sigCallConv)); // call conv
    if (sigCallConv & IMAGE_CEE_CS_CALLCONV_GENERIC)
    {
        IfFailRet(sigPtr.GetData(NULL)); // type param count
    }
    IfFailRet(sigPtr.GetData(NULL)); // arg count

#ifdef DEBUG
    PCCOR_SIGNATURE pWalk = sigPtr.GetPtr();
    _ASSERTE(pWalk <= pSig + cSig);
#endif

    return TryGetUnmanagedCallingConventionFromModOptSigStartingAtRetType(
        pModule,
        sigPtr,
        builder,
        errorResID);
}

HRESULT CallConv::TryGetUnmanagedCallingConventionFromModOptSigStartingAtRetType(
    _In_ CORINFO_MODULE_HANDLE pModule,
    _In_ SigPointer sig,
    _Inout_ CallConvBuilder* builder,
    _Out_ UINT* errorResID)
{
    PCCOR_SIGNATURE pSig;
    uint32_t cSig;
    sig.GetSignature(&pSig, &cSig);
    PCCOR_SIGNATURE pWalk = pSig;

    CallConvBuilder& callConvBuilder = *builder;
    while ((pWalk < (pSig + cSig)) && ((*pWalk == ELEMENT_TYPE_CMOD_OPT) || (*pWalk == ELEMENT_TYPE_CMOD_REQD) || (*pWalk == ELEMENT_TYPE_CMOD_INTERNAL)))
    {
        CORINFO_MODULE_HANDLE tokenLookupModule = pModule;
        mdToken tk;
        LPCSTR typeNamespace;
        LPCSTR typeName;
        if (*pWalk == ELEMENT_TYPE_CMOD_INTERNAL)
        {
            // Skip internal modifiers
            pWalk++;
            if (pWalk + 1 + sizeof(void*) > pSig + cSig)
            {
                *errorResID = BFA_BAD_SIGNATURE;
                return COR_E_BADIMAGEFORMAT; // Bad formatting
            }

            BOOL required = *pWalk++ != 0;
            void* pType;
            pWalk += CorSigUncompressPointer(pWalk, &pType);

Review comment (suggestion):
The new function is missing contract checking (CONTRACTL block) that validates preconditions. Following the pattern of the calling function TryGetUnmanagedCallingConventionFromModOpt, this function should validate that builder and errorResID are not NULL and include appropriate VM contract declarations.
```suggestion
{
    CONTRACTL
    {
        STANDARD_VM_CHECK;
        PRECONDITION(builder != NULL);
        PRECONDITION(errorResID != NULL);
    }
    CONTRACTL_END;
```

Code after:
    uint32_t sigCallConv = 0;
    IfFailRet(sigPtr.GetCallingConvInfo(&sigCallConv)); // call conv
    if (sigCallConv & IMAGE_CEE_CS_CALLCONV_GENERIC)
    {
        IfFailRet(sigPtr.GetData(NULL)); // type param count
    }
    IfFailRet(sigPtr.GetData(NULL)); // arg count

#ifdef DEBUG
    PCCOR_SIGNATURE pWalk = sigPtr.GetPtr();
    _ASSERTE(pWalk <= pSig + cSig);
#endif

    return TryGetUnmanagedCallingConventionFromModOptSigStartingAtRetType(
        pModule,
        sigPtr,
        builder,
        errorResID);
}

HRESULT CallConv::TryGetUnmanagedCallingConventionFromModOptSigStartingAtRetType(
    _In_ CORINFO_MODULE_HANDLE pModule,
    _In_ SigPointer sig,
    _Inout_ CallConvBuilder* builder,
    _Out_ UINT* errorResID)
{
    CONTRACTL
    {
        STANDARD_VM_CHECK;
        PRECONDITION(builder != NULL);
        PRECONDITION(errorResID != NULL);
    }
    CONTRACTL_END;
    PCCOR_SIGNATURE pSig;
    uint32_t cSig;
    sig.GetSignature(&pSig, &cSig);
    PCCOR_SIGNATURE pWalk = pSig;

    CallConvBuilder& callConvBuilder = *builder;
    while ((pWalk < (pSig + cSig)) && ((*pWalk == ELEMENT_TYPE_CMOD_OPT) || (*pWalk == ELEMENT_TYPE_CMOD_REQD) || (*pWalk == ELEMENT_TYPE_CMOD_INTERNAL)))
    {
        CORINFO_MODULE_HANDLE tokenLookupModule = pModule;
        mdToken tk;
        LPCSTR typeNamespace;
        LPCSTR typeName;
        if (*pWalk == ELEMENT_TYPE_CMOD_INTERNAL)
        {
            // Skip internal modifiers
            pWalk++;
            if (pWalk + 1 + sizeof(void*) > pSig + cSig)
            {