Skip to content

Commit d66caca

Browse files
committed
Clamp read callback to buffer size to prevent overflow.
1 parent 9dc6010 commit d66caca

7 files changed

Lines changed: 66 additions & 55 deletions

File tree

.gitignore

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
1-
pkg
2-
nbproject
3-
website/_site
4-
*.swp
5-
*.swo
6-
7-
/ext/vc/libxml_ruby.sdf
8-
/ext/vc/libxml_ruby_19/Debug
9-
/ext/vc/libxml_ruby_18/Debug
10-
/doc
11-
/tmp
12-
/.idea
13-
/ext/vc/ipch
14-
/ext/vc/libxml_ruby.opensdf
15-
.config
16-
InstalledFiles
17-
ext/libxml/libxml_ruby.bundle
18-
.DS_Store
19-
/ext/vc/*.suo
20-
Gemfile.lock
21-
lib/*/libxml_ruby.so
22-
/ext/vc/libxml_ruby/x64
23-
/ext/vc/libxml_ruby/libxml_ruby.vcxproj.user
1+
pkg
2+
nbproject
3+
website/_site
4+
*.swp
5+
*.swo
6+
7+
/ext/vc/libxml_ruby.sdf
8+
/ext/vc/libxml_ruby_19/Debug
9+
/ext/vc/libxml_ruby_18/Debug
10+
/doc
11+
/tmp
12+
/.idea
13+
/ext/vc/ipch
14+
/ext/vc/libxml_ruby.opensdf
15+
.config
16+
InstalledFiles
17+
ext/libxml/libxml_ruby.bundle
18+
.DS_Store
19+
/ext/vc/*.suo
20+
Gemfile.lock
21+
lib/*/libxml_ruby.so
22+
/ext/vc/libxml_ruby/x64
23+
/ext/vc/libxml_ruby/libxml_ruby.vcxproj.user

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
source "https://www.rubygems.org"
22

33
gemspec
4+
gem "ruby_memcheck", path: "../ruby_memcheck"

LICENSE

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
1-
Copyright (c) 2008-2013 Charlie Savage and contributors
2-
Copyright (c) 2002-2007 Sean Chittenden and contributors
3-
Copyright (c) 2001 Wai-Sun "Squidster" Chia
4-
5-
Permission is hereby granted, free of charge, to any person obtaining a copy of
6-
this software and associated documentation files (the "Software"), to deal in
7-
the Software without restriction, including without limitation the rights to
8-
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
9-
of the Software, and to permit persons to whom the Software is furnished to do
10-
so, subject to the following conditions:
11-
12-
The above copyright notice and this permission notice shall be included in all
13-
copies or substantial portions of the Software.
14-
15-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
1+
Copyright (c) 2008-2013 Charlie Savage and contributors
2+
Copyright (c) 2002-2007 Sean Chittenden and contributors
3+
Copyright (c) 2001 Wai-Sun "Squidster" Chia
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of
6+
this software and associated documentation files (the "Software"), to deal in
7+
the Software without restriction, including without limitation the rights to
8+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
9+
of the Software, and to permit persons to whom the Software is furnished to do
10+
so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2121
SOFTWARE.

Rakefile

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
require "rubygems"
44
require "rake/extensiontask"
55
require "rake/testtask"
6+
require "ruby_memcheck"
67
require "rubygems/package_task"
78
require "rdoc/task"
89
require "yaml"
@@ -15,6 +16,14 @@ spec = Gem::Specification.load("#{GEM_NAME}.gemspec")
1516

1617
task :default => [:test]
1718

19+
test_config = lambda do |t|
20+
t.libs << "test"
21+
t.test_files = FileList['test/test*.rb'] - ['test/test_suite.rb']
22+
t.verbose = true
23+
end
24+
25+
RubyMemcheck.config(binary_name: SO_NAME)
26+
1827
# Setup compile tasks
1928
Rake::ExtensionTask.new do |ext|
2029
ext.gem_spec = spec
@@ -72,8 +81,8 @@ RDoc::Task.new("rdoc") do |rdoc|
7281
end
7382

7483
# Test Task
75-
Rake::TestTask.new do |t|
76-
t.libs << "test"
77-
t.test_files = FileList['test/test*.rb'] - ['test/test_suite.rb']
78-
t.verbose = true
79-
end
84+
Rake::TestTask.new(&test_config)
85+
86+
namespace :test do
87+
RubyMemcheck::TestTask.new(valgrind: :compile, &test_config)
88+
end

ext/libxml/ruby_xml_io.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ int rxml_read_callback(void *context, char *buffer, int len)
1919
return 0;
2020

2121
size = RSTRING_LEN(string);
22+
if (size > (size_t)len)
23+
size = (size_t)len;
2224
memcpy(buffer, StringValuePtr(string), size);
2325

2426
return (int)size;

libxml-ruby.gemspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,6 @@ Gem::Specification.new do |spec|
4747
spec.add_development_dependency('rake-compiler')
4848
spec.add_development_dependency('minitest')
4949
spec.add_development_dependency('rdoc')
50+
spec.add_development_dependency('ruby_memcheck')
5051
spec.license = 'MIT'
5152
end

test/test_xpath.rb

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55

66
class TestXPath < Minitest::Test
77
def setup
8+
GC.stress = true
89
@doc = LibXML::XML::Document.file(File.join(File.dirname(__FILE__), 'model/soap.xml'))
910
end
1011

1112
def teardown
1213
@doc = nil
14+
GC.stress = false
1315
end
1416

1517
def test_doc_find
@@ -30,15 +32,11 @@ def test_ns
3032
end
3133

3234
def test_ns_gc
33-
_stress = GC.stress
34-
GC.stress = true
35-
3635
doc = LibXML::XML::Document.string('<foo xmlns="http://bar.com" />')
3736
node = doc.root
37+
doc = nil
3838
# This line segfaults on prior versions of libxml-ruby
3939
node.find("namespace::*")
40-
41-
GC.stress = _stress
4240
end
4341

4442
def test_ns_array
@@ -156,11 +154,11 @@ def test_memory
156154
# is free, it iterates over its results which are pointers
157155
# to the document's nodes. A segmentation fault then happens.
158156

159-
1000.times do
157+
10.times do
160158
doc = LibXML::XML::Document.new('1.0')
161159
doc.root = LibXML::XML::Node.new("header")
162160

163-
1000.times do
161+
10.times do
164162
doc.root << LibXML::XML::Node.new("footer")
165163
end
166164

0 commit comments

Comments
 (0)