libmicrohttpd
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


From: Evgeny Grin
Subject: Re: [libmicrohttpd] Run-Time Check Failure(Static Link in Debug)
Date: Wed, 13 Sep 2023 17:28:44 +0300
User-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101 Thunderbird/102.15.0

VS run-time warnings are nice.
However, they warn even when explicit casting is used, which is not nice. Explicit cast means that data truncation is intentional.

The suggested "fix" would not change the resulting binary *if compiler is smart enough*. MHD is very portable and can be built by many various compiler. I don't think that changing the code just to satisfy a single compiler is good idea if such change could potentially influence resulting binaries on other compilers. Code readability could be hurt as well.

VS project have been corrected to completely disable these warnings.
Also I've added compiler pragmas to explicitly disable such checks in some specific functions, so it is still possible to enable run-time checks in project settings and use MHD. However, I'm not sure that individual explicit disable pragmas cover all functions, where such checks must be disabled.

--
Evgeny

On 13.09.2023 10:58, luozhaotian wrote:
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.

Attachment: OpenPGP_0x460A317C3326D2AE.asc
Description: OpenPGP public key

Attachment: OpenPGP_signature
Description: OpenPGP digital signature


reply via email to

[Prev in Thread] Current Thread [Next in Thread]