[libmicrohttpd] Run-Time Check Failure(Static Link in Debug)

2023-09-11 Thread luozhaotian
When test https://www.gnu.org/software/libmicrohttpd/tutorial.html chapter 5 
Supporting basic 
authentication(libmicrohttpd-0.9.77/doc/examples/basicauthentication.cpp), i 
encounter this error:
Run-Time Check Failure #1 - A cast to a smaller data type has caused a loss of 
data.  If this was intentional, you should mask the source of the cast with the 
appropriate bitmask.  


The call stack: 
>Hello.exe!MHD_base64_to_bin_n(const char * base64, unsigned __int64 
>base64_len, void * bin, unsigned __int64 bin_size) line 1422
 Hello.exe!MHD_basic_auth_get_username_password(MHD_Connection * connection, 
char * * password) line 95
 Hello.exe!answer_to_connection(void * cls, MHD_Connection * connection, const 
char * url, const char * method, const char * version, const char * 
upload_data, unsigned __int64 * upload_data_size, void * * con_cls) line 46
 Hello.exe!call_connection_handler(MHD_Connection * connection) line 3070
..
   MHD_basic_auth_get_username_password


I use Visual Studio 2022
Microsoft Visual Studio Community 2022 (64 位) - Current Version 17.5.3


I use Debug_x64 , and static link  to 
0.9.77\x86_64\VS2022\Debug-static\libmicrohttpd_d.lib
But use dll is ok.







Re: [libmicrohttpd] Run-Time Check Failure(Static Link in Debug)

2023-09-13 Thread luozhaotian
Visual Studio show this Debug Error:

Run-Time Check Failure #1 - A cast to a smaller data type has caused a loss of 
data.  If this was intentional, you should mask the source of the cast with the 
appropriate bitmask.  For example: 

   char c = (i & 0xFF);

Changing the code in this way will not affect the quality of the resulting 
optimized code.

 

After debug, I find the reason is cast to uint8_t.  When cast values bigger 
than 256 to uint8_t in line 1421-1423,  the Debug Error will be triggered.

 

//mhd_str.c

size_t

MHD_base64_to_bin_n (const char *base64,

 size_t base64_len,

 void *bin,

 size_t bin_size){

 //……

1421  out[j + 0] = (uint8_t) uint8_t) v1) << 2) | (((uint8_t) v2) >> 
4));

1422  out[j + 1] = (uint8_t) uint8_t) v2) << 4) | (((uint8_t) v3) >> 
2));

1423  out[j + 2] = (uint8_t) uint8_t) v3) << 6) | (((uint8_t) v4)));

   //……

}

 

In Visual Studio, the Debug mode default open /RTC1, but the Release mode 
doesn’t. 

 

As the suggestion by Visual Studio, if we modify the current git 
master(mhd_str.c) to:
2375  out[j + 0] = (uint8_t) uint8_t) uint8_t) v1) << 2)&0xFF))
  | ((uint8_t) (((uint8_t) v2) >> 4))) & 0xFF);
2377  out[j + 1] = (uint8_t) uint8_t) uint8_t) v2) << 4) & 0xFF))
  | ((uint8_t) (((uint8_t) v3) >> 2))) & 0xFF);
2379  out[j + 2] = (uint8_t) uint8_t) uint8_t) v3) << 6)&0xFF))
  | ((uint8_t) (((uint8_t) v4 & 0xFF);

The error didn'tn  appear.

 

Otherwise, if we ignore the error, the program still works.