View Issue Details

IDProjectCategoryView StatusLast Update
00020051003.1(2024)/Issue8System Interfacespublic2026-11-08 12:51
Reporterlukellmann Assigned To 
PrioritynormalSeverityEditorialTypeError
Status NewResolutionOpen 
NameLuca Kellermann
Organization
User Reference
Sectionalphasort
Page NumberI don't know
Line NumberI don't know
Interp Status
Final Accepted Text
Summary0002005: scandir() example frees namelist on failure
DescriptionThe description of scandir() does not place any requirements on the value of *namelist on error. It could for example be unchanged, set to a null pointer or even point to memory that has already been freed.

The example calls free(namelist) unconditionally, potentially passing an uninitialized or already freed pointer.
Desired ActionChange the example (accepted text from 0001931):

#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>
...
struct dirent **files;
int i,n;

n = scandir(".", &files, NULL, alphasort);
if (n < 0)
    perror("scandir");
else {
    for (i = 0; i < n; i++) {
        printf("%s\n", files[i]->d_name);
        free(files[i]);
    }
}
if (files)
    free(files);
...


to (don't free on error):

#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>
...
struct dirent **files;
int i,n;

n = scandir(".", &files, NULL, alphasort);
if (n < 0)
    perror("scandir");
else {
    for (i = 0; i < n; i++) {
        printf("%s\n", files[i]->d_name);
        free(files[i]);
    }
    if (files)
        free(files);
}
...
TagsNo tags attached.

Activities

There are no notes attached to this issue.

Issue History

Date Modified Username Field Change
2026-11-08 12:49 lukellmann New Issue