// fopen is protected by fdsan, replace open with fopen
// int fd = open(DEV_NULL_FILE, O_RDONLY);
FILE *f = fopen(DEV_NULL_FILE, O_RDONLY);
sleep(3);
ssize_t ret = write(fileno(f), "fdsan test\n", 11);
if (ret == -1) {
OH_LOG_ERROR(LOG_APP, "good write but failed?!");
}
close(fileno(f));
}
复制代码
使用fopen打开的每个文件描述符都必要有一个与之对应的 tag 。fdsan 在 close 时会查抄关闭的 fd 是否与 tag 匹配,不匹配就会默认提示相干日记信息。下面是上述代码的日记信息:
# hilog | grep MUSL-FDSAN
04-30 15:03:41.760 10933 1624 E C03f00/MUSL-FDSAN: attempted to close file descriptor 43, expected to be unowned, actually owned by FILE* 0x00000000f7b90aa2