Skip to content
This repository was archived by the owner on Feb 12, 2025. It is now read-only.
This repository was archived by the owner on Feb 12, 2025. It is now read-only.

FOREACH_FILE_IN_DIR potentially invalidates the name of the file on each iteration  #43

@rexim

Description

@rexim

Which might not be what the user actually want.

Consider this use case.

Cstr_Array line = cstr_array_make(cxx, CFLAGS_EXE, NULL);
FOREACH_FILE_IN_DIR(file, ".",
{
    if (ENDS_WITH(file, ".o"))
    {
        line = cstr_array_append(line, file);
    }
});
Cmd cmd = {
    .line = line
};
cmd_run_sync(cmd);

Here we actually need to strdup the string before pushing it to the list:

--- test.c    2021-07-30 03:22:06.603302201 +0700
+++ test2.c    2021-07-30 03:22:22.127512662 +0700
@@ -2,7 +2,7 @@
 FOREACH_FILE_IN_DIR(file, ".", {
     if (ENDS_WITH(file, ".o"))
     {
-        line = cstr_array_append(line, file);
+        line = cstr_array_append(line, strdup(file));
     }
 });
 Cmd cmd = {

We need to think how we wanna approach this usability issue. Maybe do that strdup for the user?

(Found by @Ciremun)

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions