当流为空时,fwrite 和 fread 等操作是否有未定义的行为?

Do operations such as fwrite and fread have undefined behavior when stream is null?

我专门在 C11 标准 (ISO/IEC 9899:2011) 或 POSIX 页面中查找该词。我检查了 POSIX manual for fwrite out, but it doesn't mention undefined behavior at all. However, the manual for fclose 确实说

After the call to fclose(), any use of stream results in undefined behavior.

但我仍然没有看到 NULL 流是否导致 UB。

fread()fwrite() 都期望 stream 参数是通过成功调用 fopen() 得到的值 return ]、fdopen()freopen()。由于这些函数 return NULL 出错,stream 参数不能为 NULL。

因为手册页没有说明当 stream 不是通过成功调用 fopen() 等得到的值 return 时会发生什么。这意味着手册页没有说明当 stream 不是通过成功调用 fopen() 等得到的值 return 时会发生什么,或者换句话说,行为未定义。

另请参阅此板上问题“Why glibc's fclose(NULL) cause segmentation fault instead of returning error”的已接受答案。