Barby::Code128: private method split called for nil

38 views
Skip to first unread message

Gunnar Wolf

unread,
Nov 24, 2009, 10:13:38 PM11/24/09
to Barby
Hi,

We received the following bug report in the Debian BTS for Barby:

http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=557023

> Creating a new instance of Barby::Code128 with a legitimate string can
> cause a stack trace. One such string is the empty string:
>
> Barby::Code128.new("", 'B')
>
> The stack trace is:
>
> /usr/lib/ruby/1.8/barby/barcode/code_128.rb:239:in `characters': private method `split' called for ni
l:NilClass (NoMethodError)
> from /usr/lib/ruby/1.8/barby/barcode/code_128.rb:364
:in `valid?'
> from /usr/lib/ruby/1.8/barby/barcode/code_128.rb:162
:in `initialize'
> from ./foo.rb:15:in `new'
> from ./foo.rb:15
>
> foo.rb (attached) reproduces the problem two different ways.

The submitter supplied two patches fixing this problem (the second one
fixing a case he didn't originally consider) - I'm inlining them both
here:



diff -ur libbarby-ruby-0.3.orig/lib/barby/barcode/code_128.rb libbarby-
ruby-0.3/lib/barby/barcode/code_128.rb
--- libbarby-ruby-0.3.orig/lib/barby/barcode/code_128.rb
2009-11-18 17:51:28.000000000 -0700
+++ libbarby-ruby-0.3/lib/barby/barcode/code_128.rb
2009-11-18 17:51:37.000000000 -0700
@@ -227,8 +227,8 @@
#sets, in which case the extra will itself have an extra.
def extra=(extra)
raise ArgumentError, "Extra must begin with \\301, \\302 or \
\303" unless extra =~ /^[#{CODEA+CODEB+CODEC}]/n
- type = extra[/([#{CODEA+CODEB+CODEC}])/n, 1]
- data = extra[/[#{CODEA+CODEB+CODEC}](.*)/n, 1]
+ type = extra[0..0]
+ data = extra[1..-1]
@extra = class_for(type).new(data)
end



diff -ur libbarby-ruby-0.3.orig/lib/barby/barcode/code_128.rb libbarby-
ruby-0.3/lib/barby/barcode/code_128.rb
--- libbarby-ruby-0.3.orig/lib/barby/barcode/code_128.rb
2009-11-18 17:51:28.000000000 -0700
+++ libbarby-ruby-0.3/lib/barby/barcode/code_128.rb
2009-11-18 22:35:54.000000000 -0700
@@ -208,7 +208,7 @@
#character set, an extra will be created.
def data=(data)
data, *extra = data.split(/([#{CODEA+CODEB+CODEC}])/n)
- @data = data
+ @data = data || ''
self.extra = extra.join unless extra.empty?
end


Thank you,

--
Gunnar Wolf • gw...@gwolf.org(+52-55)5623-0154 / 1451-2244

Tore Darell

unread,
Nov 25, 2009, 11:56:57 AM11/25/09
to Barby
Hi Gunnar,

Sorry about the delay; I've started moderating the group as currently
the number of spam messages outnumber real messages by a much larger
factor than I have time to deal with every day.

Thanks for the patches; I'll look into them as soon as I have some
time. From just glancing over them they look ok, I'll post a reply
soon.

Tore

Tore Darell

unread,
Nov 26, 2009, 4:39:17 PM11/26/09
to Barby
Hi Again,

I've made some changes to Code128 that should fix the reported bugs:

The first example was due to . in the regex not including newlines. I
think the code used regexes originally to avoid assuming that change
codes were 1 byte string. Of course, the likelyhood of the 128
standard being altered to introduce mylti-byte change codes is next to
zero, so I think it's safe to let go of this bit of flexibility :)
Thus, I've applied the suggested patch.

The second is not really a bug, as you've pointed out in the Debian
bug report, but rather a question of how to handle something that may
or may not be an error. I'm not entirely sure if a 128 code without
any data is invalid, but that's what I've decided to go with. With
this decision made, I've added to the validity checking that empty
strings are not valid. The example code will now explicitly raise an
ArgumentError like other invalid data instead of the rather arbitrary
error which was raised before.

I've pushed all these changes to GitHub and released a new version of
the gem, 0.3.2, containing these and some other small fixes. Note that
this version contains the Pdf417 class which relies on JRuby to be
used, which could complicate distribution a bit. Everything else will
work just fine without it, it's just when you actually require and try
to use the Pdf417 functionality that it will fail if you don't have
JRuby. I also read from the bug report that the Debian package relies
on rails; Barby by itself doesn't actually require rails, in case that
was why it was added as a dependency.

Changelog: http://github.com/toretore/barby/blob/master/CHANGELOG

The gem has been pushed to Gemcutter, http://gemcutter.org

Tore
Reply all
Reply to author
Forward
0 new messages