Bugs: Browse | Submit New | Admin

[#6973] A plist with a nil Date field causes a parsing failure

Date:
2006-11-29 23:48
Priority:
3
Submitted By:
Nobody
Assigned To:
Nobody (None)
Category:
None
State:
Open
Summary:
A plist with a nil Date field causes a parsing failure

Detailed description
A plist containing a Date value represented by the empty string will cause the parser to fail. For example, the following
plist will not parse correctly under Plist 3.0.0:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN"
"http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
  <dict>
    <key>Date</key>
    <date></date>
  </dict>
  </array>
</plist>

The problem is caused in PDate#to_ruby. It makes a simple call to DateTime#parse which doesn't know what to do with
an empty string.

It should be noted that the Plist XML definition specifically disallows empty Date fields. However, we don't want our
libraries choking on every malformed piece of data that it runs across.

The patch is simple. Catch the Exception raised by DateTime#parse and return an empty string. This preserves the original
data and allows Plist#parse_xml to continue.

The patch is comprised of 3 parts. One, a patch to test_parser.rb which highlights this behavior. Two, a new "test
asset" file for test_parser.rb to use. And Three, a patch to parser.rb.

Add A Comment: Notepad

Please login


Followup

Message
Date: 2007-02-22 19:00
Sender: Ben Bleything

(copied from an email)

"It should be noted that the Plist XML definition specifically
disallows empty Date fields. However, we don't want our libraries
choking on every malformed piece of data that it runs
across."

I'm not sure I agree.  If the spec is explicit that nil date
fields are invalid, it's an exceptional case and should be dealt
with as such.  That said, we should catch the DateTime exception
and wrap it up in our own that explains why it's a problem.

This actually leads to another discussion that I think Patrick
and I had in the past, about whether we should consider adding
a strict/loose mode for parsing.  In the strict mode, an exception
would be raised there.  In the loose, you'd get an empty string
back.  I think this is ultimately the correct solution to the
problem, since if it's only a strict mode parser, you simply
can't parse invalid plists.
Date: 2006-11-29 23:57
Sender: Chuck Remes

And lastly, the patch to the parser.

--- parser.rb.org       2006-11-14 16:37:22.000000000 -0600
+++ parser.rb   2006-11-14 16:57:06.000000000 -0600
@@ -203,7 +203,12 @@
   require 'date'
   class PDate < PTag
     def to_ruby
-      DateTime.parse(text)
+      begin
+        DateTime.parse(text)
+      rescue => err
+        # on error, return an empty string
+        ""
+      end
     end
   end
 
Date: 2006-11-29 23:57
Sender: Chuck Remes

Oops, don't quite know how to upload multiple files using rubyforge.
Here is the second file pasted in.

file = test_nil_values.plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST
1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dt
d">
<plist version="1.0">
<array>
        <dict>
                <key>Real</key>
                <real></real>
                <key>String</key>
                <string></string>
                <key>Date</key>
                <date></date>
                <key>Integer</key>
                <integer></integer>
                <key>Array</key>
                <array></array>
        </dict>
        </array>
</plist>

Attached Files:

Name Description Download
test_parser.diff patch to test_parser Download

Changes:

Field Old Value Date By
File Added991: test_parser.diff2006-11-29 23:48None