g_string_truncate () 上的分段错误?

发布于 2024-10-16 07:56:58 字数 1280 浏览 2 评论 0原文

在 C 中尝试以下操作时:

g_string_printf(qbuf,"INSERT INTO inbox (number, smsdate, text) VALUES ('%s','%04d-%02d-%02d %02d:%02d:%02d', '%s')",
          xmx.remote.number,
          xmx.smsc_time.year,
          xmx.smsc_time.month,
          xmx.smsc_time.day,
          xmx.smsc_time.hour,
          xmx.smsc_time.minute,
          xmx.smsc_time.second,
          xmx.user_data[0].u.text);

我看到以下崩溃:

Program received signal SIGSEGV, Segmentation fault.
0x00984809 in g_string_truncate () from /lib/libglib-2.0.so.0
(gdb) 

为什么会发生这种情况?调用 g_string_printf() 之前是否有任何启动?

从第2帧开始:

(gdb) frame 2
#2  0x08049ba8 in fetching_phone (unit=0x807cd80) at main.c:152
152           g_string_printf(qbuf,"INSERT INTO inbox (number, smsdate, text) VALUES ('%s','%04d-%02d-%02d %02d:%02d:%02d', '%s')",
(gdb) ptype xmx.remote.number
type = char [40]
(gdb) ptype xmx.smsc_time.year
type = int
(gdb) ptype xmx.smsc_time.month
type = int
(gdb) ptype xmx.smsc_time.day
type = int
(gdb) ptype xmx.smsc_time.hour
type = int
(gdb) ptype xmx.smsc_time.minute
type = int
(gdb) ptype xmx.smsc_time.second
type = int
(gdb) ptype xmx.user_data[0].u.text
type = unsigned char [1601]
(gdb) 

但是,我仍然找不到问题出在哪里。

When trying the following in C :

g_string_printf(qbuf,"INSERT INTO inbox (number, smsdate, text) VALUES ('%s','%04d-%02d-%02d %02d:%02d:%02d', '%s')",
          xmx.remote.number,
          xmx.smsc_time.year,
          xmx.smsc_time.month,
          xmx.smsc_time.day,
          xmx.smsc_time.hour,
          xmx.smsc_time.minute,
          xmx.smsc_time.second,
          xmx.user_data[0].u.text);

I see the following crash:

Program received signal SIGSEGV, Segmentation fault.
0x00984809 in g_string_truncate () from /lib/libglib-2.0.so.0
(gdb) 

Why would this happen? Is there any initiation before calling g_string_printf() ?

From frame 2:

(gdb) frame 2
#2  0x08049ba8 in fetching_phone (unit=0x807cd80) at main.c:152
152           g_string_printf(qbuf,"INSERT INTO inbox (number, smsdate, text) VALUES ('%s','%04d-%02d-%02d %02d:%02d:%02d', '%s')",
(gdb) ptype xmx.remote.number
type = char [40]
(gdb) ptype xmx.smsc_time.year
type = int
(gdb) ptype xmx.smsc_time.month
type = int
(gdb) ptype xmx.smsc_time.day
type = int
(gdb) ptype xmx.smsc_time.hour
type = int
(gdb) ptype xmx.smsc_time.minute
type = int
(gdb) ptype xmx.smsc_time.second
type = int
(gdb) ptype xmx.user_data[0].u.text
type = unsigned char [1601]
(gdb) 

But, I still can't find where the problem is.

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

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

发布评论

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

评论(2

坦然微笑 2024-10-23 07:56:58

您可能对“%s”字段有一个错误的指针。
当您运行 gdb 时,您可以执行以下操作:

(gdb) bt
...trace...
# see the frame # of your call to g_string_printf()
(gdb) frame 5 # considering it was 5
(gdb) print xmx.remote.number
(gdb) print xmx.user_data[0].u.text
(gdb) print *xmx.remote.number
(gdb) print *xmx.user_data[0].u.text

或者您也可以检查类型(xmx.remote.number 是指针吗?)

(gdb) ptype xmx.remote.number

You probably have a bad pointer for the '%s' fields.
As you are running gdb, here is what you can do:

(gdb) bt
...trace...
# see the frame # of your call to g_string_printf()
(gdb) frame 5 # considering it was 5
(gdb) print xmx.remote.number
(gdb) print xmx.user_data[0].u.text
(gdb) print *xmx.remote.number
(gdb) print *xmx.user_data[0].u.text

or you can also check types (is xmx.remote.number a pointer ?)

(gdb) ptype xmx.remote.number
戏舞 2024-10-23 07:56:58

您初始化了qbuf吗?

GString *qbuf = g_string_new("");

Did you initialize qbuf?

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