gpt4 book ai didi

c - strncpy 调用的段错误

转载 作者:太空宇宙 更新时间:2023-11-04 07:44:55 25 4
gpt4 key购买 nike

我想在 C 中模拟一个向下增长的调用堆栈并将以下内容压入堆栈:

enter image description here

这是我写的测试代码,我只尝试压入字符串,字对齐,然后将地址压入刚刚压入的字符串:

#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <stdlib.h>

int main(){

/*Initialize stack */
size_t stack_size = (size_t) pow(2,10);
uint8_t *esp; /*Use byte-addressable stack pointer */
esp = (uint8_t *) malloc(stack_size);

esp += stack_size - 1; /*Set esp to top of allocated memory,
since stack grows downwards */

/* Parse the string into its tokens */
char s[] = "/bin/ls -l foo bar";
char *tokens[4];
char *token = strtok(s, " ");
int num_tokens = 0;
while (token != NULL)
{
tokens[num_tokens++] = token;
token = strtok(NULL, " ");
}


size_t esp_iter = 0; /*number of bytes pushed,
needed for word alignment */

char *stack_pointers[num_tokens]; /* Array to store addresses of
strings that were pushed */

/* Push the char arrays on the stack */
for (int j = 0; j < num_tokens; j++)
{
esp_iter += strlen(tokens[j]) + 1; /* +1 because of ‘\0’, which is
included in strncpy */

/* Address to store next string into */
char *next_pointer = (char *) ((uint8_t *) ( esp - esp_iter));

/* Store address in stack to which token j was pushed */
stack_pointers[j] = strncpy(next_pointer, tokens[j],
strlen(tokens[num_tokens]) + 1);
}

/* word aligning */
size_t pad = 4 - esp_iter % 4;
for (int j = 0; j < (int) pad; j++)
{
esp_iter += 1;
}

/* Push pointers to previously pushed strings */
for (int j = 0; j < num_tokens; j++)
{
esp_iter -= sizeof(char *);

/* Address on stack to store address of previously
pushed token to */
char *stack_pointer = (char *) (esp - esp_iter);
stack_pointer = (char *) stack_pointers[j];
}
return 0;
}

我想在字节级寻址堆栈,所以我使用 (uint8_t *) 作为堆栈指针。然后我应该能够在对 strncpy 的调用中使用字符串的长度 + 1(对于'\0',它包含在 strncpy 中)。但是 strncpy 会产生段错误,我不明白为什么。我将内存从 0x7fc4c5001000 分配到 0x7fc4c50013ff。 Esp 设置为 0x7fc4c50013ff,然后我想将“bar”压入堆栈,所以我将堆栈指针递减 4(堆栈向较低的内存地址增长)并使用目标地址 0x7fc4c50013fb 调用 strncpy,这应该有足够的空间来推这 4 个字符。为什么我会在这里出现段错误?

最佳答案

在修复你的代码后快速运行它会编译告诉我们段错误来自 strlen(),而不是 strncpy():

stack_pointers[j] = strncpy(next_pointer, tokens[j], strlen(tokens[i]) + 1);

tokens[i] 为空。

在你的代码的前面,你做了:

int i = 0;
while (token != NULL){
tokens[i++] = token;
printf("%s \n", token);
token = strtok(NULL, " ");
}

这会将 i 递增到 4,因为数组只包含 4 个字符串,而您正在访问第五个位置,自然地,strlen() 在访问外部时会出现段错误数组。

#0  __strlen_sse42 () at ../sysdeps/x86_64/multiarch/strlen-sse4.S:32
#1 0x00000000004007b8 in main () at main.c:32
(gdb) f 1
#1 0x00000000004007b8 in main () at main.c:32
32 stack_pointers[j] = strncpy(next_pointer, tokens[j], strlen(tokens[i]) + 1);
(gdb) print tokens[i]
$1 = 0x0
(gdb) print i
$2 = 4
(gdb) print tokens
$3 = {0x7fffffffe020 "/bin/ls", 0x7fffffffe028 "-l", 0x7fffffffe02b "foo", 0x7fffffffe02f "bar",
0x0}

关于c - strncpy 调用的段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57889903/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com