如何故意触发 fgets() 中的错误?

How do I intentionally trigger an error in fgets()?

本文关键字:错误 fgets 何故意 故意      更新时间:2023-10-16

摘要

使用fgets()时,我有错误检查代码,可以在fgets()返回null但尚未到达文件末尾的情况下执行一些修复。我想练习这部分代码来验证它是否按预期工作。

有没有一种规范的方法可以触发fgets()失败?要么通过手动方式(以某种方式删除fgets()调用之间的文件),一些测试设置(提供故意"损坏"的文件),还是其他什么?


最小可重现性示例

fgets_fail_test.cpp:

// Test code to show minimally reproducible example of fgets() error handling
// The desire is to manually trigger a failure in fgets()
#include <errno.h>
#include <iostream>
#include <stdio.h>
#include <string.h>
int main(int argc, char **argv) {
FILE *fp = fopen("test_file.txt", "r"); //assume this works, not testing this
char buf[100]; //assume large enough to hold data
unsigned int lineno = 1;
while (fgets(buf, 100, fp)) {
std::cout << buf;
lineno++;
}
if (feof(fp)) {
std::cout << "End of file encountered.n";
}
else if (ferror(fp)) { // how do I trigger an fgets error to cause this to return true?
printf("Encountered read error at line no %u. Error: %d, %s.n",
lineno,
errno,
strerror(errno));
}
fclose(fp); // assume this works, not testing this
return 0;
}

test_file.txt:

ABCDEFG
HIJKLMN
OPQRSTU
VWXYZ
The quick brown fox
jumped over the
lazy dog.
Goodbye.

理论

虽然我可以简单地用一些测试脚手架替换有问题的代码(编写一个保持内部状态的 fgets 包装器,增加一个计数器,一旦它到达第 #N 行,它就会返回null并手动设置文件错误和 errno)我觉得应该有一些"内置"方法来做到这一点?

如果唯一的解决方案是脚手架,我会这样做。老实说,也许我在这里试图变得太聪明了。

在一些相关和链接的问题中给出了很多很好的答案,这可能会使我的问题重复。虽然我在进行初始搜索时专注于fgets(),所以我无法找到这些。

一些相关问题是:

  • 导致 I/O 错误的特殊文件
  • 生成读取错误

由于我的程序从具有给定文件名格式的已知文件列表中读取数据,因此我需要使用一种允许我向程序提供错误文件的方法(而无需修改代码)。我决定用这个答案来做到这一点。

方法

为了生成一个会触发fgets()读取失败的错误文件,我利用了/proc/self/mem的"功能",即从中读取会导致I/O错误。

我删除了旧的输入文件,并将旧名称下的新输入文件链接到/proc/self/mem

ln -s /proc/self/mem test_file.txt

然后运行该文件生成以下输出:

Encountered read error at line no 1. Error: 5, Input/output error.

指示我们触发了错误。感谢@Artyer的评论,他们最初将我链接到相关问题。