{"id":3061,"date":"2020-04-05T20:52:18","date_gmt":"2020-04-05T12:52:18","guid":{"rendered":"https:\/\/www.aoisnow.net\/blog\/?p=3061"},"modified":"2020-04-05T20:52:18","modified_gmt":"2020-04-05T12:52:18","slug":"tont-36673-%e4%bf%9d%e6%8c%81%e9%94%99%e8%af%af%e4%bb%a3%e7%a0%81%e5%90%91%e4%b8%8b%e5%85%bc%e5%ae%b9%e7%9a%84%e9%87%8d%e8%a6%81%e6%80%a7","status":"publish","type":"post","link":"https:\/\/www.aoisnow.net\/blog\/archives\/3061","title":{"rendered":"TONT 36673 \u4fdd\u6301\u9519\u8bef\u4ee3\u7801\u5411\u4e0b\u517c\u5bb9\u7684\u91cd\u8981\u6027"},"content":{"rendered":"<p>\u539f\u6587\u94fe\u63a5\uff1a<a href=\"https:\/\/devblogs.microsoft.com\/oldnewthing\/20050118-00\/?p=36673\" target=\"_blank\" rel=\"noopener\">https:\/\/devblogs.microsoft.com\/oldnewthing\/20050118-00\/?p=36673<\/a><\/p>\n<p>I remember a bug report that came on in an old MS-DOS program (from a company that is still in business so don\u2019t ask me to identify them) that attempted to open the file \u201c\u201d. That\u2019s the file with no name.<\/p>\n<p>\u6211\u8bb0\u5f97\u6709\u4e00\u4e2abug\u62a5\u544a\uff0c\u662f\u5173\u4e8e\u4e00\u4e2a\u8001\u65e7\u7684 MS-DOS \u7a0b\u5e8f\uff08\u5f00\u53d1\u8fd9\u4e2a\u7a0b\u5e8f\u7684\u516c\u53f8\u76ee\u524d\u4ecd\u5728\u5b58\u7eed\u4e2d\uff0c\u6240\u4ee5\u4e0d\u8981\u95ee\u6211\u5177\u4f53\u662f\u54ea\u5bb6\u516c\u53f8\uff09\u5c1d\u8bd5\u6253\u5f00\u6587\u4ef6\u201c\u201d\uff0c\u4e5f\u5c31\u662f\u4e00\u4e2a\u6ca1\u6709\u540d\u5b57\u7684\u6587\u4ef6\u3002<\/p>\n<p>This returned error 2 (file not found). But the program didn\u2019t 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.<\/p>\n<p>\u8fd9\u6837\u505a\u4f1a\u4f7f\u7cfb\u7edf\u62a5\u544a\u9519\u8bef\u4ee3\u78012\uff08\u6587\u4ef6\u672a\u627e\u5230\uff09\uff0c\u4f46\u7a0b\u5e8f\u6ca1\u6709\u68c0\u67e5\u9519\u8bef\u4ee3\u7801\uff0c\u4ee5\u4e3a2\u5c31\u662f\u6587\u4ef6\u53e5\u67c4\uff0c\u7136\u540e\u5c31\u4f1a\u5f00\u59cb\u5411\u53e5\u67c42\u586b\u5145\u6570\u636e\uff0c\u800c\u6570\u636e\u4f1a\u663e\u793a\u5728\u5c4f\u5e55\u4e0a\uff0c\u800c\u8fd9\u662f\u56e0\u4e3a\u53e5\u67c42\u662f\u6807\u51c6\u9519\u8bef\u8f93\u51fa\u53e5\u67c4\uff0c\u5176\u9ed8\u8ba4\u884c\u4e3a\u5c31\u662f\u8f93\u51fa\u5230\u5c4f\u5e55\u4e0a\u3002<\/p>\n<p>It so happened that this program wanted to print the message to the screen anyway.<\/p>\n<p>\u78b0\u5de7\u8fd9\u4e2a\u7a0b\u5e8f\u8981\u505a\u7684\u5c31\u662f\u5411\u5c4f\u5e55\u8f93\u51fa\u6d88\u606f\u3002<\/p>\n<p>In other words, this program worked completely by accident.<\/p>\n<p>\u6362\u53e5\u8bdd\u8bf4\uff0c\u8fd9\u4e2a\u7a0b\u5e8f\u53ea\u662f\u649e\u4e86\u5927\u8fd0\u6b63\u5e38\u5de5\u4f5c\u4e86\u3002<\/p>\n<p>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.<br \/>\nWatch what happens.<\/p>\n<p>\u5728 Windows 95 \u53ef\u4ee5\u5b89\u88c5\u5728\u5176\u4e0a\u7684\u6587\u4ef6\u7cfb\u7edf\u8bbe\u8ba1\u7684\u51e0\u6b21\u53d8\u52a8\u4e2d\uff0c\u5176\u4e2d\u4e00\u4e2a\u526f\u4f5c\u7528\u662f\uff1a\u5c1d\u8bd5\u6253\u5f00\u4e00\u4e2a\u4e0d\u5b58\u5728\u7684\u6587\u4ef6\u56de\u62a5\u7684\u9519\u8bef\u4ee3\u7801\u4ece2\uff08\u6587\u4ef6\u4e0d\u5b58\u5728\uff09\u53d8\u6210\u4e863\uff08\u627e\u4e0d\u5230\u8def\u5f84\uff09\u3002\u73b0\u5728\u6765\u770b\u770b\u4f1a\u53d1\u751f\u4ec0\u4e48\u4e8b\u3002<\/p>\n<p>The program tries to open the file \u201c\u201d. Now it gets error 3 back. It mistakenly treats the 3 as a file handle and writes to it.<\/p>\n<p>\u7a0b\u5e8f\u5c1d\u8bd5\u6253\u5f00\u6587\u4ef6\u201c\u201d\uff08\u7a7a\u6587\u4ef6\u540d\uff09\u3002\u73b0\u5728\u5b83\u83b7\u5f97\u4e86\u9519\u8bef\u4ee3\u78013\u3002\u7a0b\u5e8f\u7167\u65e7\u8bef\u6253\u8bef\u649e\u5c063\u4f5c\u4e3a\u6587\u4ef6\u53e5\u67c4\uff0c\u5e76\u5f00\u59cb\u5411\u5176\u4e2d\u5199\u5165\u6570\u636e\u3002<\/p>\n<p>What is handle 3?<\/p>\n<p>\u90a3\u4e48\u53e5\u67c43\u662f\u4ec0\u4e48\u5462\uff1f<\/p>\n<p>The standard MS-DOS file handles are as follows:<\/p>\n<p>\u6807\u51c6\u7684 MS-DOS \u6587\u4ef6\u53e5\u67c4\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<table>\n<colgroup>\n<col align=\"right\" \/><\/colgroup>\n<tbody>\n<tr>\n<th bgcolor=\"#E0E0E0\">\u53e5\u67c4<\/th>\n<th bgcolor=\"#E0E0E0\">\u540d\u79f0<\/th>\n<th bgcolor=\"#E0E0E0\">\u542b\u4e49<\/th>\n<\/tr>\n<tr>\n<td>0<\/td>\n<td>stdin<\/td>\n<td>\u6807\u51c6\u8f93\u5165\u8bbe\u5907<\/td>\n<\/tr>\n<tr>\n<td>1<\/td>\n<td>stdout<\/td>\n<td>\u6807\u51c6\u8f93\u51fa\u8bbe\u5907<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>stderr<\/td>\n<td>\u6807\u51c6\u9519\u8bef\u8f93\u51fa<\/td>\n<\/tr>\n<tr>\n<td>3<\/td>\n<td class=\"\">stdaux<\/td>\n<td>\u6807\u51c6\u8f85\u52a9\u8bbe\u5907\uff08\u4e32\u53e3\uff09<\/td>\n<\/tr>\n<tr>\n<td>4<\/td>\n<td>stdprn<\/td>\n<td class=\"\">\u6807\u51c6\u6253\u5370\u673a<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>What happens when the program writes to handle 3?<\/p>\n<p>\u5f53\u7a0b\u5e8f\u5c1d\u8bd5\u5411\u53e5\u67c43\u5199\u5165\u65f6\u4f1a\u53d1\u751f\u4ec0\u4e48\u5462\uff1f<\/p>\n<p>It tries to write to the serial port.<\/p>\n<p>\u4f1a\u5c1d\u8bd5\u5411\u4e32\u53e3\u5199\u6570\u636e\u3002<\/p>\n<p>Most computers don\u2019t have anything hooked up to the serial port. The write hangs.<\/p>\n<p>\u5927\u591a\u6570\u8ba1\u7b97\u673a\u7684\u4e32\u53e3\u4e0a\u4ec0\u4e48\u4e5f\u6ca1\u8fde\uff0c\u6240\u4ee5\u5199\u64cd\u4f5c\u6302\u8d77\u4e86\u3002<\/p>\n<p>Result: Dead program.<\/p>\n<p>\u7ed3\u679c\u5c31\u662f\uff1a\u7a0b\u5e8f\u6b7b\u6389\u4e86\u3002<\/p>\n<p>The file system folks had to tweak their parameter validation so they returned error 2 in this case.<\/p>\n<p>\u6587\u4ef6\u7cfb\u7edf\u5f00\u53d1\u7ec4\u5bf9\u53c2\u6570\u6821\u9a8c\u505a\u4e86\u4e9b\u8c03\u6574\uff0c\u4f7f\u5176\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\u8fd4\u56de\u9519\u8bef\u4ee3\u78012\uff08\u6765\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff09\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u539f\u6587\u94fe\u63a5\uff1ahttps:\/\/devblogs.microsoft.com\/oldnewthing\/2005011 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-3061","post","type-post","status-publish","format-standard","hentry","category-tont_history"],"_links":{"self":[{"href":"https:\/\/www.aoisnow.net\/blog\/wp-json\/wp\/v2\/posts\/3061","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.aoisnow.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.aoisnow.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.aoisnow.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.aoisnow.net\/blog\/wp-json\/wp\/v2\/comments?post=3061"}],"version-history":[{"count":0,"href":"https:\/\/www.aoisnow.net\/blog\/wp-json\/wp\/v2\/posts\/3061\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.aoisnow.net\/blog\/wp-json\/wp\/v2\/media?parent=3061"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.aoisnow.net\/blog\/wp-json\/wp\/v2\/categories?post=3061"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.aoisnow.net\/blog\/wp-json\/wp\/v2\/tags?post=3061"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}