View Issue Details
| ID | Project | Category | View Status | Date Submitted | Last Update |
|---|---|---|---|---|---|
| 0002005 | 1003.1(2024)/Issue8 | System Interfaces | public | 2026-11-08 12:49 | 2026-11-08 12:51 |
| Reporter | lukellmann | Assigned To | |||
| Priority | normal | Severity | Editorial | Type | Error |
| Status | New | Resolution | Open | ||
| Name | Luca Kellermann | ||||
| Organization | |||||
| User Reference | |||||
| Section | alphasort | ||||
| Page Number | I don't know | ||||
| Line Number | I don't know | ||||
| Interp Status | |||||
| Final Accepted Text | |||||
| Summary | 0002005: scandir() example frees namelist on failure | ||||
| Description | The 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 Action | Change 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);
}
... | ||||
| Tags | No tags attached. | ||||
| Date Modified | Username | Field | Change |
|---|---|---|---|
| 2026-11-08 12:49 | lukellmann | New Issue |