LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 02-02-2015, 03:41 AM   #1
smiffy
LQ Newbie
 
Registered: Feb 2015
Location: Plaistow, West Sussex
Distribution: Fedora & Centos
Posts: 5

Rep: Reputation: Disabled
Where is the dirent returned by readir() located?


I recently wrote a small piece of software using opendir/readdir/closedir to access all non-directory entries in a single directory.

I needed to parse the file names to separate a variable length prefix and the main file name.

In doing so, I later realised, I had changed the contents of d_name pointed to by the dirent returned by readdir(). I was surprised that this didn't cause a segmentation fault as, presumably, the dirent is in system space ... or is it? The Linux man page for readdir() only says that the dirent structure is statically allocated.

I was initially going to run this code under Fedora 15 but I found it easier to use Xcode under OS X where I ran it initially, but it runs OK under F15 too.

Does anyone know if the dirent is in system space, and if so why did this not cause a segmentation fault?

Many thanks.

Smiffy

PS Sorry, I realise this should have been posted in Programming. Not sure how to move it.

Last edited by smiffy; 02-02-2015 at 04:12 AM.
 
Old 02-03-2015, 01:38 PM   #2
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: Fedora
Posts: 3,997

Rep: Reputation: 1218Reputation: 1218Reputation: 1218Reputation: 1218Reputation: 1218Reputation: 1218Reputation: 1218Reputation: 1218Reputation: 1218
readdir is a C library call. It allocates a static structure in the process memory. It's not defined what happens if you change the name field but I doubt that it hurts anything as long as you stay in bounds. It will just get overwritten on the next call.
 
1 members found this post helpful.
Old 02-03-2015, 04:22 PM   #3
smiffy
LQ Newbie
 
Registered: Feb 2015
Location: Plaistow, West Sussex
Distribution: Fedora & Centos
Posts: 5

Original Poster
Rep: Reputation: Disabled
smallpond,

Many thanks for that information. That the structure is allocated in process space explains why my code didn't cause a segmentation fault.

All the best

Smiffy
 
Old 02-09-2015, 07:23 AM   #4
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
If you look in /usr/include/bits/dirent.h you will see it is an array within the structure itself (256 bytes long).

You aren't supposed to change it as the results can be undefined.

But all data accessed within a process is within the process - even addresses that don't exist (and cause a segfault if used).
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] dirent.h vbx_wx Linux - Newbie 3 03-09-2010 04:45 AM
what is d_type in struct dirent ? fssengg Programming 1 03-16-2005 02:39 AM
dirent structure GregIll96 Programming 0 02-17-2005 10:25 AM
system call readir and d_name indian Programming 1 08-16-2004 01:16 AM
d_type in struct dirent brain Programming 3 05-23-2001 03:43 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 10:47 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration