c-scandir()导致segfault,即使使用有效的绝对路径



我不确定这里缺少了什么,但我已经盯着这个看了很长一段时间了。在扫描一个似乎有效的目录时,我似乎遇到了分段错误。我只是想打印所有的目录条目和子目录条目。gdb运行和回溯:

(gdb) run
Starting program: /home/p/C_projects/recursive_ls/a.out 
Directory is now /home/p/C_projects/recursive_ls
In /home/p/C_projects/recursive_ls: a.out
In /home/p/C_projects/recursive_ls: recursive_ls.c
In /home/p/C_projects/recursive_ls: test
In /home/p/C_projects/recursive_ls: test_1
In /home/p/C_projects/recursive_ls: test_2
a.out,inode=265490,owner=1000,size=10328
recursive_ls.c,inode=265577,owner=1000,size=1296
test,inode=265508,owner=1000,size=4096
Directory is now /home/p/C_projects/recursive_ls/test
Program received signal SIGSEGV, Segmentation fault.
0x00000003 in ?? ()
(gdb) backtrace
#0  0x00000003 in ?? ()
#1  0xb7eb5d5c in __alloc_dir (fd=3, close_fd=close_fd@entry=true, 
    flags=flags@entry=0, statp=0x0) at ../sysdeps/posix/opendir.c:207
#2  0xb7eb5e6a in __opendirat (dfd=dfd@entry=-100, 
    name=name@entry=0x804b938 "/home/p/C_projects/recursive_ls/test")
    at ../sysdeps/posix/opendir.c:152
#3  0xb7eb6c53 in __GI_scandirat (dfd=-100, 
    dir=0x804b938 "/home/p/C_projects/recursive_ls/test", namelist=0xbffff02c, 
    select=0x804862b <isSelect>, cmp=0x8048520 <alphasort@plt>)
    at scandirat.c:62
#4  0xb7eb62ca in scandir (
    dir=0x804b938 "/home/p/C_projects/recursive_ls/test", namelist=0xbffff02c, 
    select=0x804862b <isSelect>, cmp=0x8048520 <alphasort@plt>) at scandir.c:43
#5  0x080486c5 in getFiles (
    path=0x804b938 "/home/p/C_projects/recursive_ls/test") at recursive_ls.c:23
#6  0x0804886c in getFiles (path=0x804b008 "/home/p/C_projects/recursive_ls")
    at recursive_ls.c:40
#7  0x080488dd in main () at recursive_ls.c:48
(gdb) 

现在,这个绝对路径/home/p/C_projects/recursive_ls/test似乎是合法的:

p@p-VirtualBox:~/C_projects/recursive_ls/test$pwd/home/p/C_projects/recursive_ls/test

代码如下,你能向我指出什么吗?:

#include <stdio.h>
#include <sys/stat.h>
#include <stdbool.h>
#include <sys/types.h>
#include <dirent.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
const int MAX_PATH = 255;
int isSelect(const struct dirent *file){
  if(strcmp(file->d_name,".") == 0 || strcmp(file->d_name,"..") == 0)
    return 0;
  else
    return 1;
}
void getFiles(char* path){
  printf("nDirectory is now %s", path);
  printf("n");
  struct dirent** files;
  struct stat *stat_;
  int count = scandir(path, &files, isSelect, alphasort);
  for(int i = 0; i < count; i++){
    printf("nIn %s: %s", path, files[i]->d_name);
  }
  for(int i = 0; i < count; i++){
    char* tmp = malloc(MAX_PATH*2), *tmp1 = malloc(MAX_PATH);
    strcat(strcpy(tmp1, path), "/");
    strcat(strcat(strcpy(tmp,path), "/"), files[i]->d_name);
    int statResult = stat(tmp, stat_);
    if(statResult != 0){
      printf("nOh noes! I couldn't get stats on the file %s", files[i]->d_name);
    }
    else
      printf("n%s,inode=%lu,owner=%d,size=%lu"
          ,files[i]->d_name,stat_->st_ino,stat_->st_uid,stat_->st_size);
    if(S_ISDIR(stat_->st_mode)){
      strcat(tmp1, files[i]->d_name);
      getFiles(tmp1);
    }
  }
}
int main(){
  char* path = malloc(MAX_PATH);
  getcwd(path, MAX_PATH);
  getFiles(path);
}

这里没有给出错误,但您使用stat时出现了问题。由于这会覆盖堆栈,我敢打赌这就是原因。您正在向stat()传递一个未初始化的指针变量。相反,您应该传递一个指向变量的指针,例如:

  struct stat stat_;
  ...
      int statResult = stat (tmp, &stat_);
      if (statResult != 0)
        {
          printf ("nOh noes! I couldn't get stats on the file %s",
                  files[i]->d_name);
        }
      else
        printf ("n%s,inode=%lu,owner=%d,size=%lu", files[i]->d_name,
                stat_.st_ino, stat_.st_uid, stat_.st_size);
      if (S_ISDIR (stat_.st_mode))
  ...

这样,for循环中int i的声明和main()末尾的exit(0)的声明稍微移动一点,它就用-Wall -Werror编译了,并且似乎可以工作(即下降目录)。

相关内容

最新更新