最新消息:20190717 VPS服务器:Vultr新加坡,WordPress主题:大前端D8,统一介绍入口:关于

sprintf中关于参数是字符串时候的问题

工作和技术 crifan 659浏览 0评论

char *strNo = "no";
char *strYes="yes";

len += sprintf(buf + len, “%s”, "connectedt: %sn", connected ? strYes : strNo);
没问题,

但是如果是:

char strNo[5] = "no";
char strYes[5]="yes";

再用
len += sprintf(buf + len, “%s”, "connectedt: %sn", connected ? strYes : strNo);

就有问题了,会出现类似如下的问题:

[ 1139.960000] Unable to handle kernel paging request at virtual address 1b9f3779
[ 1139.970000] pgd = c38f4000
[ 1139.970000] [1b9f3779] *pgd=00000000
[ 1139.980000] Internal error: Oops: 5 [#2]
[ 1139.980000] Modules linked in: as352x_system_monitor as352x_afe_charger
[ 1139.980000] CPU: 0    Not tainted (2.6.22.2 #49)
[ 1139.980000] PC is at strnlen+0x18/0x54
[ 1139.980000] LR is at vsnprintf+0x510/0x77c
[ 1139.980000] pc : [<c012c764>]    lr : [<c012e04c>]    psr: 20000013
[ 1139.980000] sp : c3955e64 ip : c3955e74 fp : c3955e70
[ 1139.980000] r10: ffffffff r9 : 1b9f3779 r8 : c3955ed4
[ 1139.980000] r7 : c3955ed8 r6 : ffffffff r5 : 00000000 r4 : c3947020
[ 1139.980000] r3 : c028e94c r2 : 1b9f3779 r1 : 00000004 r0 : 1b9f3779
[ 1139.980000] Flags: nzCv IRQs on FIQs on Mode SVC_32 Segment user
[ 1139.980000] Control: c000717f Table: 338f4000 DAC: 00000015
[ 1139.980000] Process cat (pid: 712, stack limit = 0xc3954258)
[ 1139.980000] Stack: (0xc3955e64 to 0xc3956000)
[ 1139.980000] 5e60:          c3955eb8 c3955e74 c012e04c c012c75c c3955ee2 00000000 c3955ec4
[ 1139.980000] 5e80: c3955e54 3c6b8fdf c3947020 bf0037c3 c0000000 c03bd0c0 c03bd0c0 c3947000
[ 1139.980000] 5ea0: bf0043c4 c3955ee2 00000000 c3955ecc c3955ebc c012e380 c012db4c c3955ed4
[ 1139.980000] 5ec0: c3000000 c3955edc bf003200 c012e368 bf0037c0 00000004 1b9f3779 c3955ee7
[ 1139.980000] 5ee0: 6579caac 6e000073 0000006f 00000000 be88caac 00001000 00000000 00000c00
[ 1139.980000] 5f00: c3947000 c3955f80 c3955f58 c3955f18 c00be76c bf003090 c3955f2c c03bd0c0
[ 1139.980000] 5f20: c3d862a0 c3954000 00000000 00000000 c03aabc0 be88caac c3955f80 00001000
[ 1139.980000] 5f40: c0022004 c3954000 00000001 c3955f7c c3955f5c c0085ca8 c00be550 00035000
[ 1139.980000] 5f60: 00000000 00000000 c03aabc0 00000003 c3955fa4 c3955f80 c0086130 c0085c00
[ 1139.980000] 5f80: 00000000 00000000 00000000 00001000 be88caac 00000003 00000000 c3955fa8
[ 1139.980000] 5fa0: c0021e60 c00860f4 00001000 be88caac 00000003 be88caac 00001000 00000001
[ 1139.980000] 5fc0: 00001000 be88caac 00000003 0010b1b8 00000000 be88caac 00000001 00000003
[ 1139.980000] 5fe0: 00000002 be88ca8c 0003567c 0007e954 60000010 00000003 0000003c 00000044
[ 1139.980000] Backtrace:
[ 1139.980000] [<c012c74c>] (strnlen+0x0/0x54) from [<c012e04c>] (vsnprintf+0x510/0x77c)
[ 1139.980000] [<c012db3c>] (vsnprintf+0x0/0x77c) from [<c012e380>] (sprintf+0x2c/0x34)
[ 1139.980000] [<c012e354>] (sprintf+0x0/0x34) from [<bf003200>] (as352xsysmon_read_proc+0x180/0x334 [as352x_system_monitor])
[ 1139.980000] r3:1b9f3779 r2:00000004 r1:bf0037c0
[ 1139.980000] Backtrace aborted due to bad frame pointer <c3000000>
[ 1139.980000] Code: e24cb004 e3510000 e1a02000 0a00000c (e5d03000)

在去调用strnlen的时候,出问题。

转载请注明:在路上 » sprintf中关于参数是字符串时候的问题

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
56 queries in 0.269 seconds, using 18.82MB memory