Skip to content
This repository has been archived by the owner on Jan 8, 2024. It is now read-only.

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

Open
rexim opened this issue Jul 29, 2021 · 0 comments
Assignees

Comments

@rexim
Copy link
Member

rexim commented Jul 29, 2021

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)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant