I wouldn't be that happy. It's a gross oversimplification to suppose that a circular reference can only happen directly.catnikita255 wrote:Just make if(symlink.pointsTo->pointsTo!=symlink)return; and be happy.
The case for lack of recursive functions in kernel
Re: The case for lack of recursive functions in kernel
Re: The case for lack of recursive functions in kernel
What if symbolic link #1 points to symbolic link #2, which then points to symbolic link #3, which returns to the first symbolic link?Just make if(symlink.pointsTo->pointsTo!=symlink)return; and be happy.
"If you don't fail at least 90 percent of the time, you're not aiming high enough."
- Alan Kay
- Alan Kay
- Combuster
- Member
- Posts: 9301
- Joined: Wed Oct 18, 2006 3:45 am
- Libera.chat IRC: [com]buster
- Location: On the balcony, where I can actually keep 1½m distance
- Contact:
Re: The case for lack of recursive functions in kernel
To guarantee stack safety you'd want to write out the tail recursion optimisation by hand and don't rely on the compiler to fix the recursive function call for you:Note that while() loops, as opposed to your common for(i=...;i<...;i++), loops provokes pretty much the same demand for code scrutiny as recursive calls do.
The second part of the problem is less trivial. To detect circular dependencies exactly you'd need to cache all resolutions thus far:Of course caching all symlinks might be a optimisation issue for the common 1-2 symlink case and a potentional memory issue for (an attacker's) really long symlink chains, so taking a heuristic approach to the issue to prevent abuse might be an overall better idea.
$.02
Code: Select all
while (file->is_symlink()) file = resolve_symlink(file);
The second part of the problem is less trivial. To detect circular dependencies exactly you'd need to cache all resolutions thus far:
Code: Select all
hash_set->add(file);
file = resolve_symlink(file);
if (hash_set->contains(file)) generate_error();
Code: Select all
iterations++;
if (iterations > 8) generate_error();
file = resolve_symlink(file);
Re: The case for lack of recursive functions in kernel
Loop detection in a linked list a classical problem, which is often asked on interviews.Combuster wrote: The second part of the problem is less trivial. To detect circular dependencies exactly you'd need to cache all resolutions thus far:
See Floyd's "tortoise and hare" cycle detection algorithm.
- Combuster
- Member
- Posts: 9301
- Joined: Wed Oct 18, 2006 3:45 am
- Libera.chat IRC: [com]buster
- Location: On the balcony, where I can actually keep 1½m distance
- Contact:
Re: The case for lack of recursive functions in kernel
Mind that symlink resolution means (slow) disk I/O, and tortoise-hare solutions make up to thrice as many accesses to disk as strictly needed.