【问题标题】:scandir() resulting in segfault, even with valid absolute path?scandir() 导致段错误,即使使用有效的绝对路径?
【发布时间】:2015-11-20 20:00:50
【问题描述】:

我不确定我在这里缺少什么,但我已经盯着这个看了很长一段时间了。在扫描看似有效的目录时,我似乎遇到了分段错误。我只是想打印所有目录条目和子目录条目。 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);
}

【问题讨论】:

    标签: c linux scandir


    【解决方案1】:

    这里没有报错,但是你使用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 编译并且似乎可以工作(即下降目录)。

    【讨论】:

      猜你喜欢
      • 2013-05-29
      • 2017-09-02
      • 1970-01-01
      • 2020-10-19
      • 1970-01-01
      • 2018-05-27
      • 2015-09-20
      • 2016-09-12
      • 1970-01-01
      相关资源
      最近更新 更多