如何处理错误:-Wmaybe -Unitialization

发布于 2025-02-06 04:37:54 字数 1892 浏览 2 评论 0原文

我找到了此代码,并想运行它以从中学习,但是在我的PC(Linux系统)上,我会遇到此错误:

$ c99 -Wall -Wextra -pedantic -O2 ls.c
ls.c: In Funktion »list_dir«:
ls.c:44:9: Warnung: »pwd« could be used uninitialized in this function [-Wmaybe-uninitialized]
   
44 | printf("%s \t %ld \t %s \t %s", pwd->pw_name, (long)my_stat.st_size, timebuf, cur_directory->d_name);
   |             

如何通过编辑此代码工作来摆脱它。在我的朋友PC上,它有效,但对我的电脑不起作用。是因为系统还是应该更改代码上的内容。提前致谢

#includes ...
int list_dir(const char *dir)     { 

struct dirent* cur_directory;
struct stat my_stat;
struct tm lt;  
struct passwd *pwd; // User-ID

DIR* directory = opendir(dir);

    if(directory == NULL)     { 
    printf("list_dir : %s : %s \n", dir, strerror(errno));
    return 0;
}   

    printf("Directory : %s\n", dir);
    printf("\n");
    while( (cur_directory = readdir(directory) ) )     { 
    stat(cur_directory->d_name, &my_stat);  
        if ( (stat(cur_directory->d_name, &my_stat) ) == 0 )    {
        pwd = getpwuid(my_stat.st_uid); // Get User-ID
    }
        // Last Modified 
        time_t t = my_stat.st_mtime;
        localtime_r(&t, &lt);
        char timebuf[80];
        strftime(timebuf, sizeof(timebuf), "%c", &lt);
        if (pwd != 0) {
        printf("%s \t %ld \t %s \t %s", pwd->pw_name, (long)my_stat.st_size, timebuf, cur_directory->d_name);
        printf("\n");
        } 
        else {
        printf("%d \t %ld \t %s \t %s", my_stat.st_uid, (long)my_stat.st_size, timebuf, cur_directory->d_name);
        printf("\n");
    } 
}
    closedir(directory);        
    return 0; 
    }

int main(int argc, char* argv[]){

    if ( argc == 1 ) {
    return list_dir ( "." );
    } 
    else {
    int ret = 0;
    for (int i = 1; i < argc; i += 1 ) {
    if ( list_dir ( argv[i] ) != 0 ) {
    ret = 1; 
    }
   }
   return ret;
 } 
} 

i found this code and wanted to run it to learn from it, but on my pc (linux system) i get this error:

$ c99 -Wall -Wextra -pedantic -O2 ls.c
ls.c: In Funktion »list_dir«:
ls.c:44:9: Warnung: »pwd« could be used uninitialized in this function [-Wmaybe-uninitialized]
   
44 | printf("%s \t %ld \t %s \t %s", pwd->pw_name, (long)my_stat.st_size, timebuf, cur_directory->d_name);
   |             

how can i get rid of it by editing this code to work. On my friends PC it worked but not on mine. Is it because of the System or should i change something on the code. Thanks in advance

#includes ...
int list_dir(const char *dir)     { 

struct dirent* cur_directory;
struct stat my_stat;
struct tm lt;  
struct passwd *pwd; // User-ID

DIR* directory = opendir(dir);

    if(directory == NULL)     { 
    printf("list_dir : %s : %s \n", dir, strerror(errno));
    return 0;
}   

    printf("Directory : %s\n", dir);
    printf("\n");
    while( (cur_directory = readdir(directory) ) )     { 
    stat(cur_directory->d_name, &my_stat);  
        if ( (stat(cur_directory->d_name, &my_stat) ) == 0 )    {
        pwd = getpwuid(my_stat.st_uid); // Get User-ID
    }
        // Last Modified 
        time_t t = my_stat.st_mtime;
        localtime_r(&t, <);
        char timebuf[80];
        strftime(timebuf, sizeof(timebuf), "%c", <);
        if (pwd != 0) {
        printf("%s \t %ld \t %s \t %s", pwd->pw_name, (long)my_stat.st_size, timebuf, cur_directory->d_name);
        printf("\n");
        } 
        else {
        printf("%d \t %ld \t %s \t %s", my_stat.st_uid, (long)my_stat.st_size, timebuf, cur_directory->d_name);
        printf("\n");
    } 
}
    closedir(directory);        
    return 0; 
    }

int main(int argc, char* argv[]){

    if ( argc == 1 ) {
    return list_dir ( "." );
    } 
    else {
    int ret = 0;
    for (int i = 1; i < argc; i += 1 ) {
    if ( list_dir ( argv[i] ) != 0 ) {
    ret = 1; 
    }
   }
   return ret;
 } 
} 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小苏打饼 2025-02-13 04:37:56

pwd在的情况下在内部初始化。如果条件是错误的,则永远不会初始化,因此将使用非专业化。

在定义中对其进行初始化(好主意,避免具有过时的状态),或在这种情况下添加else分支来初始化它。


要在定义上初始化:

struct passwd *pwd = NULL; // User-ID

要在else中分配:

if ( (stat(cur_directory->d_name, &my_stat) ) == 0 ) {
    pwd = getpwuid(my_stat.st_uid); // Get User-ID
} else {
    pwd = NULL;
}

而且,由于该 看起来像 您需要后一个方法。

pwd is initialized inside if. If the condition is false, it is never initialized, so will be used uninitialized.

Initialize it in definition (good idea, avoids having the uninitialized state ever), or add else branch to initialize it in that case.


To initialize at definition:

struct passwd *pwd = NULL; // User-ID

To assign in else:

if ( (stat(cur_directory->d_name, &my_stat) ) == 0 ) {
    pwd = getpwuid(my_stat.st_uid); // Get User-ID
} else {
    pwd = NULL;
}

And, since that if is inside a loop, looks like you need the latter approach.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文