Author: tilman
Date: Fri Dec 26 12:38:51 2025
New Revision: 1930870

Log:
PDFBOX-6134: catch bad date; catch IllegalArgumentException; add test + missing 
older tests

Modified:
   pdfbox/branches/2.0/xmpbox/src/main/java/org/apache/xmpbox/DateConverter.java
   
pdfbox/branches/2.0/xmpbox/src/test/java/org/apache/xmpbox/DateConverterTest.java

Modified: 
pdfbox/branches/2.0/xmpbox/src/main/java/org/apache/xmpbox/DateConverter.java
==============================================================================
--- 
pdfbox/branches/2.0/xmpbox/src/main/java/org/apache/xmpbox/DateConverter.java   
    Fri Dec 26 11:25:15 2025        (r1930869)
+++ 
pdfbox/branches/2.0/xmpbox/src/main/java/org/apache/xmpbox/DateConverter.java   
    Fri Dec 26 12:38:51 2025        (r1930870)
@@ -101,12 +101,25 @@ public final class DateConverter
                 if (Pattern.matches("^\\d{4}-\\d{2}-\\d{2}T.*", date))
                 {
                     // Assuming ISO860 date string
-                    return fromISO8601(date);
+                    try
+                    {
+                        return fromISO8601(date);
+                    }
+                    catch (IllegalArgumentException ex)
+                    {
+                        // thrown by 
javax.xml.bind.DatatypeConverter.parseDateTime()
+                        throw new IOException(ex);
+                    }
                 }
                 else if (date.startsWith("D:"))
                 {
                     date = date.substring(2);
                 }
+                int posOfT = date.indexOf('T');
+                if (posOfT != 10 && posOfT != -1)
+                {
+                    throw new IOException("Error converting date:" + date);
+                }
 
                 date = date.replaceAll("[-:T]", "");
 

Modified: 
pdfbox/branches/2.0/xmpbox/src/test/java/org/apache/xmpbox/DateConverterTest.java
==============================================================================
--- 
pdfbox/branches/2.0/xmpbox/src/test/java/org/apache/xmpbox/DateConverterTest.java
   Fri Dec 26 11:25:15 2025        (r1930869)
+++ 
pdfbox/branches/2.0/xmpbox/src/test/java/org/apache/xmpbox/DateConverterTest.java
   Fri Dec 26 12:38:51 2025        (r1930870)
@@ -86,12 +86,61 @@ public class DateConverterTest
         convDate = 
DateConverter.toCalendar("2025-09-03T15:43:47.989082+00:00");
         assertEquals(989, convDate.get(Calendar.MILLISECOND));
 
+        // test some bad strings
         try
         {
             DateConverter.toCalendar("123");
             fail("IOException expected");
         }
         catch (IOException ex)
+        {
+        }
+        try
+        {
+            DateConverter.toCalendar("2008-12-31T19:48:30+19:00");
+            fail("IOException expected");
+        }
+        catch (IOException ex)
+        {
+        }
+        try
+        {
+            DateConverter.toCalendar("2008-12-31T19:48:30-19:00");
+            fail("IOException expected");
+        }
+        catch (IOException ex)
+        {
+        }
+        try
+        {
+            DateConverter.toCalendar("2008-12-02T21:04:0Z");
+            fail("IOException expected");
+        }
+        catch (IOException ex)
+        {
+        }
+        try
+        {
+            DateConverter.toCalendar("0-01-01T00:00:00Z");
+            fail("IOException expected");
+        }
+        catch (IOException ex)
+        {
+        }
+        try
+        {
+            DateConverter.toCalendar("2009-03-16T01:15:19-0-4:00");
+            fail("IOException expected");
+        }
+        catch (IOException ex)
+        {
+        }
+        try
+        {
+            DateConverter.toCalendar("0-00-00T00:00:00-04:00");
+            fail("IOException expected");
+        }
+        catch (IOException ex)
         {
         }
 

Reply via email to