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

Use pathconf(), also check NAME_MAX #494

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
177 changes: 177 additions & 0 deletions src/config_rmw.c
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,58 @@ realize_str(char *str, const char *homedir, const char *uid)
return 0;
}

static long
get_single_pathconf_limit(const char *path, int name, long fallback,
const char *limit_name)
{
if (!path)
{
fprintf(stderr, "Error: Path is NULL.\n");
return -1;
}

long limit = pathconf(path, name);
if (limit == -1)
{
if (errno == 0)
limit = fallback; // Use fallback if no limit is defined
else
{
fprintf(stderr, "Error querying %s: ", limit_name);
perror("pathconf");
return -1;
}
}

return limit;
}


static long
get_name_max(const char *path)
{
return get_single_pathconf_limit(path, _PC_NAME_MAX, NAME_MAX, "NAME_MAX");
}


static long
get_path_max(const char *path)
{
return get_single_pathconf_limit(path, _PC_PATH_MAX, PATH_MAX, "PATH_MAX");
}


static int
get_pathconf_limits(const char *path, struct pathconf_limits *pathconf_limits)
{
if ((pathconf_limits->path_max = get_path_max(path)) == -1)
return -1;
if ((pathconf_limits->name_max = get_name_max(path)) == -1)
return -1;

return 0;
}


/*!
* This function is called when the "WASTE" option is encountered in the
Expand Down Expand Up @@ -275,6 +327,16 @@ parse_line_waste(st_waste *waste_curr, struct Canfigger *node,
else if (p_state == -1)
exit(p_state);

struct pathconf_limits temp = { 0, 0 };
waste_curr->pathconf_limits = temp;
if (get_pathconf_limits(waste_curr->parent, &waste_curr->pathconf_limits)
== -1)
exit(EXIT_FAILURE);

//if (verbose)
//printf("path_max: %ld; name_max %ld", waste_curr->pathconf_limits.path_max,
//waste_curr->pathconf_limits.name_max);

waste_curr->is_btrfs = is_btrfs(waste_curr->parent);

// get device number to use later for rename
Expand Down Expand Up @@ -459,3 +521,118 @@ show_folder_line(const char *folder, const bool is_r, const bool is_attached)

putchar('\n');
}


int
validate_path(const char *path, struct pathconf_limits *pathconf_limits)
{
if (!path)
{
fprintf(stderr, "Error: Path is NULL.\n");
return -1; // Invalid input
}

size_t path_len = strlen(path);
if (path_len > (size_t) pathconf_limits->path_max - 1)
{
fprintf(stderr, "Error: Path length (%zu) exceeds PATH_MAX-1 (%ld).\n",
path_len, pathconf_limits->path_max - 1);
return -1;
}

// Check individual component lengths
const char *start = path;
while (*start)
{
const char *end = strchr(start, '/');
size_t component_len = end ? (size_t) (end - start) : strlen(start);

if (component_len > (size_t) pathconf_limits->name_max)
{
fprintf(stderr,
"Error: Path component '%.*s' exceeds NAME_MAX-1 (%ld).\n",
(int) component_len, start, pathconf_limits->name_max - 1);
return -1;
}

if (!end)
break;

start = end + 1;
}

return 0;
}

///////////////////////////////////////////////////////////////////////
#ifdef TEST_LIB

#include "test.h"

void
test_validate_path(void)
{
struct pathconf_limits pathconf_limits;
long ret = get_pathconf_limits("/", &pathconf_limits);
assert(ret != -1);

// This should be true on most systems, and if it's ever false, the test
// needs to be changed.
assert(pathconf_limits.path_max >= 128 && pathconf_limits.name_max >= 128);
assert(pathconf_limits.path_max <= 4096
&& pathconf_limits.name_max <= 4096);
assert(validate_path
("/dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd/foo",
&pathconf_limits) != 0);

long i, k;
long separators = pathconf_limits.path_max / pathconf_limits.name_max;
char tmp[pathconf_limits.path_max + separators + 1];
fprintf(stderr, "sizeof tmp: %ld\n", sizeof(tmp));

memset(tmp, 0, sizeof(tmp));
char *ptr = tmp;
for (k = 0; k < separators; k++)
{
*ptr++ = '/';
for (i = 0; i < pathconf_limits.name_max; i++)
*ptr++ = 'a';
}
ptr--;
*ptr = '\0';
fprintf(stderr, "tmp: %ld\n", strlen(tmp));
assert(validate_path(tmp, &pathconf_limits) == 0);

// Exceed path_max
*ptr++ = 'a';
*ptr = '\0';
fprintf(stderr, "tmp: %ld\n", strlen(tmp));
assert(validate_path(tmp, &pathconf_limits) == -1);

// Exceed name_max
memset(tmp, 0, sizeof(tmp));
ptr = tmp;
for (k = 0; k < separators; k++)
{
*ptr++ = '/';
for (i = 0; i < pathconf_limits.name_max - 2; i++)
*ptr++ = 'a';
}

for (k = 0; k < 5; k++)
*ptr++ = 'a';
*ptr = '\0';

fprintf(stderr, "tmp: %ld\n", strlen(tmp));
assert(validate_path(tmp, &pathconf_limits) != 0);

return;
}

int
main(void)
{
test_validate_path();
}

#endif
2 changes: 2 additions & 0 deletions src/config_rmw.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,5 @@ void init_config_data(st_config * x);

void
show_folder_line(const char *folder, const bool is_r, const bool is_attached);

int validate_path(const char *path, struct pathconf_limits *pathconf_limits);
8 changes: 8 additions & 0 deletions src/trashinfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,20 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.

#include "time_rmw.h"

struct pathconf_limits
{
long path_max;
long name_max;
};

/** Each waste directory is added to a linked list and has the data
* from this structure associated with it.
*/
typedef struct st_waste st_waste;
struct st_waste
{
struct pathconf_limits pathconf_limits;

/** The parent directory, e.g. $HOME/.local/share/Trash */
char *parent;

Expand Down
1 change: 1 addition & 0 deletions src/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -756,6 +756,7 @@ main()
free(escaped_path);

test_count_chars();

return 0;
}
#endif
2 changes: 1 addition & 1 deletion test/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ if faketime.found()
)
endif

test_cases = ['strings_rmw', 'utils', 'restore']
test_cases = ['config_rmw', 'strings_rmw', 'utils', 'restore']

scripts = [
'test_basic.sh',
Expand Down
Loading