【问题标题】:Linux getgrent() results in "Segmentation fault"Linux getgrent() 导致“分段错误”
【发布时间】:2021-06-17 14:58:11
【问题描述】:

我正在尝试打印所有系统组(Ubuntu 20.04):

#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <grp.h>

int main(int argc, char *argv[])
{
   printf("Here are all of this system's groups:\n\n");

   struct group* grp;
   while ((grp = getgrent()) != NULL)
      puts(grp->gr_name);

   endgrent();
   
   exit(EXIT_SUCCESS);
}

我用sudo 运行程序,我得到:

$ sudo ./program
Here are all of this system's groups:

Segmentation fault

使用struct spwd 时会发生同样的错误。

更新

我用include 行更新了源代码,并省略了lib/*.c 部分。当我编译这段代码时:

$ gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/9/lto-wrapper
OFFLOAD_TARGET_NAMES=nvptx-none:hsa
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-linux-gnu
Configured with: [...]
Thread model: posix
gcc version 9.3.0 (Ubuntu 9.3.0-17ubuntu1~20.04)

$ gcc -std=c17 main.c -o program
main.c: In function ‘main’:
main.c:11:18: warning: implicit declaration of function ‘getgrent’ [-Wimplicit-function-declaration]
   11 |    while ((grp = getgrent()) != NULL)
      |                  ^~~~~~~~
main.c:11:16: warning: assignment to ‘struct group *’ from ‘int’ makes pointer from integer without a cast [-Wint-conversion]
   11 |    while ((grp = getgrent()) != NULL)
      |                ^
main.c:14:4: warning: implicit declaration of function ‘endgrent’ [-Wimplicit-function-declaration]
   14 |    endgrent();
      |    ^~~~~~~~

当我运行它时:

$ ./program 
Here are all of this system's groups:

Segmentation fault (core dumped)

$ sudo ./program 
Here are all of this system's groups:

Segmentation fault

现在,当我运行 VS Code 调试器时,它可以正常工作。 VS Code 根据this article配置。

这是我的launch.json

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "gcc-9 - Build and debug active file",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}/${fileBasenameNoExtension}",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${fileDirname}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "C/C++: gcc-9 build active file",
            "miDebuggerPath": "/usr/bin/gdb"
        }
    ]
}

【问题讨论】:

  • 您应该在启用调试 (-g) 的情况下对其进行编译,然后在调试器 (gdb) 中运行它以找出崩溃的确切位置(哪一行)。
  • @Wes Hardaker - 好的,我刚刚尝试了 VS Code 调试,它成功了!我不知道我做错了什么:gcc -std=c17 main.c lib/*.c -o program
  • 奇数。这使得很难找出问题所在:-/我怀疑这是一个错误,但我认为它不在你的代码中。
  • 我在 WSL 上尝试过,它的输出很好。我只是像gcc main.c 一样编译它,然后在没有 sudo 的情况下运行 a.out。我想知道您的lib/*.c 是否引起了任何问题。即使只是为了故障排除,也建议使用默认值进行构建。如果仍然崩溃手动运行gdb program
  • while ((grp = getgrent()) != NULL) -std=c17 很奇怪:它禁止包含 &lt;grp.h&gt;(和c17非常奇怪)

标签: c linux ubuntu


【解决方案1】:

添加正确的标题:


#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <grp.h>

int main(int argc, char *argv[])
{
   printf("Here are all of this system's groups:\n\n");

   struct group* grp;
   while ((grp = getgrent()) != NULL)
      puts(grp->gr_name);

   endgrent();

   exit(EXIT_SUCCESS);
}

更新:-std=c17 很奇怪,它没有包含 &lt;grp.h&gt; 。你可能需要std=gnu17,其中包括一些 gnu+posix 的东西。

【讨论】:

  • 正在使用正确的标题。
  • 嗯,它在这里工作。也许问题出在您没有向我们展示的代码的另一部分?
  • 感谢您的宝贵时间。它适用于 VS Code 调试。但是编译后的程序不起作用。你也在使用 Ubuntu 20.04 吗?
  • 是的,但是使用理智的本机编译器而不是微软的软件产品
  • VS Code 也配置为use GCC
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-11-06
  • 2020-12-31
  • 2019-07-21
  • 2018-07-28
  • 2014-04-25
  • 2011-07-18
  • 2013-02-26
相关资源
最近更新 更多