找到代码生成器漏洞。 [英] Found code generator flaw.

查看:112
本文介绍了找到代码生成器漏洞。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在某些情况下,编译器生成错误的代码。


查看此代码:


unsigned char ary [2];

ary [0] = 0x77;

ary [1] = 0xCB;

unsigned char val;

val = ary [0]>> 4;
$
//预期:val = 0x07。得到:0x07。

val = ary [1]>> 4;
$
//预期:val = 0x0C。得到:0xFC!


这种情况发生了,因为编译器在移位时会发出SAR指令,即使使用无符号值也是如此。对于无符号值,编译器应使用SHR。因此,即使对于无符号值,符号位也会扩展。好的,有一个简单的解决方法,
这个问题(val =(ary [0]>> 4)& 0x0F;),但它很难看,在某些情况下很麻烦。


使用Microsoft Visual Studio 2010验证,版本10.0.40219.1 SP1Rel


解决方案

你应该报告这个问题@ Microsoft连接一个重现问题的小项目!


https: //connect.microsoft.com/


In some cases, the compiler generates wrong code.

look at this code:

unsigned char ary[2];
ary[0] = 0x77;
ary[1] = 0xCB;
unsigned char val;
val = ary[0] >> 4;
// expected: val = 0x07. Got: 0x07.
val = ary[1] >> 4;
// expected: val = 0x0C. Got: 0xFC!

This happens, because the compiler emits a SAR instruction when shifting, even when using unsigned values. For unsigned values, the compiler should use SHR. Due to this, the sign bit is extended even for unsigned values. Ok, there is a easy workaround for this problem (val = (ary[0] >> 4) & 0x0F;), but it's ugly and, in some cases, troublesome.

Verified with Microsoft Visual Studio 2010, Version 10.0.40219.1 SP1Rel

解决方案

You should report this issue @ Microsoft connect with a small project that reproduces the issue!

https://connect.microsoft.com/


这篇关于找到代码生成器漏洞。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