[Groonga-commit] ranguba/chupa-text at 0e80b77 [master] gzip: handle error

Back to archive index
Kouhei Sutou null+****@clear*****
Fri Mar 1 19:05:04 JST 2019


Kouhei Sutou	2019-03-01 19:05:04 +0900 (Fri, 01 Mar 2019)

  Revision: 0e80b778d9b03e16b986178e989b42b57ecb52a9
  https://github.com/ranguba/chupa-text/commit/0e80b778d9b03e16b986178e989b42b57ecb52a9

  Message:
    gzip: handle error

  Modified files:
    lib/chupa-text/decomposers/gzip.rb
    test/decomposers/test-gzip.rb

  Modified: lib/chupa-text/decomposers/gzip.rb (+23 -2)
===================================================================
--- lib/chupa-text/decomposers/gzip.rb    2019-03-01 18:26:38 +0900 (7733254)
+++ lib/chupa-text/decomposers/gzip.rb    2019-03-01 19:05:04 +0900 (38aff49)
@@ -19,6 +19,8 @@ require "zlib"
 module ChupaText
   module Decomposers
     class Gzip < Decomposer
+      include Loggable
+
       registry.register("gzip", self)
 
       TARGET_EXTENSIONS = ["gz", "tgz"]
@@ -33,8 +35,7 @@ module ChupaText
       end
 
       def decompose(data)
-        data.open do |input|
-          reader = Zlib::GzipReader.new(input)
+        open_reader(data) do |reader|
           uri = nil
           case data.extension
           when "gz"
@@ -46,6 +47,26 @@ module ChupaText
           yield(extracted)
         end
       end
+
+      private
+      def open_reader(data)
+        data.open do |input|
+          begin
+            yield(Zlib::GzipReader.new(input))
+          rescue Zlib::Error => zlib_error
+            error do
+              message = "#{log_tag} Failed to uncompress: "
+              message << "#{zlib_error.class}: #{zlib_error.message}\n"
+              message << zlib_error.backtrace.join("\n")
+              message
+            end
+          end
+        end
+      end
+
+      def log_tag
+        "[decomposer][gzip]"
+      end
     end
   end
 end

  Modified: test/decomposers/test-gzip.rb (+19 -2)
===================================================================
--- test/decomposers/test-gzip.rb    2019-03-01 18:26:38 +0900 (075fb00)
+++ test/decomposers/test-gzip.rb    2019-03-01 19:05:04 +0900 (3fe9c76)
@@ -1,4 +1,4 @@
-# Copyright (C) 2013-2017  Kouhei Sutou <kou****@clear*****>
+# Copyright (C) 2013-2019  Kouhei Sutou <kou****@clear*****>
 #
 # This library is free software; you can redistribute it and/or
 # modify it under the terms of the GNU Lesser General Public
@@ -83,7 +83,6 @@ class TestDecomposersGzip < Test::Unit::TestCase
       end
     end
 
-
     sub_test_case("tgz") do
       def setup
         super
@@ -109,5 +108,23 @@ class TestDecomposersGzip < Test::Unit::TestCase
                      decompose(@data).collect(&:source))
       end
     end
+
+    def test_invalid
+      data = ChupaText::Data.new
+      data.body = "Hello"
+      data.size = data.body.bytesize
+      data.mime_type = "application/gzip"
+      messages = capture_log do
+        assert_equal([], decompose(data).collect(&:body))
+      end
+      assert_equal([
+                     [
+                       :error,
+                       "[decomposer][gzip] Failed to uncompress: " +
+                       "Zlib::GzipFile::Error: not in gzip format",
+                     ],
+                   ],
+                   messages)
+    end
   end
 end
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.osdn.me/mailman/archives/groonga-commit/attachments/20190301/ee7fcbea/attachment-0001.html>


More information about the Groonga-commit mailing list
Back to archive index