FW: Fix for 688048 ijs driver broken in 8.51 for ProcessColorModel=Gray versus 8.50

"Dan Coby" <[email protected]> Fri, 22 Apr 2005 14:41:10 -0700
Newsgroups gmane.comp.printing.ghostscript.patches
Message-ID <[email protected]>
Fix for 688048  ijs driver broken in 8.51 for ProcessColorModel=Gray versus 8.50 

DETAILS:

There were a few minor problems with the gdevijs.c file.

1.  The file has been corrupted with extra ^M's at the end of lines.  (These
cause problems with the MSVC debugger since MSVC gets confused about which
source line is being debugged..  They can also cause problem with the Borland
Compiler.)  This a reoccurring problem and I am getting tired of seeing it.

Unfortunately they are not visible in the diff shown below or in the MSVC
debugger.

2.  The routine gsijs_read_string_malloc has some problems.  The call to
gs_free has str as a parameter when it should have been *str.  (str is a
pointer to a pointer.)  This is the major cause of the reported problem.
The routine only released the old string when the new one needed to be
larger however it always allocated a new string.  Since this is non GC memory,
this causes a leak.


The patch by Hin-Tak Leung hides the problem by increasing the size allocated
for the ColorSpace string to a point where the erroneous call to gs_free was
not being executed (at least in the situation which he describes).


Dan


Index: src/gdevijs.c
===================================================================
RCS file: /cvs/ghostscript/gs/src/gdevijs.c,v
retrieving revision 1.10
diff -u -r1.10 gdevijs.c
--- src/gdevijs.c       10 Mar 2005 14:57:23 -0000      1.10
+++ src/gdevijs.c       22 Apr 2005 21:03:16 -0000
@@ -50,54 +50,54 @@
 private dev_proc_output_page(gsijs_output_page);
 private dev_proc_get_params(gsijs_get_params);
 private dev_proc_put_params(gsijs_put_params);
-private dev_proc_finish_copydevice(gsijs_finish_copydevice);
+private dev_proc_finish_copydevice(gsijs_finish_copydevice);

 private const gx_device_procs gsijs_procs = {
-       gsijs_open,
-       NULL,   /* get_initial_matrix */
-       NULL,   /* sync_output */
-       gsijs_output_page,
-       gsijs_close,
-       gx_default_rgb_map_rgb_color,
-       gx_default_rgb_map_color_rgb,
-       NULL,   /* fill_rectangle */
-       NULL,   /* tile_rectangle */
-       NULL,   /* copy_mono */
-       NULL,   /* copy_color */
-       NULL,   /* draw_line */
-       NULL,   /* get_bits */
-       gsijs_get_params,
-       gsijs_put_params,
-       NULL,   /* map_cmyk_color */
-       NULL,   /* get_xfont_procs */
-       NULL,   /* get_xfont_device */
-       NULL,   /* map_rgb_alpha_color */
-       gx_page_device_get_page_device,
-       NULL,   /* get_alpha_bits */
-       NULL,   /* copy_alpha */
-       NULL,   /* get_band */
-       NULL,   /* copy_rop */
-       NULL,   /* fill_path */
-       NULL,   /* stroke_path */
-       NULL,   /* fill_mask */
-       NULL,   /* fill_trapezoid */
-       NULL,   /* fill_parallelogram */
-       NULL,   /* fill_triangle */
-       NULL,   /* draw_thin_line */
-       NULL,   /* begin_image */
-       NULL,   /* image_data */
-       NULL,   /* end_image */
-       NULL,   /* strip_tile_rectangle */
-       NULL,   /* strip_copy_rop, */
-       NULL,   /* get_clipping_box */
-       NULL,   /* begin_typed_image */
-       NULL,   /* get_bits_rectangle */
-       NULL,   /* map_color_rgb_alpha */
-       NULL,   /* create_compositor */
-       NULL,   /* get_hardware_params */
-       NULL,   /* text_begin */
-       gsijs_finish_copydevice
-};
+       gsijs_open,
+       NULL,   /* get_initial_matrix */
+       NULL,   /* sync_output */
+       gsijs_output_page,
+       gsijs_close,
+       gx_default_rgb_map_rgb_color,
+       gx_default_rgb_map_color_rgb,
+       NULL,   /* fill_rectangle */
+       NULL,   /* tile_rectangle */
+       NULL,   /* copy_mono */
+       NULL,   /* copy_color */
+       NULL,   /* draw_line */
+       NULL,   /* get_bits */
+       gsijs_get_params,
+       gsijs_put_params,
+       NULL,   /* map_cmyk_color */
+       NULL,   /* get_xfont_procs */
+       NULL,   /* get_xfont_device */
+       NULL,   /* map_rgb_alpha_color */
+       gx_page_device_get_page_device,
+       NULL,   /* get_alpha_bits */
+       NULL,   /* copy_alpha */
+       NULL,   /* get_band */
+       NULL,   /* copy_rop */
+       NULL,   /* fill_path */
+       NULL,   /* stroke_path */
+       NULL,   /* fill_mask */
+       NULL,   /* fill_trapezoid */
+       NULL,   /* fill_parallelogram */
+       NULL,   /* fill_triangle */
+       NULL,   /* draw_thin_line */
+       NULL,   /* begin_image */
+       NULL,   /* image_data */
+       NULL,   /* end_image */
+       NULL,   /* strip_tile_rectangle */
+       NULL,   /* strip_copy_rop, */
+       NULL,   /* get_clipping_box */
+       NULL,   /* begin_typed_image */
+       NULL,   /* get_bits_rectangle */
+       NULL,   /* map_color_rgb_alpha */
+       NULL,   /* create_compositor */
+       NULL,   /* get_hardware_params */
+       NULL,   /* text_begin */
+       gsijs_finish_copydevice
+};

 typedef struct gx_device_ijs_s gx_device_ijs;

