From 3154c5b1e8f8c70ba383a77da52ba57c16178e5d Mon Sep 17 00:00:00 2001
From: Zdenek Dohnal <zdohnal@redhat.com>
Date: Tue, 18 Feb 2020 15:39:00 +0100
Subject: [PATCH] 1802969 - Service "cups-browsed" is crashing all the time

---
 cups-filters-abrt.patch | 66 +++++++++++++++++++++++++++++++++++++++++
 cups-filters.spec       | 10 ++++++-
 2 files changed, 75 insertions(+), 1 deletion(-)
 create mode 100644 cups-filters-abrt.patch

diff --git a/cups-filters-abrt.patch b/cups-filters-abrt.patch
new file mode 100644
index 0000000..75cd5e1
--- /dev/null
+++ b/cups-filters-abrt.patch
@@ -0,0 +1,66 @@
+diff --git a/utils/cups-browsed.c b/utils/cups-browsed.c
+index d83420a..0ae3400 100644
+--- a/utils/cups-browsed.c
++++ b/utils/cups-browsed.c
+@@ -2491,18 +2491,35 @@ int check_printer_with_options(char* cluster_name, int idx_option1,
+   remote_printer_t     *p;
+   cups_array_t         *first_attributes_value;
+   cups_array_t         *second_attributes_value;
+-  char                 *borderless_pagesize;
++  char                 *borderless_pagesize = NULL;
+   int                  option1_is_size = 0, option2_is_size = 0;
++  unsigned long int    max_length = 0, option1_len = 0, option2_len = 0, t_len = 0;
+   char  t[] = ".Borderless";
+
+-  borderless_pagesize = malloc(sizeof(char) * 32);
++  t_len = strlen(t);
++  if (option1)
++    option1_len = strlen(option1);
++  if (option2)
++    option2_len = strlen(option2);
++
++  /* Seems to be possible to have both options...*/
++  max_length = option1_len + option2_len + (2 * t_len) + 1;
++
++  borderless_pagesize = (char *)malloc(sizeof(char) * max_length);
++  if (borderless_pagesize == NULL)
++  {
++    debug_printf("check_printer_with_options: Run out of memory.\n");
++    return 0;
++  }
++  memset(borderless_pagesize, 0, max_length);
++
+   if (!strcmp(ppd_keywords[idx_option1], "PageSize") ||
+       !strcmp(ppd_keywords[idx_option1], "PageRegion")) {
+     /* Check that we are generating .Borderless for the correct size, i.e We
+        are generating 4x5.Borderless for 4x5 and not generating 
+        4x5.Borderless.Borderless for 4x5.Borderless */
+-    if (strlen(option1) >= 11 &&
+-	!strcmp(&option1[strlen(option1) - strlen(t)], t))
++    if (option1_len >= 11 &&
++	!strcmp(&option1[option1_len - t_len], t))
+       ;
+     else {
+       strcat(borderless_pagesize, option1);
+@@ -2512,8 +2529,8 @@ int check_printer_with_options(char* cluster_name, int idx_option1,
+   }
+   if (!strcmp(ppd_keywords[idx_option2], "PageSize") ||
+       !strcmp(ppd_keywords[idx_option2], "PageRegion")) {
+-    if(strlen(option2) >=11 &&
+-       !strcmp(&option2[strlen(option2) - strlen(t)], t))
++    if(option2_len >=11 &&
++       !strcmp(&option2[option2_len - t_len], t))
+       ;
+     else {
+       strcat(borderless_pagesize, option2);
+@@ -2536,7 +2553,10 @@ int check_printer_with_options(char* cluster_name, int idx_option1,
+       if (cupsArrayFind(second_attributes_value,(void*)option2) ||
+ 	  (option2_is_size && cupsArrayFind(second_attributes_value,
+ 					    (void*)borderless_pagesize)))
++      {
++        free(borderless_pagesize);
+         return 1;
++      }
+     }
+   }
+   free(borderless_pagesize);
diff --git a/cups-filters.spec b/cups-filters.spec
index 0c387d9..4d198d6 100644
--- a/cups-filters.spec
+++ b/cups-filters.spec
@@ -4,7 +4,7 @@
 Summary: OpenPrinting CUPS filters and backends
 Name:    cups-filters
 Version: 1.27.0
-Release: 1%{?dist}
+Release: 2%{?dist}
 
 # For a breakdown of the licensing, see COPYING file
 # GPLv2:   filters: commandto*, imagetoraster, pdftops, rasterto*,
@@ -27,6 +27,9 @@ Patch01: cups-filters-createall.patch
 # instead of 'cups-browsed' as Ubuntu does. I can repack the project later,
 # so cups-browsed would have separate sub package, so the link would be correct
 Patch02: cups-browsed.8.patch 
+# crash on uninitialized string
+# reported upstream https://github.com/OpenPrinting/cups-filters/pull/204
+Patch03: cups-filters-abrt.patch
 
 Requires: cups-filters-libs%{?_isa} = %{version}-%{release}
 
@@ -137,6 +140,8 @@ This is the development package for OpenPrinting CUPS filters and backends.
 %patch01 -p1 -b .createall
 # links in manpage
 %patch02 -p1 -b .manpage
+# crash in cups-browsed
+%patch03 -p1 -b .abrt
 
 %build
 # work-around Rpath
@@ -305,6 +310,9 @@ done
 %{_libdir}/libfontembed.so
 
 %changelog
+* Tue Feb 17 2020 Zdenek Dohnal <zdohnal@redhat.com> - 1.27.0-2
+- 1802969 - Service "cups-browsed" is crashing all the time
+
 * Tue Jan 28 2020 Zdenek Dohnal <zdohnal@redhat.com> - 1.27.0-1
 - 1.27.0
 - add post scriptlet for update