question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Access Violation on call to DsAddressToSiteNames

See original GitHub issue

Describe the bug and how to reproduce

When passing an array of SOCKET_ADDRESS to DsAddressToSiteNames either an access violation or win32 (1783) error (The stub received bad data) is throw.

What code is involved

The header for SOCKET_ADDRESS is as follows

[StructLayout(LayoutKind.Sequential,
#if x64
	Size = 16)]
#else
	Size = 8)]
#endif
	public struct SOCKET_ADDRESS

However, there appears to be no compiler definition for x64 and therefore, the layout size is always set to 8. If you compile your app to run 32-bit it succeeds. If you compile as AnyCPU or x64, it fails.

https://github.com/dahall/Vanara/blob/d1d0ff51ca959a1e0f60c34382260d8d353b5f6a/PInvoke/Ws2_32/ws2def.cs#L1716

Changing this struct value manually works.

Expected behavior

The call should succeed on both x86 an x64 builds.

Repo sample

Modify the dcname variable below with the name of a domain controller to reproduce the issue. Running as x64 fails, running as x86 succeeds

using System.Net;
using System.Runtime.InteropServices;
using Vanara.Extensions;
using Vanara.PInvoke;
using static Vanara.PInvoke.NetApi32;
using static Vanara.PInvoke.Ws2_32;

internal class Program
{
    private static void Main(string[] args)
    {
        string dcname = "dc1";

        Ws2_32.WSAStartup(2, out _);
        List<IPAddress> ipAddresses;

        try
        {
            // fails
            ipAddresses = new List<IPAddress>() { IPAddress.Parse("127.0.0.1"), IPAddress.Parse("::1") };
            GetSites(ipAddresses, dcname);
        }

        catch (Exception ex)
        {
            Console.WriteLine("lookup failed\r\n{0}", ex);
        }

        Console.WriteLine("=======================================================\r\n");

        try
        {
            // works
            ipAddresses = new List<IPAddress>() { IPAddress.Parse("127.0.0.1") };
            GetSites(ipAddresses, dcname);
        }
        catch (Exception ex)
        {
            Console.WriteLine("lookup failed\r\n{0}", ex);
        }

        Console.WriteLine("=======================================================\r\n");

        try
        {
            // works
            ipAddresses = new List<IPAddress>() { IPAddress.Parse("::1") };
            GetSites(ipAddresses, dcname);
        }
        catch (Exception ex)
        {
            Console.WriteLine("lookup failed\r\n{0}", ex);
        }

        Console.WriteLine("=======================================================\r\n");


        Console.WriteLine("press any key to exit");
        Console.ReadLine();
    }

    private static void GetSites(List<IPAddress> ipAddresses, string dcname)
    {
        List<Ws2_32.SOCKADDR> resolvedAddresses = new List<Ws2_32.SOCKADDR>();

        foreach (IPAddress ipAddress in ipAddresses.OrderBy(t => (int)t.AddressFamily))
        {
            Ws2_32.SOCKADDR socketAddress;

            if (ipAddress.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
            {
                socketAddress = new Ws2_32.SOCKADDR_IN();
            }
            else if (ipAddress.AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6)
            {
                socketAddress = new Ws2_32.SOCKADDR_IN6();
            }
            else
            {
                Console.WriteLine("Ignoring unknown address type {0}", ipAddress.AddressFamily.ToString());
                continue;
            }

            int length = socketAddress.Size;
            string addr = ipAddress.ToString();

            var wsresult = Ws2_32.WSAStringToAddress(addr, (Ws2_32.ADDRESS_FAMILY)ipAddress.AddressFamily, IntPtr.Zero, socketAddress, ref length);

            if (wsresult.Failed)
            {
                Console.WriteLine("WSAStringToAddress failed\r\n{0}", wsresult.GetException());
            }

            resolvedAddresses.Add(socketAddress);
        }

        if (resolvedAddresses.Count == 0)
        {
            Console.WriteLine("No addresses found");
            return;
        }

        var socketAddresses = new SOCKET_ADDRESS[resolvedAddresses.Count];
        for (int i = 0; i < resolvedAddresses.Count; i++)
        {
            socketAddresses[i].iSockaddrLength = resolvedAddresses[i].Size;
            socketAddresses[i].lpSockaddr = resolvedAddresses[i].DangerousGetHandle();
        }

        NetApi32.SafeNetApiBuffer siteNames = null;

        var dsresult = DsAddressToSiteNames(dcname, (uint)socketAddresses.Length, socketAddresses, out siteNames);

        dsresult.ThrowIfFailed();

        if (siteNames == null || siteNames.IsInvalid)
        {
            Console.WriteLine("Sites were not found");
            return;
        }

        List<string> sites = siteNames.ToStringEnum(resolvedAddresses.Count).ToList();

        Console.WriteLine("Found sites {0} for addresses {1}", string.Join(", ", sites), string.Join(", ", ipAddresses));
    }
}

Issue Analytics

  • State:closed
  • Created 10 months ago
  • Comments:9 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
dahallcommented, Nov 30, 2022

Thank you for submitting such a thorough bug report! I’ll have this resolved shortly.

0reactions
dahallcommented, Dec 22, 2022

This is fixed with the latest commit. I’ll publish soon.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Catch c++ Access Violation exception
Access violation means your program is broken. If you do intercept the signal, discard it, and continue, you can't know what state the...
Read more >
Access Violation C0000005 - Execute
An Execute Access Violation occurs when the application attempts to execute code from a memory address that is invalid.
Read more >
Access violation C++ /CLI 15.9.5 ISO C++ Latest Draft ...
I've been looking into this bug; its an issue with the interaction between `/clr` mode and `/std:c++17`, more specifically the C++17 Evaluation ...
Read more >
View topic - Access violation error in 64-bit; works in 32-bit
Hi all. I have a program which works fine when compiled in 32-bit, yet terminates with an access violation error when compiled as...
Read more >
Access violation when calling ID3D12DescriptorHeap. ...
Actual behavior I'm getting access violation error when calling ID3D12DescriptorHeap.GetCPUDescriptorHandleForHeapStart method.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found