OSDev.org

The Place to Start for Operating System Developers
It is currently Tue Apr 23, 2024 6:00 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 20 posts ]  Go to page Previous  1, 2
Author Message
 Post subject: Re: The case for lack of recursive functions in kernel
PostPosted: Fri Oct 09, 2015 9:10 am 
Offline
Member
Member
User avatar

Joined: Sat Mar 31, 2012 3:07 am
Posts: 4594
Location: Chichester, UK
catnikita255 wrote:
Just make if(symlink.pointsTo->pointsTo!=symlink)return; and be happy.

I wouldn't be that happy. It's a gross oversimplification to suppose that a circular reference can only happen directly.


Top
 Profile  
 
 Post subject: Re: The case for lack of recursive functions in kernel
PostPosted: Fri Oct 09, 2015 2:03 pm 
Offline
Member
Member
User avatar

Joined: Thu Mar 27, 2014 3:57 am
Posts: 568
Location: Moscow, Russia
Quote:
Just make if(symlink.pointsTo->pointsTo!=symlink)return; and be happy.
What if symbolic link #1 points to symbolic link #2, which then points to symbolic link #3, which returns to the first symbolic link?

_________________
"If you don't fail at least 90 percent of the time, you're not aiming high enough."
- Alan Kay


Top
 Profile  
 
 Post subject: Re: The case for lack of recursive functions in kernel
PostPosted: Mon Oct 12, 2015 3:59 am 
Offline
Member
Member
User avatar

Joined: Wed Oct 18, 2006 3:45 am
Posts: 9301
Location: On the balcony, where I can actually keep 1½m distance
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:
Code:
while (file->is_symlink()) file = resolve_symlink(file);
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:
Code:
hash_set->add(file);
file = resolve_symlink(file);
if (hash_set->contains(file)) generate_error();
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.
Code:
iterations++;
if (iterations > 8) generate_error();
file = resolve_symlink(file);


$.02

_________________
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]


Top
 Profile  
 
 Post subject: Re: The case for lack of recursive functions in kernel
PostPosted: Mon Oct 12, 2015 4:11 am 
Offline
Member
Member

Joined: Tue Mar 04, 2014 5:27 am
Posts: 1108
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:

Loop detection in a linked list a classical problem, which is often asked on interviews.
See Floyd's "tortoise and hare" cycle detection algorithm.


Top
 Profile  
 
 Post subject: Re: The case for lack of recursive functions in kernel
PostPosted: Mon Oct 12, 2015 5:56 am 
Offline
Member
Member
User avatar

Joined: Wed Oct 18, 2006 3:45 am
Posts: 9301
Location: On the balcony, where I can actually keep 1½m distance
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.

_________________
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 20 posts ]  Go to page Previous  1, 2

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot] and 39 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group