* win32/win32.c (rb_w32_seekdir): should not segfault even if passed

the location which rb_w32_telldir didn't return. (and should change
  `bits' position) [ruby-core:7035]

* win32/dir.h: ditto. (stores `loc' instead of `bitpos')

* test/ruby/test_dir.rb: added.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9782 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
ocean 2006-01-01 05:49:01 +00:00
parent a5e6541ad6
commit 88a3caaafe
4 changed files with 83 additions and 19 deletions

View File

@ -1,3 +1,13 @@
Sun Jan 1 14:42:54 2006 Hirokazu Yamamoto <ocean@m2.ccsnet.ne.jp>
* win32/win32.c (rb_w32_seekdir): should not segfault even if passed
the location which rb_w32_telldir didn't return. (and should change
`bits' position) [ruby-core:7035]
* win32/dir.h: ditto. (stores `loc' instead of `bitpos')
* test/ruby/test_dir.rb: added.
Sat Dec 31 22:57:00 2005 Nobuyoshi Nakada <nobu@ruby-lang.org> Sat Dec 31 22:57:00 2005 Nobuyoshi Nakada <nobu@ruby-lang.org>
* eval.c (rb_thread_save_context): should not recycle scope object used * eval.c (rb_thread_save_context): should not recycle scope object used

42
test/ruby/test_dir.rb Normal file
View File

@ -0,0 +1,42 @@
require 'test/unit'
require 'tmpdir'
require 'fileutils'
class TestDir < Test::Unit::TestCase
ROOT = File.join(Dir.tmpdir, "__test_dir__#{$$}")
def setup
Dir.mkdir(ROOT)
for i in ?a..?z
if i % 2 == 0
FileUtils.touch(File.join(ROOT, i.chr))
else
FileUtils.mkdir(File.join(ROOT, i.chr))
end
end
end
def teardown
FileUtils.rm_rf ROOT if File.directory?(ROOT)
end
def test_seek
dir = Dir.open(ROOT)
begin
cache = []
loop do
pos = dir.tell
break unless name = dir.read
cache << [pos, name]
end
for x in cache.sort_by {|x| x[0] % 3 } # shuffle
dir.seek(x[0])
assert_equal(x[1], dir.read)
end
ensure
dir.close
end
end
end

View File

@ -21,9 +21,9 @@ typedef struct {
char *curr; char *curr;
long size; long size;
long nfiles; long nfiles;
long loc; /* [0, nfiles) */
struct direct dirstr; struct direct dirstr;
char *bits; /* used for d_isdir and d_isrep */ char *bits; /* used for d_isdir and d_isrep */
long bitpos; /* used for d_isdir and d_isrep */
} DIR; } DIR;

View File

@ -1461,6 +1461,21 @@ rb_w32_opendir(const char *filename)
return p; return p;
} }
//
// Move to next entry
//
static void
move_to_next_entry(DIR *dirp)
{
if (dirp->curr) {
dirp->loc++;
dirp->curr += strlen(dirp->curr) + 1;
if (dirp->curr >= (dirp->start + dirp->size)) {
dirp->curr = NULL;
}
}
}
// //
// Readdir just returns the current string pointer and bumps the // Readdir just returns the current string pointer and bumps the
@ -1470,7 +1485,6 @@ rb_w32_opendir(const char *filename)
struct direct * struct direct *
rb_w32_readdir(DIR *dirp) rb_w32_readdir(DIR *dirp)
{ {
int len;
static int dummy = 0; static int dummy = 0;
if (dirp->curr) { if (dirp->curr) {
@ -1479,9 +1493,8 @@ rb_w32_readdir(DIR *dirp)
// first set up the structure to return // first set up the structure to return
// //
len = strlen(dirp->curr);
strcpy(dirp->dirstr.d_name, dirp->curr); strcpy(dirp->dirstr.d_name, dirp->curr);
dirp->dirstr.d_namlen = len; dirp->dirstr.d_namlen = strlen(dirp->curr);
// //
// Fake inode // Fake inode
@ -1491,19 +1504,14 @@ rb_w32_readdir(DIR *dirp)
// //
// Attributes // Attributes
// //
dirp->dirstr.d_isdir = GetBit(dirp->bits, dirp->bitpos); dirp->dirstr.d_isdir = GetBit(dirp->bits, dirp->loc * 2);
dirp->bitpos++; dirp->dirstr.d_isrep = GetBit(dirp->bits, dirp->loc * 2 + 1);
dirp->dirstr.d_isrep = GetBit(dirp->bits, dirp->bitpos);
dirp->bitpos++;
// //
// Now set up for the next call to readdir // Now set up for the next call to readdir
// //
dirp->curr += len + 1; move_to_next_entry(dirp);
if (dirp->curr >= (dirp->start + dirp->size)) {
dirp->curr = NULL;
}
return &(dirp->dirstr); return &(dirp->dirstr);
@ -1518,7 +1526,7 @@ rb_w32_readdir(DIR *dirp)
long long
rb_w32_telldir(DIR *dirp) rb_w32_telldir(DIR *dirp)
{ {
return (long) dirp->curr; /* ouch! pointer to long cast */ return dirp->loc;
} }
// //
@ -1528,7 +1536,11 @@ rb_w32_telldir(DIR *dirp)
void void
rb_w32_seekdir(DIR *dirp, long loc) rb_w32_seekdir(DIR *dirp, long loc)
{ {
dirp->curr = (char *) loc; /* ouch! long to pointer cast */ rb_w32_rewinddir(dirp);
while (dirp->curr && dirp->loc < loc) {
move_to_next_entry(dirp);
}
} }
// //
@ -1538,8 +1550,8 @@ rb_w32_seekdir(DIR *dirp, long loc)
void void
rb_w32_rewinddir(DIR *dirp) rb_w32_rewinddir(DIR *dirp)
{ {
dirp->curr = dirp->start; dirp->curr = dirp->start;
dirp->bitpos = 0; dirp->loc = 0;
} }
// //
@ -1549,9 +1561,9 @@ rb_w32_rewinddir(DIR *dirp)
void void
rb_w32_closedir(DIR *dirp) rb_w32_closedir(DIR *dirp)
{ {
free(dirp->start); free(dirp->start);
free(dirp->bits); free(dirp->bits);
free(dirp); free(dirp);
} }
#if (defined _MT || defined __MSVCRT__) && !defined __BORLANDC__ #if (defined _MT || defined __MSVCRT__) && !defined __BORLANDC__