[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[vidalia-svn] r1357: Make ourselves less likely to crash when reading in a bogus (trunk/src/util/geoip)



Author: edmanm
Date: 2006-10-19 02:39:56 -0400 (Thu, 19 Oct 2006)
New Revision: 1357

Modified:
   trunk/src/util/geoip/geoipcache.cpp
   trunk/src/util/geoip/geoipcacheitem.cpp
   trunk/src/util/geoip/geoipcacheitem.h
Log:
Make ourselves less likely to crash when reading in a bogus geoip-cache file.


Modified: trunk/src/util/geoip/geoipcache.cpp
===================================================================
--- trunk/src/util/geoip/geoipcache.cpp	2006-10-19 05:22:34 UTC (rev 1356)
+++ trunk/src/util/geoip/geoipcache.cpp	2006-10-19 06:39:56 UTC (rev 1357)
@@ -110,7 +110,7 @@
     while (!line.isNull()) {
       /* Create a GeoIpCacheItem from the line and save it */
       GeoIpCacheItem item = GeoIpCacheItem::fromString(line);
-      if (!item.isExpired()) {
+      if (!item.isEmpty() && !item.isExpired()) {
         /* Only load non-stale cache items. */
         _cache.insert(item.ip().toIPv4Address(), item);
       }

Modified: trunk/src/util/geoip/geoipcacheitem.cpp
===================================================================
--- trunk/src/util/geoip/geoipcacheitem.cpp	2006-10-19 05:22:34 UTC (rev 1356)
+++ trunk/src/util/geoip/geoipcacheitem.cpp	2006-10-19 06:39:56 UTC (rev 1357)
@@ -37,6 +37,14 @@
   _timestamp = timestamp;
 }
 
+/** Returns true if this cache item is empty and invalid. A valid cache item
+ * must have a valid GeoIp object and timestamp. */
+bool
+GeoIpCacheItem::isEmpty() const
+{
+  return (_geoip.isEmpty() || _timestamp.isNull());
+}
+
 /** Returns a string representing the contents of this cache item, suitable
  * for writing to disk. The format is as in the following example:
  *                     <Geo IP Data>:<Timestamp>
@@ -50,16 +58,23 @@
 /** Returns a GeoIpCacheItem from a string as read from the cache that was
  * written to disk. The format is:
  *                     <Geo IP Data>:<Timestamp>
+ *
+ * If the string cannot be parsed for valid cached GeoIP data, then an empty
+ * GeoIpCacheItem object is returned. The calling method should call isEmpty()
+ * on the returned GeoIpCacheItem object to ensure it got a valid object.
  */
 GeoIpCacheItem
 GeoIpCacheItem::fromString(QString cacheString)
 {
   QDateTime timestamp;
   QStringList cacheData = cacheString.split(":");
-  
-  GeoIp geoip = GeoIp::fromString(cacheData.at(0));
-  timestamp.setTime_t(cacheData.at(1).toUInt());
-  return GeoIpCacheItem(geoip, timestamp);
+ 
+  if (cacheData.size() >= 2) {
+    GeoIp geoip = GeoIp::fromString(cacheData.at(0));
+    timestamp.setTime_t(cacheData.at(1).toUInt());
+    return GeoIpCacheItem(geoip, timestamp);
+  }
+  return GeoIpCacheItem();
 }
 
 /** Returns true if the cache item is too old to be considered valid. */

Modified: trunk/src/util/geoip/geoipcacheitem.h
===================================================================
--- trunk/src/util/geoip/geoipcacheitem.h	2006-10-19 05:22:34 UTC (rev 1356)
+++ trunk/src/util/geoip/geoipcacheitem.h	2006-10-19 06:39:56 UTC (rev 1357)
@@ -47,7 +47,9 @@
   GeoIp geoip() const { return _geoip; }
   /** Returns true if this cache item is expired. */
   bool isExpired() const;
- 
+  /** Returns true if this cache item is empty and invalid. */
+  bool isEmpty() const;
+
   /** Returns a string representing the contents of this cache item, suitable
    * for writing to disk. */
   QString toString() const;