TONT 36673 保持错误代码向下兼容的重要性

原文链接:https://devblogs.microsoft.com/oldnewthing/20050118-00/?p=36673

I remember a bug report that came on in an old MS-DOS program (from a company that is still in business so don’t ask me to identify them) that attempted to open the file “”. That’s the file with no name.

我记得有一个bug报告,是关于一个老旧的 MS-DOS 程序(开发这个程序的公司目前仍在存续中,所以不要问我具体是哪家公司)尝试打开文件“”,也就是一个没有名字的文件。

This returned error 2 (file not found). But the program didn’t check the error code and though that 2 was the file handle. It then began writing data to handle 2, which ended up going to the screen because handle 2 is the standard error handle, which by default goes to the screen.

这样做会使系统报告错误代码2(文件未找到),但程序没有检查错误代码,以为2就是文件句柄,然后就会开始向句柄2填充数据,而数据会显示在屏幕上,而这是因为句柄2是标准错误输出句柄,其默认行为就是输出到屏幕上。

It so happened that this program wanted to print the message to the screen anyway.

碰巧这个程序要做的就是向屏幕输出消息。

In other words, this program worked completely by accident.

换句话说,这个程序只是撞了大运正常工作了。

Due to various changes to the installable file system in Windows 95, the error code for attempting to open the null file changed from 2 (file not found) to 3 (path not found) as a side-effect.
Watch what happens.

在 Windows 95 可以安装在其上的文件系统设计的几次变动中,其中一个副作用是:尝试打开一个不存在的文件回报的错误代码从2(文件不存在)变成了3(找不到路径)。现在来看看会发生什么事。

The program tries to open the file “”. Now it gets error 3 back. It mistakenly treats the 3 as a file handle and writes to it.

程序尝试打开文件“”(空文件名)。现在它获得了错误代码3。程序照旧误打误撞将3作为文件句柄,并开始向其中写入数据。

What is handle 3?

那么句柄3是什么呢?

The standard MS-DOS file handles are as follows:

标准的 MS-DOS 文件句柄如下所示:

句柄 名称 含义
0 stdin 标准输入设备
1 stdout 标准输出设备
2 stderr 标准错误输出
3 stdaux 标准辅助设备(串口)
4 stdprn 标准打印机

What happens when the program writes to handle 3?

当程序尝试向句柄3写入时会发生什么呢?

It tries to write to the serial port.

会尝试向串口写数据。

Most computers don’t have anything hooked up to the serial port. The write hangs.

大多数计算机的串口上什么也没连,所以写操作挂起了。

Result: Dead program.

结果就是:程序死掉了。

The file system folks had to tweak their parameter validation so they returned error 2 in this case.

文件系统开发组对参数校验做了些调整,使其在这种情况下返回错误代码2(来解决这个问题)。

Comments

  1. 讲道理我到现在都没有想清楚串口的另一边会有什么,除了听说过它可以用来硬件调试。

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

 剩余字数 ( Characters available )

Your comment will be available after auditing.
您的评论将在通过审核后显示。

Please DO NOT add any links in your comment, otherwise it would be identified as SPAM automatically and never be audited.
请不要在评论中插入任何链接,否则将被自动归类为垃圾评论,且永远不会被提交给博主进行复审。

*