android -- ndk (stack corruption detected)
程序员文章站
2022-04-01 13:02:53
针对于android-ndk报错,一眼看上去确实懵逼,采用了最土的方法:“加调试,打印出问题点”。主要是要记录下,“stack corruption detected” 应该第一时间考虑到是堆栈溢出了报错信息pid: 6411, tid: 6411, name: m.test.myplayer >>> com.test.myplayer <<
针对于android-ndk报错,一眼看上去确实懵逼,采用了最土的方法:“加调试,打印出问题点”。
主要是要记录下,“stack corruption detected” 应该第一时间考虑到是堆栈溢出了
报错信息
pid: 6411, tid: 6411, name: m.test.myplayer >>> com.test.myplayer <<<
signal 6 (SIGABRT), code -6 (SI_TKILL), fault addr --------
Abort message: 'stack corruption detected (-fstack-protector)'
r0 00000000 r1 0000190b r2 00000006 r3 00000008
r4 0000190b r5 0000190b r6 bea8cd74 r7 0000010c
r8 00000000 r9 b14a7000 sl bea8de30 fp b14a7000
ip bea8de08 sp bea8cd60 lr b3df2055 pc b3dec298 cpsr 20070030
代码:
此代码也是学习雷神的博客中进行实验的
雷神的最简单的基于FFmpeg的移动端实验
改进前的代码
extern "C"
jstring
Java_com_test_myplayer_MyPlayer_avformatinfo
(JNIEnv *env, jobject obj) {
char info[4096] = {0};
av_register_all();
AVInputFormat *if_temp = av_iformat_next(NULL);
AVOutputFormat *of_temp = av_oformat_next(NULL);
//Input
while(if_temp!=NULL) {
// 如下就是隐患的地方, info的大小可能大于预设定栈的大小 4096
sprintf(info, "%s[In][%10s]\n", info, if_temp->name);
if_temp = if_temp->next;
}
while (of_temp != NULL) {
sprintf(info, "%s[Out][%10s]\n", info, of_temp->name);
of_temp = of_temp->next;
}
return (env)->NewStringUTF(info);
}
改进后的代码
#define BUFFER_SIZE 8192
extern "C"
jstring
Java_com_test_myplayer_MyPlayer_avformatinfo
(JNIEnv *env, jobject obj) {
char info[BUFFER_SIZE] = {0};
av_register_all();
AVInputFormat *if_temp = av_iformat_next(NULL);
AVOutputFormat *of_temp = av_oformat_next(NULL);
//Input
while(if_temp!=NULL) {
if(strlen(info) <= 0) {
sprintf(info, "[In][%10s]\n", if_temp->name);
} else {
// 这里需要考虑 sprintf 拷贝的时候除了info,有把[In][]\n字符拷贝进去
int buffsize = strlen(info) + strlen(if_temp->name) + strlen("[In][") + 2;
// buffsize + 1 ,考虑到应该要给字符串一个末尾填充符
if(buffsize + 1 < BUFFER_SIZE) {
sprintf(info, "%s[In][%10s]\n", info, if_temp->name);
}
}
if_temp = if_temp->next;
}
//Output
while (of_temp != NULL) {
int buffsize = strlen(info) + strlen(of_temp->name) + strlen("[Out][") + 2;
if(buffsize + 1 < BUFFER_SIZE) {
sprintf(info, "%s[Out][%10s]\n", info, of_temp->name);
of_temp = of_temp->next;
}
}
return (env)->NewStringUTF(info);
}
本文地址:https://blog.csdn.net/LHshooter/article/details/107578097