@@ -616,29 +616,29 @@
     return code;
 }

-/* Finish device initialization. */
-private int
-gsijs_finish_copydevice(gx_device *dev, const gx_device *from_dev)
-{
-    int code;
-    static const char rgb[] = "DeviceRGB";
-    gx_device_ijs *ijsdev = (gx_device_ijs *)dev;
-
-    code = gx_default_finish_copydevice(dev, from_dev);
-    if(code < 0)
-        return code;
-
-    if (!ijsdev->ColorSpace) {
-       ijsdev->ColorSpace = gs_malloc(ijsdev->memory, sizeof(rgb), 1,
-               "gsijs_finish_copydevice");
-        if (!ijsdev->ColorSpace)
-           return gs_note_error(gs_error_VMerror);
-        ijsdev->ColorSpace_size = sizeof(rgb);
-        memcpy(ijsdev->ColorSpace, rgb, sizeof(rgb));
-    }
-    return code;
-}
-
+/* Finish device initialization. */
+private int
+gsijs_finish_copydevice(gx_device *dev, const gx_device *from_dev)
+{
+    int code;
+    static const char rgb[] = "DeviceRGB";
+    gx_device_ijs *ijsdev = (gx_device_ijs *)dev;
+
+    code = gx_default_finish_copydevice(dev, from_dev);
+    if(code < 0)
+        return code;
+
+    if (!ijsdev->ColorSpace) {
+       ijsdev->ColorSpace = gs_malloc(ijsdev->memory, sizeof(rgb), 1,
+               "gsijs_finish_copydevice");
+        if (!ijsdev->ColorSpace)
+           return gs_note_error(gs_error_VMerror);
+        ijsdev->ColorSpace_size = sizeof(rgb);
+        memcpy(ijsdev->ColorSpace, rgb, sizeof(rgb));
+    }
+    return code;
+}
+
 /* Close the gsijs driver */
 private int
 gsijs_close(gx_device *dev)
@@ -654,19 +654,17 @@

     code = gdev_prn_close(dev);
     if (ijsdev->IjsParams)
-       gs_free(dev->memory, ijsdev->IjsParams, ijsdev->IjsParams_size, 1,
-           "gsijs_read_string_malloc");
+       gs_free(dev->memory, ijsdev->IjsParams,
+               ijsdev->IjsParams_size, 1, "gsijs_read_string_malloc");
     if (ijsdev->ColorSpace)
        gs_free(dev->memory, ijsdev->ColorSpace,
-               ijsdev->ColorSpace_size, 1,
-               "gsijs_read_string_malloc");
+               ijsdev->ColorSpace_size, 1, "gsijs_read_string_malloc");
     if (ijsdev->DeviceManufacturer)
        gs_free(dev->memory, ijsdev->DeviceManufacturer,
-               ijsdev->DeviceManufacturer_size, 1,
-               "gsijs_read_string_malloc");
+               ijsdev->DeviceManufacturer_size, 1, "gsijs_read_string_malloc");
     if (ijsdev->DeviceModel)
-       gs_free(dev->memory, ijsdev->DeviceModel, ijsdev->DeviceModel_size, 1,
-               "gsijs_read_string_malloc");
+       gs_free(dev->memory, ijsdev->DeviceModel,
+               ijsdev->DeviceModel_size, 1, "gsijs_read_string_malloc");
     ijsdev->IjsParams = NULL;
     ijsdev->IjsParams_size = 0;
     ijsdev->DeviceManufacturer = NULL;
@@ -980,14 +978,16 @@
                code = gs_error_rangecheck;
                goto e;
            }
-           if (new_value.size >= *size) {
+           if (new_value.size + 1 != *size) {
                if (*str)
-                   gs_free(plist->memory, str, *size, 1, "gsijs_read_string_malloc");
+                   gs_free(plist->memory, *str, *size, 1,
+                                       "gsijs_read_string_malloc");
                *str = NULL;
                *size = 0;
            }
-           *str = gs_malloc(plist->memory, new_value.size + 1, 1,
-               "gsijs_read_string_malloc");
+           if (*str == NULL)
+               *str = gs_malloc(plist->memory, new_value.size + 1, 1,
+                                       "gsijs_read_string_malloc");
            if (*str == NULL) {
                 code = gs_note_error(gs_error_VMerror);
                 goto e;