一个是语言律师 [英] One for the language lawyers

查看:101
本文介绍了一个是语言律师的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一种常用的技术,当然,它可以在任何合理的现代普通硬件上正常工作。但是,它是否通过了CLC测试?


/ *假设形式良好的输入 - 当然,你总是可以通过

*来打破它输入* /


struct foo {int field1,field2; char nl; } * bar;

char buffer [SOMENUMBERWHATEVERFLOATSYOURBOAT];


int main(void){

bar =(struct foo * )缓冲区;

fgets(缓冲区,SOMENUMBERWHATEVERFLOATSYOURBOAT,stdi n);

/ *现在访问结构的成员(使用例如bar -field1)。 />
*请注意,没有声明实际的结构 - 我们正在使用

*缓冲区,就好像它是结构* /

}

Here is a commonly used technique, that will, of course, work fine on
any reasonably modern, normal hardware. But, does it pass the CLC test?

/* Assume well-formed input - of course, you can always break it by
* feeding it bad input */

struct foo { int field1, field2; char nl; } *bar;
char buffer[SOMENUMBERWHATEVERFLOATSYOURBOAT];

int main(void) {
bar = (struct foo *) buffer;
fgets(buffer,SOMENUMBERWHATEVERFLOATSYOURBOAT,stdi n);
/* Now access the members of the struct (using, e.g., bar -field1).
* Note that no actual struct was ever declared - we are using
* buffer as if it were the struct */
}

推荐答案

2008年6月9日星期一17:08:20 +0000,Kenny McCormack写道:
On Mon, 09 Jun 2008 17:08:20 +0000, Kenny McCormack wrote:

这是一种常用的技术,
Here is a commonly used technique,



这是?你在哪里看过它?

It is? Where have you seen it used?


当然,在任何合理的现代普通硬件上工作正常。但是,它是否通过了CLC测试?
that will, of course, work fine on
any reasonably modern, normal hardware. But, does it pass the CLC test?



No.

No.


/ *假设格式良好的输入 - 当然,你总能打破它通过

*输入错误的输入* /


struct foo {int field1,field2; char nl; };酒吧;
/* Assume well-formed input - of course, you can always break it by
* feeding it bad input */

struct foo { int field1, field2; char nl; } *bar;



nl成员是什么?

What''s the nl member for?


char buffer [SOMENUMBERWHATEVERFLOATSYOURBOAT];


int main(无效){

bar =(struct foo *)buffer;
char buffer[SOMENUMBERWHATEVERFLOATSYOURBOAT];

int main(void) {
bar = (struct foo *) buffer;



这假设缓冲区适当地对齐struct foo。当你访问* bar $

This assumes that buffer is appropriately aligned for a struct foo. When
you access *bar, you also ignore C''s aliasing rules. Both problems can be
avoided by using a union.


fgets(buffer,SOMENUMBERWHATEVERFLOATSYOURBOAT,stdi n);
fgets(buffer,SOMENUMBERWHATEVERFLOATSYOURBOAT,stdi n);



你的意思是fread,还是你真的在询问fgets?如果你的意思是

fread,我根本不会看到nl成员的观点。如果你的意思是fgets,我不会在最后看到nl成员的意思。

Did you mean fread, or were you really asking about fgets? If you meant
fread, I don''t see the point of a nl member at all. If you meant fgets, I
don''t see the point of a nl member at the very end.


/ *现在访问结构的成员(使用例如bar -field1)。

*注意,没有声明实际的结构 - 我们正在使用

*缓冲区,就像它是结构* /

}
/* Now access the members of the struct (using, e.g., bar -field1).
* Note that no actual struct was ever declared - we are using
* buffer as if it were the struct */
}


Kenny McCormack< ga ***** @ xmission.xmission.comwrote:
Kenny McCormack <ga*****@xmission.xmission.comwrote:

这是一种常用的技术,当然,它可以在任何合理的现代普通硬件上正常工作。但是,它是否通过了CLC测试?
Here is a commonly used technique, that will, of course, work fine on
any reasonably modern, normal hardware. But, does it pass the CLC test?


/ *假设格式良好的输入 - 当然,你总是可以通过

*来打破它输入错误的输入* /
/* Assume well-formed input - of course, you can always break it by
* feeding it bad input */


struct foo {int field1,field2; char nl; } * bar;

char buffer [SOMENUMBERWHATEVERFLOATSYOURBOAT];
struct foo { int field1, field2; char nl; } *bar;
char buffer[SOMENUMBERWHATEVERFLOATSYOURBOAT];


int main(void){

bar =(struct foo *)buffer;

fgets(缓冲区,SOMENUMBERWHATEVERFLOATSYOURBOAT,stdi n);

/ *现在访问结构的成员(使用例如bar -field1)。

*注意没有实际结构曾被声明 - 我们正在使用

*缓冲区,就好像它是结构* /

}
int main(void) {
bar = (struct foo *) buffer;
fgets(buffer,SOMENUMBERWHATEVERFLOATSYOURBOAT,stdi n);
/* Now access the members of the struct (using, e.g., bar -field1).
* Note that no actual struct was ever declared - we are using
* buffer as if it were the struct */
}



只要sizeof(struct foo)不小于

SOMENUMBERWHATEVERFLOATSYOURBOAT那么就没有问题了。

它相当模糊,我敢怀疑这是'b $ ba'常用技术',但''缓冲''是你拥有的记忆

所以你可以随心所欲地使用它。

当然,所有这些都取决于你的主要原因是

输入结构良好(可能难以实现)

非结构良好的结构类型结构

在主流硬件上,但可能有一些

系统,其中某些位模式不代表整数

因此你可能会遇到未定义行为的危险。)

所以弄清楚什么是格式良好可能有点像

麻烦,但只要你这样做就没有问题。


问候,Jens

-

\ Jens Thoms Toerring ___ jt@toerring.de

\ __________________________ http://toerring.de


Kenny McCormack写道:
Kenny McCormack writes:

这是一种常用的技术,(...)
Here is a commonly used technique, (...)



我希望不是。

I hope not.


struct foo {int field1,field2; char nl; } * bar;

char buffer [SOMENUMBERWHATEVERFLOATSYOURBOAT];


int main(void){

bar =(struct foo * )缓冲区;

fgets(缓冲区,SOMENUMBERWHATEVERFLOATSYOURBOAT,stdi n);

/ *现在访问结构的成员(使用例如bar -field1)。
struct foo { int field1, field2; char nl; } *bar;
char buffer[SOMENUMBERWHATEVERFLOATSYOURBOAT];

int main(void) {
bar = (struct foo *) buffer;
fgets(buffer,SOMENUMBERWHATEVERFLOATSYOURBOAT,stdi n);
/* Now access the members of the struct (using, e.g., bar -field1).



如果在整数

表示可能的bar-> field1值中有一个0x10字节(换行符)。正如Harald所说,如果缓冲区未正确对齐struct foo,它会中断。


另外当我看到fgets()时我怀疑文件有用文本打开

而不是二进制模式,这意味着在换行符和文件系统的行尾代表之间转换

可能会有错误。


-

Hallvard

This breaks e.g. if there is a 0x10 byte (newline) in the integer
representation of the would-be bar->field1 value. And as Harald
said, it breaks if buffer is not properly aligned for a struct foo.

Also when I see fgets() I suspect the file has been opened in text
instead of binary mode, which means there may be bugs from converting
between newline and the file system''s representation of end-of-line.

--
Hallvard


这篇关于一个是语言律师的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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