Bug 1574863 - Handle 'foreign' files in logs directory gracefully. r=florian a=jorgk
authorJorg K <jorgk@jorgk.com>
Mon, 19 Aug 2019 19:29:16 +0200
changeset 36169 f513420103a459fa43ee6d3739b4434c52e959b0
parent 36168 8a41d86c072a4fc05df6f96caa4e967d9d90934f
child 36170 0cdb8a1fb8e8ed67be1fd89708e7d001542c562f
push id392
push userclokep@gmail.com
push dateMon, 02 Sep 2019 20:17:19 +0000
reviewersflorian, jorgk
bugs1574863
Bug 1574863 - Handle 'foreign' files in logs directory gracefully. r=florian a=jorgk
chat/components/src/logger.js
--- a/chat/components/src/logger.js
+++ b/chat/components/src/logger.js
@@ -404,18 +404,20 @@ function closeSystemLogWriter(aAccount) 
  * @param aFilename the name of the file
  * @returns an Array, where the first element is a Date object for the date
  *          that the log file represents, and the file type as a string.
  */
 function getDateFromFilename(aFilename) {
   const kRegExp = /([\d]{4})-([\d]{2})-([\d]{2}).([\d]{2})([\d]{2})([\d]{2})([+-])([\d]{2})([\d]{2}).*\.([A-Za-z]+)$/;
 
   let r = aFilename.match(kRegExp);
-  if (!r)
+  if (!r) {
+    Cu.reportError("Found log file with name not maching YYYY-MM-DD.HHmmSS+ZZzz.format: " + aFilename);
     return [];
+  }
 
   // We ignore the timezone offset for now (FIXME)
   return [new Date(r[1], r[2] - 1, r[3], r[4], r[5], r[6]), r[10]];
 }
 
 /**
  * Returns true if a Conversation is both a chat conversation, and not
  * a Twitter conversation.
@@ -734,17 +736,19 @@ Logger.prototype = {
     let iterator = new OS.File.DirectoryIterator(OS.Path.dirname(aFilePath));
     let relevantEntries = [];
     return iterator.forEach(function(aEntry) {
       if (aEntry.isDir)
         return;
       let path = aEntry.path;
       let [logTime] = getDateFromFilename(OS.Path.basename(path));
 
-      if (targetDate == logTime.toDateString()) {
+      // If someone placed a 'foreign' file into the logs directory,
+      // pattern matching fails and getDateFromFilename() returns [].
+      if (logTime && targetDate == logTime.toDateString()) {
         relevantEntries.push({
           path,
           time: logTime,
         });
       }
     }).then(() => {
       iterator.close();
       return new Log(relevantEntries);