memset 和 SIGSEGV

发布于 2024-07-13 18:15:36 字数 934 浏览 6 评论 0原文

我在一段代码中遇到了一个奇怪的问题。

void app_ErrDesc(char *ps_logbuf, char *pc_buf_err_recno)
 {
    char *pc_logbuf_in;
    char rec_num[10];
    char *y = "|";
    int i, j;

    memset(rec_num, 0, sizeof(rec_num));
    memset(pc_buf_err_recno, 0, LOGBUFF);
        .....
        .....
 }

由于某种原因,第一个 memset 调用会发送 SIGSEGV。 更奇怪的是当 在 gdb 中,尽管调用了该函数,但同一行执行了大约 30 次 仅一次且内部没有循环! 这是一段 gdb 会话。

7295            /*Point to logbuffer string*/
(gdb)
7292            memset(rec_num, 0, sizeof(rec_num));
(gdb)
7295            /*Point to logbuffer string*/
(gdb)
7292            memset(rec_num, 0, sizeof(rec_num));
(gdb) n
7295            /*Point to logbuffer string*/
(gdb)
7292            memset(rec_num, 0, sizeof(rec_num));
(gdb)

程序收到信号 SIGSEGV,分段错误。

我还尝试通过 valgrind 的 memcheck 工具运行该程序,但没有得到有关上述代码段的任何重要信息。

我正在解析的文件只有一条记录。

任何指示表示赞赏。 谢谢。

I have been facing a weird issue in a piece of code.

void app_ErrDesc(char *ps_logbuf, char *pc_buf_err_recno)
 {
    char *pc_logbuf_in;
    char rec_num[10];
    char *y = "|";
    int i, j;

    memset(rec_num, 0, sizeof(rec_num));
    memset(pc_buf_err_recno, 0, LOGBUFF);
        .....
        .....
 }

For some reason the first memset call sends a SIGSEGV. Whats more strange is when
inside gdb the same line executes for about 30 times though the function is called
only once and there are no loops inside! Here's a piece of gdb session.

7295            /*Point to logbuffer string*/
(gdb)
7292            memset(rec_num, 0, sizeof(rec_num));
(gdb)
7295            /*Point to logbuffer string*/
(gdb)
7292            memset(rec_num, 0, sizeof(rec_num));
(gdb) n
7295            /*Point to logbuffer string*/
(gdb)
7292            memset(rec_num, 0, sizeof(rec_num));
(gdb)

Program received signal SIGSEGV, Segmentation fault.

I have also tried running the program through valgrind's memcheck tool but not getting anything significant about the above piece of code.

The file that I'm parsing has just one record.

Any pointers are appreciated. Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

萌辣 2024-07-20 18:15:40

它很可能是第二个 memset,原因是调用外部函数时缓冲区大小不足。 调试器可能会错误地显示您所在的位置。 尝试在每个步骤后添加日志记录,以找出到底发生了什么崩溃。

It's likely that it's the second memset and the reason is that the outer function is called with an insufficient buffer size. Debuggers can show incorrectly where you are. Try to add logging after each step to find out what exactly crashes.

黯淡〆 2024-07-20 18:15:40

我怀疑对该函数的调用,因此请确保该调用不是类似的

char pc_buf_err_recno[SMALLER_THAN_LOGBUFF];
char ps_logbuf[TOO_SMALL]
app_ErrDesc(ps_logbuf, pc_buf_err_recno);

i suspect the call to the function, so ensure the call is not something like

char pc_buf_err_recno[SMALLER_THAN_LOGBUFF];
char ps_logbuf[TOO_SMALL]
app_ErrDesc(ps_logbuf, pc_buf_err_recno);
听你说爱我 2024-07-20 18:15:40

调试器可能不正确,特别是当您获得 SEGV 时。 请记住,当出现分段错误时,您很可能已经破坏了堆栈,并且如果发生这种情况,调试器会感到困惑。

也很可能是调用函数弄乱了,而不是当前函数。

Debuggers can be incorrect, particularly if you're getting SEGV. Remember, it's quite possible you've trashed the stack when you get a segmentation fault and the debugger will get confused if that happens.

It's also quite possible the calling function has made a mess, not the current one.

維他命╮ 2024-07-20 18:15:40

更奇怪的是,在 gdb 内部,同一行执行了大约 30 次,尽管该函数只被调用一次并且内部没有循环!

这听起来是经过优化编译的症状。 如果您在关闭优化的情况下进行编译,您可能会更容易地查明 GDB 中的问题。

Whats more strange is when inside gdb the same line executes for about 30 times though the function is called only once and there are no loops inside!

This sounds symptomatic of having compiled with optimizations. You may have an easier time pinpointing the problem in GDB if you compile with optimizations turned off.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文