Why glibc's fclose(NULL) cause segmentation fault instead of returning error?

后端 未结 5 796
天命终不由人
天命终不由人 2020-12-03 05:02

According to man page fclose(3):

RETURN VALUE

Upon successful completion 0 is returned. Otherwise, EOF is return

5条回答
  •  借酒劲吻你
    2020-12-03 05:53

    This fclose() issue seems to be a legacy of FreeBSD, and was accepted uncritically by both the Microsoft and Linux camps.

    But HP, SGI, Solaris, and CYGWIN, on the other hand, all handle fclose(NULL) reasonably. For example, man fclose for CYGWIN, which uses newlib rather than the OP's glibc, states:

    fclose returns 0 if successful (including when FP is NULL or not an open file)

    See https://stackoverflow.com/a/8442421/318716 for a related discussion.

提交回复
热议问题