Permalink
Browse files

Fix a ruby 1.9 encoding problem,

"LooseObjectError: size mismatch" by committing non-ASCII content.

String#length returns number of characters in the encoding of the string. Instead, we should use String#bytesize.
  • Loading branch information...
1 parent a9c027c commit 696761d8047ffd038dc2828e6a1998e3f7c3b419 @hiroshi hiroshi committed Jan 2, 2011
Showing with 5 additions and 5 deletions.
  1. +5 −5 lib/grit/git-ruby/internal/loose.rb
@@ -36,7 +36,7 @@ def [](sha1)
end
def get_raw_object(buf)
- if buf.length < 2
+ if buf.bytesize < 2
raise LooseObjectError, "object file too small"
end
@@ -56,14 +56,14 @@ def get_raw_object(buf)
type, size, used = unpack_object_header_gently(buf)
content = Zlib::Inflate.inflate(buf[used..-1])
end
- raise LooseObjectError, "size mismatch" if content.length != size
+ raise LooseObjectError, "size mismatch" if content.bytesize != size
return RawObject.new(type, content)
end
# currently, I'm using the legacy format because it's easier to do
# this function takes content and a type and writes out the loose object and returns a sha
def put_raw_object(content, type)
- size = content.length.to_s
+ size = content.bytesize.to_s
LooseStorage.verify_header(type, size)
header = "#{type} #{size}\0"
@@ -85,7 +85,7 @@ def put_raw_object(content, type)
# simply figure out the sha
def self.calculate_sha(content, type)
- size = content.length.to_s
+ size = content.bytesize.to_s
verify_header(type, size)
header = "#{type} #{size}\0"
store = header + content
@@ -109,7 +109,7 @@ def unpack_object_header_gently(buf)
size = c & 15;
shift = 4;
while c & 0x80 != 0
- if buf.length <= used
+ if buf.bytesize <= used
raise LooseObjectError, "object file too short"
end
c = buf.getord(used)

2 comments on commit 696761d

@hiroshi Thank you! I just spent a day tracking down a bug that turned out to be this. :-P

@technoweenie Any idea when there will be a new release of Grit with this commit in it? Looks like it just missed 2.4.1.

pal replied Oct 3, 2011

I'm also hoping for a new official release to fix this error, any updates?

Please sign in to comment.