Skip to content

输出长日志崩溃 #730

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wanglxchina opened this issue Apr 25, 2025 · 1 comment
Open

输出长日志崩溃 #730

wanglxchina opened this issue Apr 25, 2025 · 1 comment

Comments

@wanglxchina
Copy link

vsnprintf用法貌似不对

#include <stdio.h>
#include <stdarg.h>

int vsnprintf(char *str, size_t size, const char *format, va_list ap);
  1. 如果输出的字符串长度小于 size,返回实际写入的字符数(不包括字符串结束符 '\0')。
  2. 如果输出的字符串长度大于等于 size,返回如果有足够空间时本应写入的字符数(不包括字符串结束符 '\0')。这意味着返回值可能会大于 size - 1。

hlog.c下面这段代码没有考虑第二种长度超出size的情况,造成len+=..后len值错误

case 's':
{
    va_list ap;
    va_start(ap, fmt);
    len += vsnprintf(buf + len, bufsize - len, fmt, ap);
    va_end(ap);
}
@House-Men
Copy link
Contributor

这并不是这一处vsnprintf用法问题,还有其他地方也存在可能因超出bufSize导致崩溃,根本问题在于hlog并不是防御性编码风格主动做了参数检查防止超出bufSize,这样的函数属于契约式编码,要求打印log时调用者必须确保绝不超过bufSize,默认bufSize=16k,可通过logger_set_max_bufsize调整。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants