当前位置:网站首页>Serious bugs with lifted/nullable conversions from int, allowing conversion from decimal
Serious bugs with lifted/nullable conversions from int, allowing conversion from decimal
2022-07-05 02:00:00 【Rich in starch】
problem :
I think this question will bring me instant fame here on Stack Overflow. I think this question will make me in Stack Overflow Become famous immediately .
Suppose you have the following type: Suppose you have the following types :
// represents a decimal number with at most two decimal places after the periodstruct NumberFixedPoint2{ decimal number; // an integer has no fractional part; can convert to this type public static implicit operator NumberFixedPoint2(int integer) { return new NumberFixedPoint2 { number = integer }; } // this type is a decimal number; can convert to System.Decimal public static implicit operator decimal(NumberFixedPoint2 nfp2) { return nfp2.number; } /* will add more nice members later */}
It has been written such that only safe conversions that don't lose precision are allowed. It is written so that only safe conversions are allowed without losing accuracy .However, when I try this code: however , When I try this code :
static void Main() { decimal bad = 2.718281828m; NumberFixedPoint2 badNfp2 = (NumberFixedPoint2)bad; Console.WriteLine(badNfp2); }
I am surprised this compiles and, when run, writes out 2
. I'm surprised at this compilation , And at runtime Write 2
.The conversion from int
(of value 2
) to NumberFixedPoint2
is important here. from int
( value 2
) To NumberFixedPoint2
The transformation of is very important here .(An overload of WriteLine
that takes in a System.Decimal
is preferred, in case anyone wonders.)( If anyone wants to know , Better accept System.Decimal
Of WriteLine
heavy load .)
Why on Earth is the conversion from decimal
to NumberFixedPoint2
allowed? Why is it allowed on earth from decimal
To NumberFixedPoint2
Transformation ?(By the way, in the above code, if NumberFixedPoint2
is changed from a struct to a class, nothing changes.)( By the way , In the code above , If NumberFixedPoint2
From structure to class , There is no change .)
Do you know if the C# Language Specification says that an implicit conversion from int
to a custom type "implies" the existence of a "direct" explicit conversion from decimal
to that custom type? You know, C# Whether the language specification is from int
Implicit conversion to custom types “ Hint ” Whether there is from decimal
To this custom type “ direct ” Explicit conversion ?
It becomes much worse. It's getting worse .Try this code instead: Please try this code :
static void Main() { decimal? moreBad = 7.3890560989m; NumberFixedPoint2? moreBadNfp2 = (NumberFixedPoint2?)moreBad; Console.WriteLine(moreBadNfp2.Value); }
As you see, we have (lifted) Nullable<>
conversions here. As you can see , Here we are ( promote ) Nullable<>
transformation .But oh yes, that does compile. But oh , Yes , That really compiles .
When compiled in x86 "platform", this code writes out an unpredictable numeric value. stay x86 “platform” At compile time , This code will write unpredictable values .Which one varies from time to time. Which one changes from time to time .As an example, on one occasion I got 2289956
. for instance , Once I got 2289956
.Now, that's one serious bug! Now? , This is a serious mistake !
When compiled for the x64 platform, the above code crashes the application with a System.InvalidProgramException
with message Common Language Runtime detected an invalid program. When it comes to x64 Platform compile time , The above code is used System.InvalidProgramException
Crash the application , And display the message Common Language Runtime Invalid program detected .According to the documentation of the InvalidProgramException
class: according to InvalidProgramException
Class :
Generally this indicates a bug in the compiler that generated the program. Usually , This indicates an error in the generated program in the compiler .
Does anyone (like Eric Lippert, or someone who has worked with lifted conversions in the C# compiler) know the cause of these bugs? Is there anyone ( such as Eric Lippert, Or once in C# The person who has removed the conversion in the compiler ) Know the reasons for these mistakes ?Like, what is a sufficient condition that we don't run into them in our code? such as , What are the sufficient conditions for not encountering them in our code ?Because the type NumberFixedPoint2
is actually something that we have in real code (managing other people's money and stuff). because NumberFixedPoint2
Types are actually what we have in actual code ( Manage other people's money and things ).
Solution :
Reference resources : https://stackoom.com/en/question/1Expv边栏推荐
- Learn tla+ (XII) -- functions through examples
- PowerShell: use PowerShell behind the proxy server
- Incremental backup? db full
- batchnorm.py这个文件单GPU运行报错解决
- 使用druid连接MySQL数据库报类型错误
- He was laid off.. 39 year old Ali P9, saved 150million
- Three properties that a good homomorphic encryption should satisfy
- es使用collapseBuilder去重和只返回某个字段
- Huawei machine test question: longest continuous subsequence
- Restful fast request 2022.2.1 release, support curl import
猜你喜欢
流批一体在京东的探索与实践
Go RPC call
Visual studio 2019 set transparent background (fool teaching)
[Digital IC hand tearing code] Verilog edge detection circuit (rising edge, falling edge, double edge) | topic | principle | design | simulation
PowerShell: use PowerShell behind the proxy server
线上故障突突突?如何紧急诊断、排查与恢复
MySQL backup and recovery + experiment
"2022" is a must know web security interview question for job hopping
Li Kou Jianzhi offer -- binary tree chapter
Video display and hiding of imitation tudou.com
随机推荐
Win: use PowerShell to check the strength of wireless signal
Advanced conditional statements of common SQL operations
85.4% mIOU! NVIDIA: using multi-scale attention for semantic segmentation, the code is open source!
Some query constructors in laravel (2)
Wechat applet; Gibberish generator
187. Repeated DNA sequence - with unordered_ Map basic content
Nebula importer data import practice
Valentine's Day flirting with girls to force a small way, one can learn
Wechat applet: Xingxiu UI v1.5 WordPress system information resources blog download applet wechat QQ dual end source code support WordPress secondary classification loading animation optimization
220213c language learning diary
The steering wheel can be turned for one and a half turns. Is there any difference between it and two turns
Practice of tdengine in TCL air conditioning energy management platform
Unified blog writing environment
STM32 series - serial port UART software pin internal pull-up or external resistance pull-up - cause problem search
Win:使用 Shadow Mode 查看远程用户的桌面会话
Win: use shadow mode to view the Desktop Session of a remote user
How to build a technical team that will bring down the company?
Go RPC call
Educational Codeforces Round 122 (Rated for Div. 2) ABC
Practical case of SQL optimization: speed up your database