udp - WinXP: sendto() failed with 10014 (WSAEFAULT) if destination address is const-qualified, IPv4-specific -


it seems, found bug in windows... ok, let not such pathetic one. i'm trying generic sendto() operation udp , occasionaly found winxp (32 bit, sp3, checked on real , virtual machines) returns "-1" bytes sent wsagetlasterror() error 10014 (aka wsaefault). occurs on ipv4 addresses (same code ipv6 destination works perfectly). major condition reproduce usage of "const struct sockaddr_in" declared @ global scope. here plain c code vs2010 (also i've tried eclipse+mingw, got same results):

#include <stdio.h> #include <stdlib.h> #include <errno.h> #include <winsock2.h> #include <stdint.h>  #pragma comment(lib, "ws2_32.lib")  #define inaddr_upnp_v4 0xeffffffa #define htons(x) ((((uint16_t)(x) & 0xff00) >> 8) | (((uint16_t)(x) & 0x00ff) << 8)) #define htonl(x) ((((uint32_t)(x) & 0xff000000) >> 24) | (((uint32_t)(x) & 0x00ff0000) >> 8) | (((uint32_t)(x) & 0x0000ff00) << 8) | (((uint32_t)(x) & 0x000000ff) << 24))  // magic "const" qualifier, causes run-time error const struct sockaddr_in addr_global = {     af_inet,     htons(1900),     {             htonl(inaddr_upnp_v4)     },     {0}, };  int main(int argc, char** argv) { #define cr_lf "\r\n"      // these 2 lines un-buffer console window output @ win32, see url below details     // http://wiki.eclipse.org/cdt/user/faq#eclipse_console_does_not_show_output_on_windows     setvbuf(stdout, null, _ionbf, 0);     setvbuf(stderr, null, _ionbf, 0);      printf("started\n");      const struct sockaddr_in addr_local = {             af_inet,             htons(1900),             {                     htonl(inaddr_upnp_v4)             },             {0},     };      const char *msearch_request_v4 = "m-search * http/1.1"cr_lf     "host:239.255.255.250:1900"cr_lf     "man:\"ssdp:discover\""cr_lf     "st:ssdp:all"cr_lf     "mx:3"cr_lf     cr_lf;      const int msearch_len = strlen(msearch_request_v4);      wsadata wsadata;     int res = wsastartup(makeword(2, 2), &wsadata);      int af = af_inet;     int sock_id = socket(af, sock_dgram, ipproto_udp);     if (-1 == sock_id) {         printf("%s: socket() failed error %i/%i\n", __function__,                 errno, wsagetlasterror());         return 1;     }      int data_sent = 0;      printf("1st sendto()\n");     data_sent = sendto(sock_id, msearch_request_v4,             msearch_len, 0,             (const struct sockaddr * const)&addr_local,             sizeof(struct sockaddr_in));     if (data_sent < 0) {         printf("%s: sendto(local) failed error %i/%i\n", __function__,                 errno, wsagetlasterror());     }      printf("2nd sendto(), fail on winxp sp3 (32 bit)\n");     data_sent = sendto(sock_id, msearch_request_v4,             msearch_len, 0,             (const struct sockaddr * const)&addr_global,             sizeof(struct sockaddr_in));     if (data_sent < 0) {         printf("%s: sendto(global) failed error %i/%i\n", __function__,                 errno, wsagetlasterror());     }      closesocket(sock_id);      res = wsacleanup();     printf("finished\n");     return 0; } 

so, if run code @ win7, example, absolutely ok. winxp fails on addr_global usage if equipped "const" qualifier (see "magic" comment above). also, "output" window says:

first-chance exception @ 0x71a912f4 in sendtobugxp.exe: 0xc0000005: access violation writing location 0x00415744.

with of "autos" window, it's easy figure out 0x00415744 location address of addr_global.sin_zero field. seems, winxp write zeros there , violates memory access flags. or silly me, trying go wrong door?

appreciate comments lot. in advance.

yeah found bug. sendto() has argument declared const, wrote anyway. luck getting fixed though. hint: might in antivirus or firewall.


Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -