每当我打电话给vasprintf()
功能errno
设置为 11(资源暂时不可用)。然而,看起来一切都运行正常。为了更好地理解错误的根源,我找到了一个实现vasprintf()
在 uclibc 中并将其放入我的程序中。我发现的是fflush()
正在设置errno
至 11。然而,所有迹象都表明代码运行正常。例如,返回值来自fflush()
is 0
. The size
价值open_memstream()
文件关闭后正确更新。输出缓冲区已正确更新。我还打电话给output()
在无限循环中运行函数来查看是否有内存泄漏,但我发现在几千个循环中内存没有增加。
如果文件已关闭且数据已写入,是否确实存在需要解决的错误?
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <errno.h>
void output(int type, const char *fmt, ...)
{
FILE *f;
size_t size;
int rv = -1;
int fclose_return = 5;
int fflush_return = 5;
va_list ap;
char *output_str_no_prefix = NULL;
va_start(ap, fmt);
// vasprintf(&output_str_no_prefix, fmt, ap);
if ((f = open_memstream(&output_str_no_prefix, &size)) != NULL) {
rv = vfprintf(f, fmt, ap);
errno = 0;
printf("%s: errno(%d): %s -- Return Value: %d\n",
__func__, errno, strerror(errno), fflush_return);
fflush_return = fflush(f);
printf("%s: errno(%d): %s -- Return Value: %d\n",
__func__, errno, strerror(errno), fflush_return);
errno=0;
fclose_return = fclose(f);
printf("%s: errno(%d): %s -- Return Value: %d\n",
__func__, errno, strerror(errno), fclose_return);
if (rv < 0) {
free(output_str_no_prefix);
output_str_no_prefix = NULL;
} else {
output_str_no_prefix = realloc(output_str_no_prefix, rv + 1);
}
}
va_end(ap);
printf ("%s\n", output_str_no_prefix);
free(output_str_no_prefix);
}
int main () {
output(0, "Hello! -- %d\n", 4);
return 0;
}
这是上面程序的输出。
# /data/HelloWorld
output: errno(0): Success -- Return Value: 5
output: errno(11): Resource temporarily unavailable -- Return Value: 0
output: errno(0): Success -- Return Value: 0
Hello! -- 4
#