Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dir.c---dir_list_file running error #144

Open
mocuishleshadow opened this issue Sep 8, 2020 · 1 comment
Open

dir.c---dir_list_file running error #144

mocuishleshadow opened this issue Sep 8, 2020 · 1 comment
Assignees

Comments

@mocuishleshadow
Copy link

Hi, man
In dir_list_file function. when black list file exist and not empty, something wrong happend.
black_item var is local and memory on stack.

  under code:
 
  while (fgets(buf, lengthof(buf), black_list_file) != NULL)
	{
		join_path_components(black_item, pgdata, buf);
		if (black_item[strlen(black_item) - 1] == '\n')
			black_item[strlen(black_item) - 1] = '\0';
		if (black_item[0] == '#' || black_item[0] == '\0')
			continue;
		parray_append(black_list, black_item);
	}

           when cycle > 2 then   black_list will be the same thing。
@ljluestc
Copy link

void dir_list_file(const char *pgdata, const char *blacklist, parray *black_list)
{
    FILE *black_list_file;
    char buf[MAXPGPATH];

    black_list_file = fopen(blacklist, "r");
    if (black_list_file == NULL)
        return;

    while (fgets(buf, sizeof(buf), black_list_file) != NULL)
    {
        char *black_item = (char *)malloc(strlen(buf) + 1);
        if (black_item == NULL)
        {
            fclose(black_list_file);
            // Handle memory allocation error if needed
            return;
        }

        join_path_components(black_item, pgdata, buf);
        if (black_item[strlen(black_item) - 1] == '\n')
            black_item[strlen(black_item) - 1] = '\0';
        if (black_item[0] == '#' || black_item[0] == '\0')
        {
            free(black_item); // Free the memory if not added to black_list
            continue;
        }
        
        parray_append(black_list, black_item);
    }

    fclose(black_list_file);
}

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

No branches or pull requests

5 participants