From 9fe09c0e0746067f9e9774229b3d0beb35c5766b Mon Sep 17 00:00:00 2001
From: Nils Philippsen <nils@redhat.com>
Date: Wed, 2 Apr 2014 14:54:05 +0200
Subject: [PATCH] patch: coverity

Squashed commit of the following:

commit e26d784a5217d1609e774678d8abaeb2e1ecdadc
Author: Nils Philippsen <nils@redhat.com>
Date:   Tue Mar 11 10:28:02 2014 +0100

    ensure loop is run at least once while scaling

commit e5bf8c8a659a7b03ae3eb130ebfaad292e16816d
Author: Nils Philippsen <nils@redhat.com>
Date:   Thu Feb 27 15:07:42 2014 +0100

    remove stray semicolon

commit ad928333ba038051ca0a405470dadeaa0f365b95
Author: Nils Philippsen <nils@redhat.com>
Date:   Thu Feb 27 15:06:17 2014 +0100

    use actual pointer type in realloc()

commit 11dfb289d03555218318ce1a284c1165bbc58b22
Author: Nils Philippsen <nils@redhat.com>
Date:   Tue Feb 25 17:42:54 2014 +0100

    don't overflow size expression for allocation

commit 5daccfc14f129c632f8409a89903d21c5495dd2f
Author: Nils Philippsen <nils@redhat.com>
Date:   Tue Feb 25 17:18:58 2014 +0100

    don't leak file descriptors and other resources

commit 034023d7aaac378b8ab02051d11392c639154842
Author: Nils Philippsen <nils@redhat.com>
Date:   Tue Feb 25 16:04:28 2014 +0100

    don't overrun buffers

commit f7454d62b5206e2100e8569e04ff4a6868cbd909
Author: Nils Philippsen <nils@redhat.com>
Date:   Tue Feb 25 15:56:40 2014 +0100

    check if files actually could be opened

commit de30cacd285c02382a32dc48cf9fe015223a57f7
Author: Nils Philippsen <nils@redhat.com>
Date:   Tue Feb 25 15:47:51 2014 +0100

    handle negative return values of some calls

commit b46a520ba85bda776b1a91de9f842ed96d97fa73
Author: Nils Philippsen <nils@redhat.com>
Date:   Tue Feb 25 11:54:31 2014 +0100

    only attempt to close successfully opened files

commit bb6a8ccab418f89fa6fb367874f84aa56195cca0
Author: Nils Philippsen <nils@redhat.com>
Date:   Tue Feb 25 11:54:15 2014 +0100

    bail out early if memory can't be allocated

commit 9b1e34c1d099c0a7acf7bd5aec8e3f0c2d2eafa8
Author: Nils Philippsen <nils@redhat.com>
Date:   Tue Feb 25 11:53:11 2014 +0100

    gtk_entry_get_text() isn't supposed to return NULL

commit bc7553caaea1955e7fa48f3a2f4c0773d40204c3
Author: Nils Philippsen <nils@redhat.com>
Date:   Tue Feb 25 11:00:11 2014 +0100

    negate comparison instead of operand

commit d1d4675a416383d808def730ade4f6b5b1a493b8
Author: Nils Philippsen <nils@redhat.com>
Date:   Tue Feb 25 10:55:42 2014 +0100

    use int types for fgetc() results

commit cbdc2df85367c27ff56fb9cdd77a61c6d82be0e1
Author: Nils Philippsen <nils@redhat.com>
Date:   Tue Feb 25 10:36:53 2014 +0100

    avoid freeing static strings

commit 0c377b9658daa5dcdbc6c260baa4e31237202f3e
Author: Nils Philippsen <nils@redhat.com>
Date:   Wed Feb 19 16:10:35 2014 +0100

    use guchar* for PNG profile buffer

commit 70152131771b891fe04b3087e3b73c42e6d5ebbe
Author: Nils Philippsen <nils@redhat.com>
Date:   Wed Feb 19 16:09:46 2014 +0100

    cast time_t into long instead of int

commit 0ac0683bcba7d8cf4e786abc3abe9c3e3c7d60a0
Author: Nils Philippsen <nils@redhat.com>
Date:   Tue Feb 18 17:55:02 2014 +0100

    use glib macros where ints are transported in pointers

commit 8e85947fe5a592a3f938ab5bb2dc79488b638515
Author: Nils Philippsen <nils@redhat.com>
Date:   Mon Feb 17 13:42:13 2014 +0100

    handle return values of some file related system calls

commit f4ba79f720a9a1e4a3caddc77d0de19a7d2fb75c
Author: Nils Philippsen <nils@redhat.com>
Date:   Mon Feb 17 12:07:09 2014 +0100

    avoid leaking resources
---
 src/xsane-back-gtk.c          |  75 +++++++++++++---
 src/xsane-batch-scan.c        |  41 ++++-----
 src/xsane-email-project.c     |  83 +++++++++---------
 src/xsane-fax-project.c       |  14 ++-
 src/xsane-front-gtk.c         |  42 ++++-----
 src/xsane-multipage-project.c |   5 ++
 src/xsane-preview.c           |  34 ++++----
 src/xsane-save.c              | 166 ++++++++++++++++++++++-------------
 src/xsane-scan.c              |  17 ++--
 src/xsane-setup.c             |  70 +++++++--------
 src/xsane-text.h              |   1 +
 src/xsane-viewer.c            | 199 +++++++++++++++++++++++-------------------
 src/xsane.c                   |  65 ++++++++------
 13 files changed, 478 insertions(+), 334 deletions(-)

diff --git a/src/xsane-back-gtk.c b/src/xsane-back-gtk.c
index 6ef1506..6452f4b 100644
--- a/src/xsane-back-gtk.c
+++ b/src/xsane-back-gtk.c
@@ -40,6 +40,7 @@ SANE_Status xsane_control_option(SANE_Handle handle, SANE_Int option, SANE_Actio
 const SANE_Option_Descriptor *xsane_get_option_descriptor(SANE_Handle handle, SANE_Int option);
 const char *xsane_back_gtk_unit_string(SANE_Unit unit);
 void xsane_back_gtk_set_tooltip(GtkTooltips *tooltips, GtkWidget *widget, const gchar *desc);
+static int xsane_back_gtk_ensure_dir(const char *path);
 int xsane_back_gtk_make_path(size_t buf_size, char *buf, const char *prog_name, const char *dir_name,
                              const char *prefix, const char *dev_name, const char *postfix, int location);
 void xsane_back_gtk_set_option(int opt_num, void *val, SANE_Action action);
@@ -245,11 +246,47 @@ void xsane_back_gtk_set_tooltip(GtkTooltips *tooltips, GtkWidget *widget, const
 
 /* ----------------------------------------------------------------------------------------------------------------- */
 
+static int xsane_back_gtk_ensure_dir(const char *path)
+{
+  struct stat statbuf;
+
+  if (stat(path, &statbuf) < 0)
+  {
+    if (errno != ENOENT)
+    {
+      /* stat() failed and it wasn't because the file doesn't exist */
+      return -1;
+    }
+    else
+    {
+      /* if path doesn't exist, create directory */
+      if (mkdir(path, 0777) < 0)
+      {
+        /* mkdir() failed */
+        return -2;
+      }
+      else
+      {
+        return 0;
+      }
+    }
+  }
+
+  if (S_ISDIR(statbuf.st_mode))
+  {
+    return 0;
+  }
+
+  /* path exists and isn't a directory */
+  return -3;
+}
+
 int xsane_back_gtk_make_path(size_t buf_size, char *buf, const char *prog_name, const char *dir_name,
                              const char *prefix, const char *dev_name, const char *postfix, int location)
 {
  size_t len, extra;
  int i;
+ char msgbuf[TEXTBUFSIZE];
 
   DBG(DBG_proc, "xsane_back_gtk_make_path\n");
 
@@ -263,7 +300,10 @@ int xsane_back_gtk_make_path(size_t buf_size, char *buf, const char *prog_name,
     {
       snprintf(buf, buf_size-2, "%s", STRINGIFY(XSANE_FIXED_APPDATA_DIR));
     }
-    mkdir(buf, 0777);	/* ensure ~/.sane directory exists */
+    if (xsane_back_gtk_ensure_dir(buf))
+    {
+      goto dir_failed;
+    }
   }
   else if (location == XSANE_PATH_SYSTEM) /* make path to system file */
   {
@@ -290,7 +330,10 @@ int xsane_back_gtk_make_path(size_t buf_size, char *buf, const char *prog_name,
     len += extra;
 
     buf[len] = '\0';
-    mkdir(buf, 0777);	/* ensure ~/.sane/PROG_NAME directory exists */
+    if (xsane_back_gtk_ensure_dir(buf))
+    {
+      goto dir_failed;
+    }
 
     buf[len++] = SLASH; /* OS/2 does not like slash at end of mktemp-path */
   }
@@ -314,7 +357,10 @@ int xsane_back_gtk_make_path(size_t buf_size, char *buf, const char *prog_name,
     buf[len++] = SLASH;
 
     buf[len] = '\0';
-    mkdir(buf, 0777);	/* ensure DIR_NAME directory exists */
+    if (xsane_back_gtk_ensure_dir(buf))
+    {
+      goto dir_failed;
+    }
   }
 
   if (len >= buf_size)
@@ -454,6 +500,12 @@ filename_too_long:
   xsane_back_gtk_error(ERR_FILENAME_TOO_LONG, FALSE);
   errno = E2BIG;
  return -1;
+
+dir_failed:
+  snprintf(msgbuf, sizeof(msgbuf), "%s %s.", ERR_FAILED_CREATE_ENSURE_DIR,
+           strerror(errno));
+  xsane_back_gtk_error(msgbuf, FALSE);
+  return -1;
 }
 
 /* ----------------------------------------------------------------------------------------------------------------- */
@@ -784,11 +836,15 @@ gint xsane_back_gtk_decision(gchar *title, gchar **xpm_d,  gchar *message, gchar
 void xsane_back_gtk_ipc_dialog_callback(gpointer data, gint source, GdkInputCondition cond)
 {
  char message[TEXTBUFSIZE];
- size_t bytes;
+ ssize_t bytes;
 
   DBG(DBG_proc, "xsane_back_gtk_message\n");
 
-  bytes = read(xsane.ipc_pipefd[0], message, sizeof(message)-1);
+  if ((bytes = read(xsane.ipc_pipefd[0], message, sizeof(message)-1)) < 0)
+  {
+    DBG(DBG_error, "xsane_back_gtk_message(): can't read from pipe\n");
+    return;
+  }
   message[bytes] = 0;
 
   xsane_back_gtk_decision(ERR_HEADER_CHILD_PROCESS_ERROR, (gchar **) error_xpm, message, BUTTON_CLOSE, 0 /* no reject text */, FALSE);
@@ -1980,7 +2036,7 @@ static void xsane_back_gtk_value_update(GtkAdjustment *adj_data, DialogElement *
 static void xsane_back_gtk_range_display_value_right_callback(GtkAdjustment *adjust, gpointer data)
 {
  gchar buf[TEXTBUFSIZE];
- int digits = (int) data;
+ int digits = GPOINTER_TO_INT(data);
  GtkLabel *label;
  
   snprintf(buf, sizeof(buf), "%1.*f", digits, adjust->value);
@@ -2028,7 +2084,7 @@ void xsane_back_gtk_range_new(GtkWidget *parent, const char *name, gfloat val,
     gtk_widget_set_size_request(value_label, 45, -1);
     gtk_box_pack_end(GTK_BOX(hbox), value_label, FALSE, FALSE, 1);
  
-    g_signal_connect(elem->data, "value_changed", (GtkSignalFunc) xsane_back_gtk_range_display_value_right_callback, (void *) digits);
+    g_signal_connect(elem->data, "value_changed", (GtkSignalFunc) xsane_back_gtk_range_display_value_right_callback, GINT_TO_POINTER(digits));
     gtk_object_set_data(GTK_OBJECT(elem->data), "value-label", value_label);
     g_signal_emit_by_name(GTK_OBJECT(elem->data), "value_changed"); /* update value */
     gtk_widget_show(value_label);
@@ -2318,10 +2374,7 @@ static void xsane_back_gtk_text_entry_callback(GtkWidget *w, gpointer data)
   buf[0] = '\0';
 
   text = gtk_entry_get_text(GTK_ENTRY(elem->widget));
-  if (text)
-  {
-    strncpy(buf, text, opt->size);
-  }
+  strncpy(buf, text, opt->size);
   buf[opt->size - 1] = '\0';
 
   xsane_back_gtk_set_option(opt_num, buf, SANE_ACTION_SET_VALUE);
diff --git a/src/xsane-batch-scan.c b/src/xsane-batch-scan.c
index 90cc0e0..12eb1b2 100644
--- a/src/xsane-batch-scan.c
+++ b/src/xsane-batch-scan.c
@@ -291,7 +291,7 @@ int xsane_batch_scan_load_list_from_file(char *filename)
 
   fd = open(filename, O_RDONLY);
 
-  if (fd > 0)
+  if (fd >= 0)
   {
     w.io.fd = fd;
     w.io.read = read;
@@ -396,11 +396,11 @@ static void xsane_batch_scan_save_list(void)
   sprintf(windowname, "%s %s %s", xsane.prog_name, WINDOW_SAVE_BATCH_LIST, xsane.device_text);
   xsane_back_gtk_make_path(sizeof(filename), filename, "xsane", "batch-lists", 0, "default", ".xbl", XSANE_PATH_LOCAL_SANE);
 
-  if (!xsane_back_gtk_get_filename(windowname, filename, sizeof(filename), filename, NULL, NULL, XSANE_FILE_CHOOSER_ACTION_SAVE, XSANE_GET_FILENAME_SHOW_NOTHING, XSANE_FILE_FILTER_ALL | XSANE_FILE_FILTER_BATCHLIST, XSANE_FILE_FILTER_BATCHLIST));
+  if (!xsane_back_gtk_get_filename(windowname, filename, sizeof(filename), filename, NULL, NULL, XSANE_FILE_CHOOSER_ACTION_SAVE, XSANE_GET_FILENAME_SHOW_NOTHING, XSANE_FILE_FILTER_ALL | XSANE_FILE_FILTER_BATCHLIST, XSANE_FILE_FILTER_BATCHLIST))
   {
     fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0600);
 
-    if (fd > 0)
+    if (fd >= 0)
     {
       w.io.fd = fd;
       w.io.read = read;
@@ -680,7 +680,6 @@ static GtkWidget *xsane_batch_scan_create_list_entry(Batch_Scan_Parameters *para
  GtkWidget *list_item;
  GtkWidget *hbox;
  int size = 120;
- char *data;
 
   list_item = gtk_list_item_new();
 
@@ -688,8 +687,6 @@ static GtkWidget *xsane_batch_scan_create_list_entry(Batch_Scan_Parameters *para
   gtk_container_add(GTK_CONTAINER(list_item), hbox);
   gtk_widget_show(hbox);
 
-  data = calloc(size, size);
-
   parameters->gtk_preview = gtk_preview_new(GTK_PREVIEW_COLOR);
   gtk_preview_size(GTK_PREVIEW(parameters->gtk_preview), size, size);
   gtk_box_pack_start(GTK_BOX(hbox), parameters->gtk_preview, FALSE, FALSE, 0);
@@ -725,13 +722,17 @@ void xsane_batch_scan_add()
 
   parameters = calloc(1, sizeof(Batch_Scan_Parameters));
 
-  if (parameters)
+  if (!parameters)
   {
-    xsane_batch_scan_get_parameters(parameters);
-
-    parameters->name = strdup(TEXT_BATCH_AREA_DEFAULT_NAME);
+    DBG(DBG_error, "xsane_batch_scan_add(): couldn't allocate memory for "
+                   "parameters\n");
+    return;
   }
 
+  xsane_batch_scan_get_parameters(parameters);
+
+  parameters->name = strdup(TEXT_BATCH_AREA_DEFAULT_NAME);
+
   list_item = xsane_batch_scan_create_list_entry(parameters);
 
   /* scroll list to end */
@@ -796,7 +797,7 @@ static void xsane_batch_scan_rotate_mirror(GtkWidget *widget, gpointer data)
  GList *select;
  GtkObject *list_item;
  Batch_Scan_Parameters *parameters = NULL;
- int rotate_info = (int) data;
+ int rotate_info = GPOINTER_TO_INT(data);
  int rotate, mirror;
 
   DBG(DBG_proc, "xsane_batch_scan_rotate_mirror\n");
@@ -834,7 +835,7 @@ static void xsane_batch_scan_rename_button_callback(GtkWidget *widget, gpointer
 {
   DBG(DBG_proc, "xsane_batch_scan_rename\n");
 
-  xsane_batch_scan_rename = (int) data;
+  xsane_batch_scan_rename = GPOINTER_TO_INT(data);
 }
 
 
@@ -881,7 +882,7 @@ static void xsane_batch_scan_rename_callback(GtkWidget *widget, gpointer data)
       gtk_window_set_resizable(GTK_WINDOW(rename_dialog), FALSE);
       snprintf(buf, sizeof(buf), "%s %s", xsane.prog_name, WINDOW_BATCH_RENAME);
       gtk_window_set_title(GTK_WINDOW(rename_dialog), buf);
-      g_signal_connect(GTK_OBJECT(rename_dialog), "delete_event", (GtkSignalFunc) xsane_batch_scan_rename_button_callback,(void *) -1);
+      g_signal_connect(GTK_OBJECT(rename_dialog), "delete_event", (GtkSignalFunc) xsane_batch_scan_rename_button_callback, GINT_TO_POINTER(-1));
       gtk_widget_show(rename_dialog);
 
       text = gtk_entry_new();
@@ -898,7 +899,7 @@ static void xsane_batch_scan_rename_callback(GtkWidget *widget, gpointer data)
 #else
       button = gtk_button_new_with_label(BUTTON_CANCEL);
 #endif
-      g_signal_connect(GTK_OBJECT(button), "clicked", (GtkSignalFunc) xsane_batch_scan_rename_button_callback, (void *) -1);
+      g_signal_connect(GTK_OBJECT(button), "clicked", (GtkSignalFunc) xsane_batch_scan_rename_button_callback, GINT_TO_POINTER(-1));
       gtk_box_pack_start(GTK_BOX(hbox), button, TRUE, TRUE, 0);
       gtk_widget_show(button);
 
@@ -909,7 +910,7 @@ static void xsane_batch_scan_rename_callback(GtkWidget *widget, gpointer data)
       button = gtk_button_new_with_label(BUTTON_OK);
 #endif
       GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
-      g_signal_connect(GTK_OBJECT(button), "clicked", (GtkSignalFunc) xsane_batch_scan_rename_button_callback, (void *) 1);
+      g_signal_connect(GTK_OBJECT(button), "clicked", (GtkSignalFunc) xsane_batch_scan_rename_button_callback, GINT_TO_POINTER(1));
       gtk_box_pack_start(GTK_BOX(hbox), button, TRUE, TRUE, 0);
       gtk_widget_grab_default(button);
       gtk_widget_show(button);
@@ -1000,11 +1001,11 @@ void xsane_create_batch_scan_dialog(const char *devicetext)
   xsane_button_new_with_pixmap(xsane.batch_scan_dialog->window, xsane.batch_scan_button_box, del_batch_xpm,   DESC_BATCH_DEL, (GtkSignalFunc) xsane_batch_scan_delete, NULL);
 
   xsane_button_new_with_pixmap(xsane.batch_scan_dialog->window, xsane.batch_scan_button_box, ascii_xpm,       DESC_BATCH_RENAME, (GtkSignalFunc) xsane_batch_scan_rename_callback, NULL);
-  xsane_button_new_with_pixmap(xsane.batch_scan_dialog->window, xsane.batch_scan_button_box, rotate90_xpm,    DESC_ROTATE90,     (GtkSignalFunc) xsane_batch_scan_rotate_mirror, (void *) 1);
-  xsane_button_new_with_pixmap(xsane.batch_scan_dialog->window, xsane.batch_scan_button_box, rotate180_xpm,   DESC_ROTATE180,    (GtkSignalFunc) xsane_batch_scan_rotate_mirror, (void *) 2);
-  xsane_button_new_with_pixmap(xsane.batch_scan_dialog->window, xsane.batch_scan_button_box, rotate270_xpm,   DESC_ROTATE270,    (GtkSignalFunc) xsane_batch_scan_rotate_mirror, (void *) 3);
-  xsane_button_new_with_pixmap(xsane.batch_scan_dialog->window, xsane.batch_scan_button_box, mirror_x_xpm,    DESC_MIRROR_X,     (GtkSignalFunc) xsane_batch_scan_rotate_mirror, (void *) 4);
-  xsane_button_new_with_pixmap(xsane.batch_scan_dialog->window, xsane.batch_scan_button_box, mirror_y_xpm,    DESC_MIRROR_Y,     (GtkSignalFunc) xsane_batch_scan_rotate_mirror, (void *) 6);
+  xsane_button_new_with_pixmap(xsane.batch_scan_dialog->window, xsane.batch_scan_button_box, rotate90_xpm,    DESC_ROTATE90,     (GtkSignalFunc) xsane_batch_scan_rotate_mirror, GINT_TO_POINTER(1));
+  xsane_button_new_with_pixmap(xsane.batch_scan_dialog->window, xsane.batch_scan_button_box, rotate180_xpm,   DESC_ROTATE180,    (GtkSignalFunc) xsane_batch_scan_rotate_mirror, GINT_TO_POINTER(2));
+  xsane_button_new_with_pixmap(xsane.batch_scan_dialog->window, xsane.batch_scan_button_box, rotate270_xpm,   DESC_ROTATE270,    (GtkSignalFunc) xsane_batch_scan_rotate_mirror, GINT_TO_POINTER(3));
+  xsane_button_new_with_pixmap(xsane.batch_scan_dialog->window, xsane.batch_scan_button_box, mirror_x_xpm,    DESC_MIRROR_X,     (GtkSignalFunc) xsane_batch_scan_rotate_mirror, GINT_TO_POINTER(4));
+  xsane_button_new_with_pixmap(xsane.batch_scan_dialog->window, xsane.batch_scan_button_box, mirror_y_xpm,    DESC_MIRROR_Y,     (GtkSignalFunc) xsane_batch_scan_rotate_mirror, GINT_TO_POINTER(6));
 
 
   /* the scolled window with the list */
diff --git a/src/xsane-email-project.c b/src/xsane-email-project.c
index 23fc942..413e3f7 100644
--- a/src/xsane-email-project.c
+++ b/src/xsane-email-project.c
@@ -895,6 +895,11 @@ static void xsane_email_project_update_project_status()
 
   snprintf(filename, sizeof(filename), "%s/xsane-mail-list", preferences.email_project);
   projectfile = fopen(filename, "r+b"); /* r+ = read and write, position = start of file */
+  if (!projectfile)
+  {
+    DBG(DBG_error, "Could not open project file '%s'.\n", filename);
+    return;
+  }
 
   snprintf(buf, 32, "%s@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@", xsane.email_status); /* fill 32 characters status line */
   fprintf(projectfile, "%s\n", buf); /* first line is status of email */
@@ -931,6 +936,11 @@ void xsane_email_project_save()
   }
 
   projectfile = fopen(filename, "wb"); /* write binary (b for win32) */
+  if (! projectfile)
+  {
+    DBG(DBG_error, "Could not open project file '%s'.\n", filename);
+    return;
+  }
 
   if (xsane.email_status)
   {
@@ -1271,7 +1281,7 @@ static void xsane_email_entry_rename_button_callback(GtkWidget *widget, gpointer
 {
   DBG(DBG_proc, "xsane_email_entry_rename\n");
 
-  xsane_email_entry_rename = (int) data;
+  xsane_email_entry_rename = GPOINTER_TO_INT(data);
 }
 
 /* ---------------------------------------------------------------------------------------------------------------------- */
@@ -1322,7 +1332,7 @@ static void xsane_email_entry_rename_callback(GtkWidget *widget, gpointer list)
     gtk_window_set_resizable(GTK_WINDOW(rename_dialog), FALSE);
     snprintf(filename, sizeof(filename), "%s %s", xsane.prog_name, WINDOW_EMAIL_RENAME);
     gtk_window_set_title(GTK_WINDOW(rename_dialog), filename);
-    g_signal_connect(GTK_OBJECT(rename_dialog), "delete_event", (GtkSignalFunc) xsane_email_entry_rename_button_callback, (void *) -1);
+    g_signal_connect(GTK_OBJECT(rename_dialog), "delete_event", (GtkSignalFunc) xsane_email_entry_rename_button_callback, GINT_TO_POINTER(-1));
     gtk_widget_show(rename_dialog);
 
     text = gtk_entry_new();
@@ -1339,7 +1349,7 @@ static void xsane_email_entry_rename_callback(GtkWidget *widget, gpointer list)
 #else
   button = gtk_button_new_with_label(BUTTON_CANCEL);
 #endif
-    g_signal_connect(GTK_OBJECT(button), "clicked", (GtkSignalFunc) xsane_email_entry_rename_button_callback,(void *) -1);
+    g_signal_connect(GTK_OBJECT(button), "clicked", (GtkSignalFunc) xsane_email_entry_rename_button_callback, GINT_TO_POINTER(-1));
     gtk_box_pack_start(GTK_BOX(hbox), button, TRUE, TRUE, 0);
     gtk_widget_show(button);
 
@@ -1348,7 +1358,7 @@ static void xsane_email_entry_rename_callback(GtkWidget *widget, gpointer list)
 #else
     button = gtk_button_new_with_label(BUTTON_OK);
 #endif
-    g_signal_connect(GTK_OBJECT(button), "clicked", (GtkSignalFunc) xsane_email_entry_rename_button_callback, (void *) 1);
+    g_signal_connect(GTK_OBJECT(button), "clicked", (GtkSignalFunc) xsane_email_entry_rename_button_callback, GINT_TO_POINTER(1));
     gtk_box_pack_start(GTK_BOX(hbox), button, TRUE, TRUE, 0);
     gtk_widget_show(button);
 
@@ -1526,6 +1536,11 @@ static void xsane_create_email(int fd)
   {
     DBG(DBG_error, "could not open email project file %s\n", filename);
 
+    if (projectfile)
+    {
+      fclose(projectfile);
+    }
+
     if (xsane.email_status)
     {
       free(xsane.email_status);
@@ -1766,7 +1781,7 @@ static void xsane_create_email(int fd)
 
 static void xsane_email_send_process()
 {
- int fd_socket;
+ int fd_socket = -1;
  int status;
  char *password;
  int i;
@@ -1787,36 +1802,20 @@ static void xsane_email_send_process()
 
     if (fd_socket < 0) /* could not open socket */
     {
-      if (xsane.email_status)
-      {
-        free(xsane.email_status);
-      }
       xsane.email_status = strdup(TEXT_EMAIL_STATUS_POP3_CONNECTION_FAILED);
-      xsane.email_progress_val = 0.0;
-      xsane_front_gtk_email_project_update_lockfile_status();
-
-      free(password);
-
-     return;
+      status = -1;
+      goto out;
     }
 
     status = pop3_login(fd_socket, preferences.email_auth_user, password);
 
     close(fd_socket);
+    fd_socket = -1;
 
     if (status == -1)
     {
-      if (xsane.email_status)
-      {
-        free(xsane.email_status);
-      }
       xsane.email_status = strdup(TEXT_EMAIL_STATUS_POP3_LOGIN_FAILED);
-      xsane.email_progress_val = 0.0;
-      xsane_front_gtk_email_project_update_lockfile_status();
-
-      free(password);
-
-     return;
+      goto out;
     }
 
     DBG(DBG_info, "POP3 authentication done\n");
@@ -1829,17 +1828,9 @@ static void xsane_email_send_process()
 
   if (fd_socket < 0) /* could not open socket */
   {
-    if (xsane.email_status)
-    {
-      free(xsane.email_status);
-    }
     xsane.email_status = strdup(TEXT_EMAIL_STATUS_SMTP_CONNECTION_FAILED);
-    xsane.email_progress_val = 0.0;
-    xsane_front_gtk_email_project_update_lockfile_status();
-
-    free(password);
-
-   return;
+    status = -1;
+    goto out;
   }
 
 
@@ -1847,7 +1838,8 @@ static void xsane_email_send_process()
                              preferences.email_authentication, preferences.email_auth_user, password);
   if (status == -1)
   {
-   return;
+    xsane.email_status = strdup(TEXT_EMAIL_STATUS_SMTP_ERR_DATA);
+    goto out;
   }
 
 
@@ -1855,19 +1847,30 @@ static void xsane_email_send_process()
 
   write_smtp_footer(fd_socket);
 
-  close(fd_socket);
+out:
+  if (fd_socket >= 0)
+  {
+    close(fd_socket);
+  }
 
   if (xsane.email_status)
   {
     free(xsane.email_status);
   }
-  xsane.email_status = strdup(TEXT_EMAIL_STATUS_SENT);
-  xsane.email_progress_val = 1.0;
+  if (status != -1)
+  {
+    xsane.email_status = strdup(TEXT_EMAIL_STATUS_SENT);
+    xsane.email_progress_val = 1.0;
+  }
+  else
+  {
+    xsane.email_progress_val = 0.0;
+  }
   xsane_front_gtk_email_project_update_lockfile_status();
 
   free(password);
 
-  _exit(0);
+  _exit(status);
 }
 
 /* ---------------------------------------------------------------------------------------------------------------------- */
diff --git a/src/xsane-fax-project.c b/src/xsane-fax-project.c
index 5750b55..c08af59 100644
--- a/src/xsane-fax-project.c
+++ b/src/xsane-fax-project.c
@@ -452,6 +452,12 @@ static void xsane_fax_project_update_project_status()
   snprintf(filename, sizeof(filename), "%s/xsane-fax-list", preferences.fax_project);
   projectfile = fopen(filename, "r+b"); /* r+ = read and write, position = start of file */
 
+  if (! projectfile)
+  {
+    DBG(DBG_error, "Could not open project file '%s'.\n", filename);
+    return;
+  }
+
   snprintf(buf, 32, "%s@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@", xsane.fax_status); /* fill 32 characters status line */
   fprintf(projectfile, "%s\n", buf); /* first line is status of mail */
 
@@ -754,7 +760,7 @@ static void xsane_fax_entry_rename_button_callback(GtkWidget *widget, gpointer d
 {
   DBG(DBG_proc, "xsane_fax_entry_rename\n");
 
-  xsane_fax_entry_rename = (int) data;
+  xsane_fax_entry_rename = GPOINTER_TO_INT(data);
 }
 
 /* ---------------------------------------------------------------------------------------------------------------------- */
@@ -805,7 +811,7 @@ static void xsane_fax_entry_rename_callback(GtkWidget *widget, gpointer list)
     gtk_window_set_resizable(GTK_WINDOW(rename_dialog), FALSE);
     snprintf(filename, sizeof(filename), "%s %s", xsane.prog_name, WINDOW_FAX_RENAME);
     gtk_window_set_title(GTK_WINDOW(rename_dialog), filename);
-    g_signal_connect(GTK_OBJECT(rename_dialog), "delete_event", (GtkSignalFunc) xsane_fax_entry_rename_button_callback,(void *) -1);
+    g_signal_connect(GTK_OBJECT(rename_dialog), "delete_event", (GtkSignalFunc) xsane_fax_entry_rename_button_callback, GINT_TO_POINTER(-1));
     gtk_widget_show(rename_dialog);
 
     text = gtk_entry_new();
@@ -822,7 +828,7 @@ static void xsane_fax_entry_rename_callback(GtkWidget *widget, gpointer list)
 #else
   button = gtk_button_new_with_label(BUTTON_CANCEL);
 #endif
-    g_signal_connect(GTK_OBJECT(button), "clicked", (GtkSignalFunc) xsane_fax_entry_rename_button_callback, (void *) -1);
+    g_signal_connect(GTK_OBJECT(button), "clicked", (GtkSignalFunc) xsane_fax_entry_rename_button_callback, GINT_TO_POINTER(-1));
     gtk_box_pack_start(GTK_BOX(hbox), button, TRUE, TRUE, 0);
     gtk_widget_show(button);
 
@@ -832,7 +838,7 @@ static void xsane_fax_entry_rename_callback(GtkWidget *widget, gpointer list)
 #else
     button = gtk_button_new_with_label(BUTTON_OK);
 #endif
-    g_signal_connect(GTK_OBJECT(button), "clicked", (GtkSignalFunc) xsane_fax_entry_rename_button_callback, (void *) 1);
+    g_signal_connect(GTK_OBJECT(button), "clicked", (GtkSignalFunc) xsane_fax_entry_rename_button_callback, GINT_TO_POINTER(1));
     gtk_box_pack_start(GTK_BOX(hbox), button, TRUE, TRUE, 0);
     gtk_widget_show(button);
 
diff --git a/src/xsane-front-gtk.c b/src/xsane-front-gtk.c
index 7bb49b0..84452f2 100644
--- a/src/xsane-front-gtk.c
+++ b/src/xsane-front-gtk.c
@@ -598,7 +598,7 @@ gint xsane_authorization_callback(SANE_String_Const resource,
     authorize_dialog = gtk_window_new(GTK_WINDOW_TOPLEVEL);
     gtk_window_set_position(GTK_WINDOW(authorize_dialog), GTK_WIN_POS_CENTER);
     gtk_window_set_resizable(GTK_WINDOW(authorize_dialog), FALSE);
-    g_signal_connect(GTK_OBJECT(authorize_dialog), "delete_event", GTK_SIGNAL_FUNC(xsane_authorization_button_callback), (void *) -1); /* -1 = cancel */
+    g_signal_connect(GTK_OBJECT(authorize_dialog), "delete_event", GTK_SIGNAL_FUNC(xsane_authorization_button_callback), GINT_TO_POINTER(-1)); /* -1 = cancel */
     snprintf(buf, sizeof(buf), "%s %s", xsane.prog_name, WINDOW_AUTHORIZE);
     gtk_window_set_title(GTK_WINDOW(authorize_dialog), buf);
     xsane_set_window_icon(authorize_dialog, 0);
@@ -667,7 +667,7 @@ gint xsane_authorization_callback(SANE_String_Const resource,
 #else
     button = gtk_button_new_with_label(BUTTON_CANCEL);
 #endif
-    g_signal_connect(GTK_OBJECT(button), "clicked", GTK_SIGNAL_FUNC(xsane_authorization_button_callback), (void *) -1);
+    g_signal_connect(GTK_OBJECT(button), "clicked", GTK_SIGNAL_FUNC(xsane_authorization_button_callback), GINT_TO_POINTER(-1));
     gtk_box_pack_start(GTK_BOX(hbox), button, TRUE, TRUE, 10); /* x-space around cancel-button */
     gtk_widget_show(button);
 
@@ -677,7 +677,7 @@ gint xsane_authorization_callback(SANE_String_Const resource,
     button = gtk_button_new_with_label(BUTTON_OK);
 #endif
     GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
-    g_signal_connect(GTK_OBJECT(button), "clicked", GTK_SIGNAL_FUNC(xsane_authorization_button_callback), (void *) 1);
+    g_signal_connect(GTK_OBJECT(button), "clicked", GTK_SIGNAL_FUNC(xsane_authorization_button_callback), GINT_TO_POINTER(1));
     gtk_box_pack_start(GTK_BOX(hbox), button, TRUE, TRUE, 10); /* x-space around OK-button */
     gtk_widget_grab_default(button);
     gtk_widget_show(button);
@@ -1107,7 +1107,7 @@ void xsane_option_menu_new_with_pixmap(GdkWindow *window, GtkBox *parent, const
 static void xsane_range_display_value_right_callback(GtkAdjustment *adjust, gpointer data)
 {
  gchar buf[TEXTBUFSIZE];
- int digits = (int) data;
+ int digits = GPOINTER_TO_INT(data);
  GtkLabel *label;
 
   snprintf(buf, sizeof(buf), "%1.*f", digits, adjust->value);
@@ -1144,7 +1144,7 @@ void xsane_range_new(GtkBox *parent, char *labeltext, const char *desc,
     gtk_widget_set_size_request(value_label, 35, -1);
     gtk_box_pack_end(GTK_BOX(hbox), value_label, FALSE, FALSE, 1);
     
-    g_signal_connect(*data, "value_changed", (GtkSignalFunc) xsane_range_display_value_right_callback, (void *) digits);
+    g_signal_connect(*data, "value_changed", (GtkSignalFunc) xsane_range_display_value_right_callback, GINT_TO_POINTER(digits));
     gtk_object_set_data(GTK_OBJECT(*data), "value-label", value_label); 
     g_signal_emit_by_name(GTK_OBJECT(*data), "value_changed"); /* update value */
     gtk_widget_show(value_label);
@@ -1236,7 +1236,7 @@ void xsane_range_new_with_pixmap(GdkWindow *window, GtkBox *parent, const char *
     gtk_widget_set_size_request(value_label, 35, -1);
     gtk_box_pack_end(GTK_BOX(hbox), value_label, FALSE, FALSE, 1);
     
-    g_signal_connect(*data, "value_changed", (GtkSignalFunc) xsane_range_display_value_right_callback, (void *) digits);
+    g_signal_connect(*data, "value_changed", (GtkSignalFunc) xsane_range_display_value_right_callback, GINT_TO_POINTER(digits));
     gtk_object_set_data(GTK_OBJECT(*data), "value-label", value_label); 
     g_signal_emit_by_name(GTK_OBJECT(*data), "value_changed"); /* update value */
     gtk_widget_show(value_label);
@@ -1383,7 +1383,7 @@ static void xsane_filename_counter_step_callback(GtkWidget *widget, gpointer dat
 {
   DBG(DBG_proc, "xsane_filename_counter_step_callback\n");
  
-  preferences.filename_counter_step = (int) data;
+  preferences.filename_counter_step = GPOINTER_TO_INT(data);
 }
 
 /* ---------------------------------------------------------------------------------------------------------------------- */
@@ -1528,7 +1528,7 @@ void xsane_outputfilename_new(GtkWidget *vbox)
     snprintf(buf, sizeof(buf), "%+d", j);
     xsane_filename_counter_step_item = gtk_menu_item_new_with_label(buf);
     gtk_container_add(GTK_CONTAINER(xsane_filename_counter_step_menu), xsane_filename_counter_step_item);
-    g_signal_connect(GTK_OBJECT(xsane_filename_counter_step_item), "activate", (GtkSignalFunc) xsane_filename_counter_step_callback, (void *) j);
+    g_signal_connect(GTK_OBJECT(xsane_filename_counter_step_item), "activate", (GtkSignalFunc) xsane_filename_counter_step_callback, GINT_TO_POINTER(j));
     gtk_widget_show(xsane_filename_counter_step_item);
     if (preferences.filename_counter_step == j++)
     {
@@ -1895,7 +1895,7 @@ static GtkWidget *eula_dialog = NULL;
 
 static gboolean xsane_eula_delete_event(GtkWidget *widget, GdkEvent *event, gpointer data)
 {
-  eula_accept_flag = (int) data;
+  eula_accept_flag = GPOINTER_TO_INT(data);
 
   DBG(DBG_proc ,"xsane_eula_delete_event(%d)\n", eula_accept_flag);
   eula_dialog = NULL;
@@ -1907,7 +1907,7 @@ static gboolean xsane_eula_delete_event(GtkWidget *widget, GdkEvent *event, gpoi
 
 static void xsane_eula_button_callback(GtkWidget *widget, gpointer data)
 {
-  eula_accept_flag = (int) data;
+  eula_accept_flag = GPOINTER_TO_INT(data);
 
   DBG(DBG_proc ,"xsane_eula_button_callback(%d)\n", eula_accept_flag);
 
@@ -1937,7 +1937,7 @@ int xsane_display_eula(int ask_for_accept)
   gtk_widget_set_size_request(eula_dialog, 550, 580);
   gtk_window_set_position(GTK_WINDOW(eula_dialog), GTK_WIN_POS_CENTER);
   gtk_window_set_resizable(GTK_WINDOW(eula_dialog), TRUE);
-  g_signal_connect(GTK_OBJECT(eula_dialog), "delete_event", GTK_SIGNAL_FUNC(xsane_eula_delete_event), (void *) -1); /* -1 = cancel */
+  g_signal_connect(GTK_OBJECT(eula_dialog), "delete_event", GTK_SIGNAL_FUNC(xsane_eula_delete_event), GINT_TO_POINTER(-1)); /* -1 = cancel */
   snprintf(buf, sizeof(buf), "%s %s", xsane.prog_name, WINDOW_EULA);
   gtk_window_set_title(GTK_WINDOW(eula_dialog), buf);
   accelerator_group = gtk_accel_group_new();
@@ -2090,13 +2090,13 @@ int xsane_display_eula(int ask_for_accept)
     button = gtk_button_new_with_label(BUTTON_NOT_ACCEPT);
     gtk_widget_add_accelerator(button, "clicked", accelerator_group, GDK_Escape, 0, DEF_GTK_ACCEL_LOCKED);
     GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
-    g_signal_connect(GTK_OBJECT(button), "clicked", (GtkSignalFunc) xsane_eula_button_callback, (void *) 1 /* not accept */);
+    g_signal_connect(GTK_OBJECT(button), "clicked", (GtkSignalFunc) xsane_eula_button_callback, GINT_TO_POINTER(1) /* not accept */);
     gtk_container_add(GTK_CONTAINER(hbox), button);
     gtk_widget_grab_default(button);
     gtk_widget_show(button);
 
     button = gtk_button_new_with_label(BUTTON_ACCEPT);
-    g_signal_connect(GTK_OBJECT(button), "clicked", (GtkSignalFunc) xsane_eula_button_callback, (void *) 0 /* accept */);
+    g_signal_connect(GTK_OBJECT(button), "clicked", (GtkSignalFunc) xsane_eula_button_callback, GINT_TO_POINTER(0) /* accept */);
     gtk_container_add(GTK_CONTAINER(hbox), button);
     gtk_widget_show(button);
   }
@@ -2109,7 +2109,7 @@ int xsane_display_eula(int ask_for_accept)
 #endif
     gtk_widget_add_accelerator(button, "clicked", accelerator_group, GDK_Escape, 0, DEF_GTK_ACCEL_LOCKED);
     GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
-    g_signal_connect(GTK_OBJECT(button), "clicked", (GtkSignalFunc) xsane_eula_button_callback, (void *) 0 /* ok = accept */);
+    g_signal_connect(GTK_OBJECT(button), "clicked", (GtkSignalFunc) xsane_eula_button_callback, GINT_TO_POINTER(0) /* ok = accept */);
     gtk_container_add(GTK_CONTAINER(hbox), button);
     gtk_widget_grab_default(button);
     gtk_widget_show(button);
@@ -2336,7 +2336,7 @@ void xsane_display_gpl(void)
 #endif
   gtk_widget_add_accelerator(button, "clicked", accelerator_group, GDK_Escape, 0, DEF_GTK_ACCEL_LOCKED);
   GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
-  g_signal_connect(GTK_OBJECT(button), "clicked", (GtkSignalFunc) xsane_close_license_dialog_callback, (void *) 0 /* ok = accept */);
+  g_signal_connect(GTK_OBJECT(button), "clicked", (GtkSignalFunc) xsane_close_license_dialog_callback, GINT_TO_POINTER(0) /* ok = accept */);
   gtk_container_add(GTK_CONTAINER(hbox), button);
   gtk_widget_grab_default(button);
   gtk_widget_show(button);
@@ -2414,7 +2414,7 @@ static void xsane_front_gtk_getname_button_callback(GtkWidget *widget, gpointer
 {
   DBG(DBG_proc, "xsane_front_gtk_getname_button_callback\n");
                                                                                                                                  
-  xsane_front_gtk_getname_button = (int) data;
+  xsane_front_gtk_getname_button = GPOINTER_TO_INT(data);
 }
                                                                                                                                  
 /* ---------------------------------------------------------------------------------------------------------------------- */
@@ -2438,7 +2438,7 @@ int xsane_front_gtk_getname_dialog(const char *dialog_title, const char *desc_te
   gtk_window_set_resizable(GTK_WINDOW(getname_dialog), FALSE);
   snprintf(buf, sizeof(buf), "%s %s", xsane.prog_name, dialog_title);
   gtk_window_set_title(GTK_WINDOW(getname_dialog), buf);
-  g_signal_connect(GTK_OBJECT(getname_dialog), "delete_event", (GtkSignalFunc) xsane_front_gtk_getname_button_callback, (void *) -1);
+  g_signal_connect(GTK_OBJECT(getname_dialog), "delete_event", (GtkSignalFunc) xsane_front_gtk_getname_button_callback, GINT_TO_POINTER(-1));
   gtk_widget_show(getname_dialog);
                                                                                                                                  
   /* set the main vbox */
@@ -2469,7 +2469,7 @@ int xsane_front_gtk_getname_dialog(const char *dialog_title, const char *desc_te
 #else
   button = gtk_button_new_with_label(BUTTON_CANCEL);
 #endif
-  g_signal_connect(GTK_OBJECT(button), "clicked", (GtkSignalFunc) xsane_front_gtk_getname_button_callback, (void *) -1);
+  g_signal_connect(GTK_OBJECT(button), "clicked", (GtkSignalFunc) xsane_front_gtk_getname_button_callback, GINT_TO_POINTER(-1));
   gtk_widget_add_accelerator(button, "clicked", accelerator_group, GDK_Escape, 0, DEF_GTK_ACCEL_LOCKED); /* ESC */
   gtk_box_pack_start(GTK_BOX(hbox), button, TRUE, TRUE, 0);
   gtk_widget_show(button);
@@ -2479,7 +2479,7 @@ int xsane_front_gtk_getname_dialog(const char *dialog_title, const char *desc_te
 #else
   button = gtk_button_new_with_label(BUTTON_OK);
 #endif
-  g_signal_connect(GTK_OBJECT(button), "clicked", (GtkSignalFunc) xsane_front_gtk_getname_button_callback, (void *) 1);
+  g_signal_connect(GTK_OBJECT(button), "clicked", (GtkSignalFunc) xsane_front_gtk_getname_button_callback, GINT_TO_POINTER(1));
   GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
   gtk_box_pack_start(GTK_BOX(hbox), button, TRUE, TRUE, 0);
   gtk_widget_show(button);
@@ -2580,9 +2580,9 @@ void xsane_front_gtk_email_project_update_lockfile_status()
   {
     fprintf(lockfile, "%s\n", xsane.email_status); /* first line is status of mail */
     fprintf(lockfile, "%3d\n", (int) (xsane.email_progress_val * 100));
-  }
 
-  fclose(lockfile);
+    fclose(lockfile);
+  }
 }
 #endif
 /* ---------------------------------------------------------------------------------------------------------------------- */
diff --git a/src/xsane-multipage-project.c b/src/xsane-multipage-project.c
index f23e5f8..af3c6cc 100644
--- a/src/xsane-multipage-project.c
+++ b/src/xsane-multipage-project.c
@@ -517,6 +517,11 @@ void xsane_multipage_project_save()
   }
 
   projectfile = fopen(filename, "wb"); /* write binary (b for win32) */
+  if (!projectfile)
+  {
+    DBG(DBG_error, "Could not open project file '%s'.\n", filename);
+    return;
+  }
 
   if (xsane.multipage_status)
   {
diff --git a/src/xsane-preview.c b/src/xsane-preview.c
index 6eaf687..ef26098 100644
--- a/src/xsane-preview.c
+++ b/src/xsane-preview.c
@@ -4112,7 +4112,7 @@ static void preview_create_preset_area_menu(Preview *p, int selection)
     gtk_container_add(GTK_CONTAINER(preset_area_menu), preset_area_item);
     g_signal_connect(GTK_OBJECT(preset_area_item), "button_press_event", (GtkSignalFunc) preview_preset_area_context_menu_callback, p);
     g_signal_connect(GTK_OBJECT(preset_area_item), "activate", (GtkSignalFunc) preview_preset_area_callback, p);
-    gtk_object_set_data(GTK_OBJECT(preset_area_item), "Selection", (void *) i);  
+    gtk_object_set_data(GTK_OBJECT(preset_area_item), "Selection", GINT_TO_POINTER(i));
     gtk_object_set_data(GTK_OBJECT(preset_area_item), "Preview", (void *) p);  
 
     gtk_widget_show(preset_area_item);
@@ -4468,7 +4468,7 @@ Preview *preview_new(void)
     rotation_item = gtk_menu_item_new_with_label(buf);
     gtk_container_add(GTK_CONTAINER(rotation_menu), rotation_item);
     g_signal_connect(GTK_OBJECT(rotation_item), "activate", (GtkSignalFunc) preview_rotation_callback, p);
-    gtk_object_set_data(GTK_OBJECT(rotation_item), "Selection", (void *) rot);  
+    gtk_object_set_data(GTK_OBJECT(rotation_item), "Selection", GINT_TO_POINTER(rot));
 
     gtk_widget_show(rotation_item);
   }                  
@@ -5145,11 +5145,11 @@ static void preview_save_image_file(Preview *p, FILE *out)
     /* always save it as a 16 bit PPM image: */
     fprintf(out, "P6\n"
                  "# surface: %g %g %g %g %u %u\n"
-                 "# time: %d\n"
+                 "# time: %ld\n"
                  "%d %d\n65535\n",
                  dsurface[0], dsurface[1], dsurface[2], dsurface[3],
                  p->surface_type, p->surface_unit,
-                 (int) time(NULL),
+                 (long) time(NULL),
                  p->image_width, p->image_height);
 
     fwrite(p->image_data_raw, 6, p->image_width*p->image_height, out);
@@ -5711,14 +5711,14 @@ static gint preview_preset_area_rename_callback(GtkWidget *widget, GtkWidget *pr
 
   DBG(DBG_proc, "preview_preset_area_rename_callback\n");
 
-  selection = (int) gtk_object_get_data(GTK_OBJECT(preset_area_widget), "Selection");
+  selection = GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(preset_area_widget), "Selection"));
   p = (Preview *) gtk_object_get_data(GTK_OBJECT(preset_area_widget), "Preview"); 
 
   DBG(DBG_info ,"rename %s\n", preferences.preset_area[selection]->name);
 
   /* set menu in correct state, is a bit strange this way but I do not have a better idea */
   old_preset_area_menu = gtk_option_menu_get_menu(GTK_OPTION_MENU(p->preset_area_option_menu));
-  old_selection = (int) gtk_object_get_data(GTK_OBJECT(gtk_menu_get_active(GTK_MENU(old_preset_area_menu))), "Selection");  
+  old_selection = GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(gtk_menu_get_active(GTK_MENU(old_preset_area_menu))), "Selection"));
   gtk_menu_popdown(GTK_MENU(old_preset_area_menu));
   gtk_option_menu_set_history(GTK_OPTION_MENU(p->preset_area_option_menu), old_selection);
 
@@ -5761,12 +5761,12 @@ static gint preview_preset_area_add_callback(GtkWidget *widget, GtkWidget *prese
 
   DBG(DBG_proc, "preview_preset_area_add_callback\n");
 
-  selection = (int) gtk_object_get_data(GTK_OBJECT(preset_area_widget), "Selection");
+  selection = GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(preset_area_widget), "Selection"));
   p = (Preview *) gtk_object_get_data(GTK_OBJECT(preset_area_widget), "Preview"); 
 
   /* set menu in correct state, is a bit strange this way but I do not have a better idea */
   old_preset_area_menu = gtk_option_menu_get_menu(GTK_OPTION_MENU(p->preset_area_option_menu));
-  old_selection = (int) gtk_object_get_data(GTK_OBJECT(gtk_menu_get_active(GTK_MENU(old_preset_area_menu))), "Selection");  
+  old_selection = GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(gtk_menu_get_active(GTK_MENU(old_preset_area_menu))), "Selection"));
   gtk_menu_popdown(GTK_MENU(old_preset_area_menu));
   gtk_option_menu_set_history(GTK_OPTION_MENU(p->preset_area_option_menu), old_selection);
 
@@ -5812,7 +5812,7 @@ static gint preview_preset_area_delete_callback(GtkWidget *widget, GtkWidget *pr
 
   DBG(DBG_proc, "preview_preset_area_delete_callback\n");
 
-  selection = (int) gtk_object_get_data(GTK_OBJECT(preset_area_widget), "Selection");
+  selection = GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(preset_area_widget), "Selection"));
   p = (Preview *) gtk_object_get_data(GTK_OBJECT(preset_area_widget), "Preview"); 
 
 
@@ -5832,7 +5832,7 @@ static gint preview_preset_area_delete_callback(GtkWidget *widget, GtkWidget *pr
     old_preset_area_menu = gtk_option_menu_get_menu(GTK_OPTION_MENU(p->preset_area_option_menu));
 
     gtk_option_menu_remove_menu(GTK_OPTION_MENU(p->preset_area_option_menu));
-    old_selection = (int) gtk_object_get_data(GTK_OBJECT(gtk_menu_get_active(GTK_MENU(old_preset_area_menu))), "Selection");  
+    old_selection = GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(gtk_menu_get_active(GTK_MENU(old_preset_area_menu))), "Selection"));
 
     if (old_selection == selection) /* we are deleting the selected surface */
     {
@@ -5861,7 +5861,7 @@ static gint preview_preset_area_move_up_callback(GtkWidget *widget, GtkWidget *p
 
   DBG(DBG_proc, "preview_preset_area_move_up_callback\n");
 
-  selection = (int) gtk_object_get_data(GTK_OBJECT(preset_area_widget), "Selection");
+  selection = GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(preset_area_widget), "Selection"));
   p = (Preview *) gtk_object_get_data(GTK_OBJECT(preset_area_widget), "Preview"); 
 
   if (selection > 1) /* make sure "full area" stays at top */
@@ -5877,7 +5877,7 @@ static gint preview_preset_area_move_up_callback(GtkWidget *widget, GtkWidget *p
     old_preset_area_menu = gtk_option_menu_get_menu(GTK_OPTION_MENU(p->preset_area_option_menu));
 
     gtk_option_menu_remove_menu(GTK_OPTION_MENU(p->preset_area_option_menu));
-    old_selection = (int) gtk_object_get_data(GTK_OBJECT(gtk_menu_get_active(GTK_MENU(old_preset_area_menu))), "Selection");  
+    old_selection = GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(gtk_menu_get_active(GTK_MENU(old_preset_area_menu))), "Selection"));
 
     if (old_selection == selection)
     {
@@ -5906,7 +5906,7 @@ static gint preview_preset_area_move_down_callback(GtkWidget *widget, GtkWidget
 
   DBG(DBG_proc, "preview_preset_area_move_down_callback\n");
 
-  selection = (int) gtk_object_get_data(GTK_OBJECT(preset_area_widget), "Selection");
+  selection = GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(preset_area_widget), "Selection"));
   p = (Preview *) gtk_object_get_data(GTK_OBJECT(preset_area_widget), "Preview"); 
 
   /* full size can not moved down */
@@ -5923,7 +5923,7 @@ static gint preview_preset_area_move_down_callback(GtkWidget *widget, GtkWidget
     old_preset_area_menu = gtk_option_menu_get_menu(GTK_OPTION_MENU(p->preset_area_option_menu));
 
     gtk_option_menu_remove_menu(GTK_OPTION_MENU(p->preset_area_option_menu));
-    old_selection = (int) gtk_object_get_data(GTK_OBJECT(gtk_menu_get_active(GTK_MENU(old_preset_area_menu))), "Selection");  
+    old_selection = GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(gtk_menu_get_active(GTK_MENU(old_preset_area_menu))), "Selection"));
 
     if (old_selection == selection)
     {
@@ -5954,7 +5954,7 @@ static gint preview_preset_area_context_menu_callback(GtkWidget *widget, GdkEven
 
   DBG(DBG_proc, "preview_preset_area_context_menu_callback\n");
 
-  selection = (int) gtk_object_get_data(GTK_OBJECT(widget), "Selection");
+  selection = GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(widget), "Selection"));
 
   if (event->type == GDK_BUTTON_PRESS)
   {
@@ -6031,7 +6031,7 @@ static void preview_preset_area_callback(GtkWidget *widget, gpointer data)
 
   DBG(DBG_proc, "preview_preset_area_callback\n");
 
-  selection = (int) gtk_object_get_data(GTK_OBJECT(widget), "Selection");
+  selection = GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(widget), "Selection"));
 
   p->preset_surface[0] = preferences.preset_area[selection]->xoffset;
   p->preset_surface[1] = preferences.preset_area[selection]->yoffset;
@@ -6055,7 +6055,7 @@ static void preview_rotation_callback(GtkWidget *widget, gpointer data)
 
   DBG(DBG_proc, "preview_rotation_callback\n");
 
-  rot = (int) gtk_object_get_data(GTK_OBJECT(widget), "Selection");
+  rot = GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(widget), "Selection"));
 
   switch (rot)
   {
diff --git a/src/xsane-save.c b/src/xsane-save.c
index 2d0e44b..31825d8 100644
--- a/src/xsane-save.c
+++ b/src/xsane-save.c
@@ -28,6 +28,8 @@
 #include "xsane-save.h"
 #include <time.h>
 #include <sys/wait.h> 
+#include <string.h>
+#include <errno.h>
 
 #include <glib.h>
 
@@ -138,7 +140,7 @@ int xsane_create_secure_file(const char *filename)
   fd = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0666);
   umask(XSANE_DEFAULT_UMASK); /* define new file permissions */   
 
-  if (fd > 0)
+  if (fd >= 0)
   {
     DBG(DBG_info, "file %s is created and secure\n", filename);
     close(fd);
@@ -261,22 +263,27 @@ int xsane_get_filesize(char *filename)
 {
  FILE *infile;
  int pos;
- int size;
+ int size = -1;
 
   infile = fopen(filename, "rb"); /* read binary (b for win32) */
   if (infile == NULL)
   {
-   return 0;
+    return 0;
   }
 
-  pos = ftell(infile);
+  if ((pos = ftell(infile)) < 0)
+  {
+    DBG(DBG_error, "xsane_get_filesize(): can't get file position\n");
+    goto out;
+  }
   fseek(infile, 0, SEEK_END); /* get size */
   size = ftell(infile);
   fseek(infile, pos, SEEK_SET); /* go to previous position */
 
+out:
   fclose(infile);
 
- return size;
+  return size;
 }
 
 /* ---------------------------------------------------------------------------------------------------------------------- */
@@ -959,7 +966,8 @@ cmsHTRANSFORM xsane_create_cms_transform(Image_info *image_info, int cms_functio
 int xsane_save_grayscale_image_as_lineart(FILE *outfile, FILE *imagefile, Image_info *image_info, GtkProgressBar *progress_bar, int *cancel_save)
 {
  int x, y, bit;
- u_char bitval, packed;
+ int bitval;
+ u_char packed;
 
   *cancel_save = 0;
 
@@ -1107,7 +1115,7 @@ int xsane_save_scaled_image(FILE *outfile, FILE *imagefile, Image_info *image_in
   y_factor = 1.0;
   y = 0.0;
 
-  while (y < original_image_height)
+  do
   {
     DBG(DBG_info2, "xsane_save_scaled_image: original line %d, new line %d\n", (int) y, y_new);
 
@@ -1233,6 +1241,7 @@ int xsane_save_scaled_image(FILE *outfile, FILE *imagefile, Image_info *image_in
     y += y_factor;
     read_line = (oldy != (int) y);
   }
+  while (y < original_image_height);
 
   if (read_line) /* we have to write one more line */
   {
@@ -3784,7 +3793,8 @@ int xsane_save_ps(FILE *outfile, FILE *imagefile, Image_info *image_info, float
 static int xsane_embed_pdf_icm_profile(FILE *outfile, struct pdf_xref *xref, char *icm_filename, int flatedecode, int icc_object)
 {
  FILE *icm_profile;
- size_t size, embed_len;
+ ssize_t size;
+ size_t embed_len;
  unsigned char *embed_buffer;
  int ret;
 
@@ -3798,7 +3808,12 @@ static int xsane_embed_pdf_icm_profile(FILE *outfile, struct pdf_xref *xref, cha
   }
 
   fseek(icm_profile, 0, SEEK_END);
-  size = ftell(icm_profile);
+  if ((size = ftell(icm_profile)) < 0)
+  {
+    DBG(DBG_error, "Could not determine size of ICM profile file.");
+    fclose(icm_profile);
+    return -1;
+  }
   fseek(icm_profile, 0, SEEK_SET);
   
   embed_buffer = malloc(size + 1);
@@ -4374,7 +4389,8 @@ static void xsane_jpeg_write_icm_profile(j_compress_ptr cinfo_ptr, const JOCTET
 static void xsane_jpeg_embed_scanner_icm_profile(j_compress_ptr cinfo_ptr, const char *icm_filename)
 {
  FILE *icm_profile;
- size_t size, embed_len;
+ ssize_t size;
+ size_t embed_len;
  cmsUInt8Number *embed_buffer;
 
   DBG(DBG_proc, "xsane_jpeg_embed_scanner_icm_profile(%s)\n", icm_filename);
@@ -4385,15 +4401,19 @@ static void xsane_jpeg_embed_scanner_icm_profile(j_compress_ptr cinfo_ptr, const
     return;
   }
 
-  fseek(icm_profile, 0, SEEK_END);
-  size = ftell(icm_profile);
-  fseek(icm_profile, 0, SEEK_SET);
+  if ((fseek(icm_profile, 0, SEEK_END) < 0) ||
+      ((size = ftell(icm_profile)) < 0) ||
+      (fseek(icm_profile, 0, SEEK_SET) < 0))
+  {
+    DBG(DBG_error, "Can't determine size of ICM profile file '%s': '%s'",
+        icm_filename, strerror(errno));
+    goto out;
+  }
 
   embed_buffer = (cmsUInt8Number *) malloc(size + 1);
   if (embed_buffer)
   {
     embed_len = fread(embed_buffer, 1, size, icm_profile);
-    fclose(icm_profile);
     embed_buffer[embed_len] = 0;
 
     xsane_jpeg_write_icm_profile(cinfo_ptr, embed_buffer, embed_len);
@@ -4401,6 +4421,9 @@ static void xsane_jpeg_embed_scanner_icm_profile(j_compress_ptr cinfo_ptr, const
 
     DBG(DBG_info, "ICM profile %s has been embedded to jpeg file\n", icm_filename);
   }
+
+out:
+  fclose(icm_profile);
 }
 #endif
 
@@ -4606,15 +4629,20 @@ int xsane_save_jpeg(FILE *outfile, int quality, FILE *imagefile, Image_info *ima
 static void xsane_tiff_embed_scanner_icm_profile(TIFF *tiffile, const char *icm_filename)
 {
  FILE *icm_profile;
- size_t size;
+ ssize_t size;
  char *icm_profile_buffer;
 
   DBG(DBG_proc, "xsane_tiff_embed_scanner_icm_profile(%s)\n", icm_filename);
   if((icm_profile = fopen(icm_filename, "rb")))
   {
-    fseek(icm_profile, 0, SEEK_END);
-    size = ftell(icm_profile);
-    fseek(icm_profile, 0, SEEK_SET);
+    if ((fseek(icm_profile, 0, SEEK_END) < 0) ||
+        ((size = ftell(icm_profile)) < 0) ||
+        (fseek(icm_profile, 0, SEEK_SET) < 0))
+    {
+      DBG(DBG_error, "Can't determine size of ICM profile file '%s': '%s'",
+          icm_filename, strerror(errno));
+      goto out;
+    }
 
     icm_profile_buffer = (char *) malloc(size + 1);
 
@@ -4638,7 +4666,7 @@ static void xsane_tiff_embed_scanner_icm_profile(TIFF *tiffile, const char *icm_
       DBG(DBG_error, "Can not get enogh memory for ICM profile\n");
     }
 
-
+out:
     fclose(icm_profile);
   }
   else
@@ -4869,17 +4897,22 @@ int xsane_save_tiff_page(TIFF *tiffile, int page, int pages, int quality, FILE *
 static void xsane_png_embed_scanner_icm_profile(png_structp png_ptr, png_infop png_info_ptr, const char *icm_filename)
 {
  FILE *icm_profile;
- gchar *profile_buffer;
- size_t size;
+ guchar *profile_buffer;
+ ssize_t size;
 
   DBG(DBG_proc, "xsane_png_embed_scanner_icm_profile(%s)\n", icm_filename);
   icm_profile = fopen(icm_filename, "rb");
 
   if (icm_profile)
   {
-    fseek(icm_profile, 0, SEEK_END);
-    size = ftell(icm_profile);
-    fseek(icm_profile, 0, SEEK_SET);
+    if ((fseek(icm_profile, 0, SEEK_END) < 0) ||
+        ((size = ftell(icm_profile)) < 0) ||
+        (fseek(icm_profile, 0, SEEK_SET) < 0))
+    {
+      DBG(DBG_error, "Can't determine size of ICM profile file '%s': '%s'",
+          icm_filename, strerror(errno));
+      goto out;
+    }
 
     profile_buffer = malloc(size);
 
@@ -4901,6 +4934,7 @@ static void xsane_png_embed_scanner_icm_profile(png_structp png_ptr, png_infop p
       DBG(DBG_error, "can not allocate profile_buffer\n");
     }
 
+out:
     fclose(icm_profile);
   }
   else
@@ -5416,7 +5450,7 @@ static int xsane_save_pnm_16_ascii_color(FILE *outfile, FILE *imagefile, Image_i
   *cancel_save = 0;
 
 
-  data = malloc(image_info->image_width * 6);
+  data = malloc((guint32)image_info->image_width * 6);
 
   if (!data)
   {
@@ -5432,7 +5466,7 @@ static int xsane_save_pnm_16_ascii_color(FILE *outfile, FILE *imagefile, Image_i
   {
     DBG(DBG_info, "Doing CMS color conversion\n");
 
-    data_raw = malloc(image_info->image_width * 6);
+    data_raw = malloc((guint32)image_info->image_width * 6);
 
     if (!data_raw)
     {
@@ -5622,7 +5656,7 @@ static int xsane_save_pnm_16_binary_color(FILE *outfile, FILE *imagefile, Image_
 
   *cancel_save = 0;
 
-  data = malloc(image_info->image_width * 6);
+  data = malloc((guint32)image_info->image_width * 6);
 
   if (!data)
   {
@@ -5638,7 +5672,7 @@ static int xsane_save_pnm_16_binary_color(FILE *outfile, FILE *imagefile, Image_
   {
     DBG(DBG_info, "Doing CMS color conversion\n");
 
-    data_raw = malloc(image_info->image_width * 6);
+    data_raw = malloc((guint32)image_info->image_width * 6);
 
     if (!data_raw)
     {
@@ -6159,6 +6193,8 @@ int xsane_save_image_as_text(char *output_filename, char *input_filename, GtkPro
  
     gtk_progress_set_format_string(GTK_PROGRESS(progress_bar), "");
     xsane_progress_bar_set_fraction(GTK_PROGRESS_BAR(progress_bar), 0.0);
+
+    fclose(ocr_progress); /* close reading end of pipe */
   }
   else /* no pipe available */
   {
@@ -6178,11 +6214,6 @@ int xsane_save_image_as_text(char *output_filename, char *input_filename, GtkPro
       }
     }
   }
- 
-  if (pipefd[0])
-  {
-    fclose(ocr_progress); /* close reading end of pipe */
-  }
 
  return (*cancel_save);
 }                                                                                                                                                      
@@ -6784,11 +6815,12 @@ int xsane_transfer_to_gimp(char *input_filename, int apply_ICM_profile, int cms_
  unsigned tile_offset;
  int i, x, y;
  Image_info image_info;
- FILE *imagefile;
+ FILE *imagefile = NULL;
  int bytes;
  unsigned char *data = NULL;
  guint16 *data16 = NULL;
  size_t bytes_read;
+ int retval = 0;
 #ifdef HAVE_LIBLCMS
  unsigned char *data_raw = NULL;
  cmsHTRANSFORM hTransform = NULL;
@@ -6799,13 +6831,13 @@ int xsane_transfer_to_gimp(char *input_filename, int apply_ICM_profile, int cms_
   *cancel_save = 0;
 
   imagefile = fopen(input_filename, "rb"); /* read binary (b for win32) */
-  if (imagefile == 0)
+  if (!imagefile)
   {
    char buf[TEXTBUFSIZE];
     snprintf(buf, sizeof(buf), "%s `%s': %s", ERR_OPEN_FAILED, input_filename, strerror(errno));
     xsane_back_gtk_error(buf, TRUE);     
-
-   return -1;
+    retval = -1;
+    goto out;
   }
 
   xsane_read_pnm_header(imagefile, &image_info);
@@ -6828,7 +6860,8 @@ int xsane_transfer_to_gimp(char *input_filename, int apply_ICM_profile, int cms_
 
     snprintf(buf, sizeof(buf), "%s %s", ERR_DURING_SAVE, ERR_NO_MEM);
     xsane_back_gtk_error(buf, TRUE);
-   return -1; /* error */
+    retval = -1; /* error */
+    goto out;
   }
 
 #ifdef HAVE_LIBLCMS
@@ -6847,11 +6880,10 @@ int xsane_transfer_to_gimp(char *input_filename, int apply_ICM_profile, int cms_
     {
      char buf[TEXTBUFSIZE];
 
-      free(data);
-
       snprintf(buf, sizeof(buf), "%s %s", ERR_DURING_SAVE, ERR_NO_MEM);
       xsane_back_gtk_error(buf, TRUE);
-     return -1; /* error */
+      retval = -1; /* error */
+      goto out;
     }
   }
 #endif
@@ -6892,6 +6924,11 @@ int xsane_transfer_to_gimp(char *input_filename, int apply_ICM_profile, int cms_
     {
       fseek(icm_profile, 0, SEEK_END);
       size = ftell(icm_profile);
+      if (size < 0)
+      {
+        DBG(DBG_error, "xsane_transfer_to_gimp(): can't tell file position");
+        goto out;
+      }
       fseek(icm_profile, 0, SEEK_SET);
 
       profile_buffer = malloc(size);
@@ -6949,7 +6986,7 @@ int xsane_transfer_to_gimp(char *input_filename, int apply_ICM_profile, int cms_
       case 1: /* 1 bit gray => conversion to 8 bit gray */
         for (i = 0; i < ( (image_info.image_width + 7) / 8) * image_info.image_height; ++i)
         {
-         u_char mask;
+         int mask;
          int j;
 
           mask = fgetc(imagefile);
@@ -7219,7 +7256,7 @@ int xsane_transfer_to_gimp(char *input_filename, int apply_ICM_profile, int cms_
   g_free(tile);
   tile = 0;
 
-  fclose(imagefile);
+out:
 
 #ifdef HAVE_LIBLCMS
   if (hTransform != NULL)
@@ -7232,9 +7269,18 @@ int xsane_transfer_to_gimp(char *input_filename, int apply_ICM_profile, int cms_
     free(data_raw);
   }
 #endif
-  free(data);
 
- return 0;
+  if (imagefile)
+  {
+    fclose(imagefile);
+  }
+
+  if (data)
+  {
+    free(data);
+  }
+
+  return retval;
 }
 #endif /* HAVE_ANY_GIMP */ 
 
@@ -7617,7 +7663,7 @@ int pop3_login(int fd_socket, char *user, char *passwd)
  int len;
  ssize_t bytes_written;
 
-  len = read(fd_socket, buf, sizeof(buf));
+  len = read(fd_socket, buf, sizeof(buf) - 1);
   if (len >= 0)
   {
     buf[len] = 0;
@@ -7627,7 +7673,7 @@ int pop3_login(int fd_socket, char *user, char *passwd)
   snprintf(buf, sizeof(buf), "USER %s\r\n", user);
   DBG(DBG_info2, "> USER xxx\n");
   bytes_written = write(fd_socket, buf, strlen(buf));
-  len = read(fd_socket, buf, sizeof(buf));
+  len = read(fd_socket, buf, sizeof(buf) - 1);
   if (len >= 0)
   {
     buf[len] = 0;
@@ -7641,7 +7687,7 @@ int pop3_login(int fd_socket, char *user, char *passwd)
   snprintf(buf, sizeof(buf), "PASS %s\r\n", passwd);
   DBG(DBG_info2, "> PASS xxx\n");
   bytes_written = write(fd_socket, buf, strlen(buf));
-  len = read(fd_socket, buf, sizeof(buf));
+  len = read(fd_socket, buf, sizeof(buf) - 1);
   if (len >= 0)
   {
     buf[len] = 0;
@@ -7655,7 +7701,7 @@ int pop3_login(int fd_socket, char *user, char *passwd)
   snprintf(buf, sizeof(buf), "QUIT\r\n");
   DBG(DBG_info2, "> QUIT\n");
   bytes_written = write(fd_socket, buf, strlen(buf));
-  len = read(fd_socket, buf, sizeof(buf));
+  len = read(fd_socket, buf, sizeof(buf) - 1);
   if (len >= 0)
   {
     buf[len] = 0;
@@ -7683,7 +7729,7 @@ int asmtp_authentication(int fd_socket, int auth_type, char *user, char *passwd)
       bytes_written = write(fd_socket, buf, strlen(buf));
       snprintf(buf, sizeof(buf), "%c%s%c%s", 0, user, 0, passwd);
       write_string_base64(fd_socket, buf, strlen(user)+strlen(passwd)+2);
-      len = read(fd_socket, buf, sizeof(buf));
+      len = read(fd_socket, buf, sizeof(buf) - 1);
       if (len >= 0)
       {
         buf[len] = 0;
@@ -7695,7 +7741,7 @@ int asmtp_authentication(int fd_socket, int auth_type, char *user, char *passwd)
       snprintf(buf, sizeof(buf), "AUTH LOGIN\r\n");
       DBG(DBG_info2, "> %s", buf);
       bytes_written = write(fd_socket, buf, strlen(buf));
-      len = read(fd_socket, buf, sizeof(buf));
+      len = read(fd_socket, buf, sizeof(buf) - 1);
       if (len >= 0)
       {
         buf[len] = 0;
@@ -7710,7 +7756,7 @@ int asmtp_authentication(int fd_socket, int auth_type, char *user, char *passwd)
       DBG(DBG_info2, "> (USERNAME)\n");
       write_string_base64(fd_socket, user, strlen(user));
 
-      len = read(fd_socket, buf, sizeof(buf));
+      len = read(fd_socket, buf, sizeof(buf) - 1);
       if (len >= 0)
       {
         buf[len] = 0;
@@ -7725,7 +7771,7 @@ int asmtp_authentication(int fd_socket, int auth_type, char *user, char *passwd)
       DBG(DBG_info2, "> (PASSWORD)\n");
       write_string_base64(fd_socket, passwd, strlen(passwd));
 
-      len = read(fd_socket, buf, sizeof(buf));
+      len = read(fd_socket, buf, sizeof(buf) - 1);
       if (len >= 0)
       {
         buf[len] = 0;
@@ -7743,7 +7789,7 @@ int asmtp_authentication(int fd_socket, int auth_type, char *user, char *passwd)
       snprintf(buf, sizeof(buf), "AUTH CRAM-MD5\r\n");
       DBG(DBG_info2, "> %s", buf);
       bytes_written = write(fd_socket, buf, strlen(buf));
-      len = read(fd_socket, buf, sizeof(buf));
+      len = read(fd_socket, buf, sizeof(buf) - 1);
       if (len >= 0)
       {
         buf[len] = 0;
@@ -7773,7 +7819,7 @@ int write_smtp_header(int fd_socket, char *from, char *to, int auth_type, char *
  char *pos = NULL;
  ssize_t bytes_written;
 
-  len = read(fd_socket, buf, sizeof(buf));
+  len = read(fd_socket, buf, sizeof(buf) - 1);
   if (len >= 0)
   {
     buf[len] = 0;
@@ -7790,7 +7836,7 @@ int write_smtp_header(int fd_socket, char *from, char *to, int auth_type, char *
   }
   DBG(DBG_info2, "> %s", buf);
   bytes_written = write(fd_socket, buf, strlen(buf));
-  len = read(fd_socket, buf, sizeof(buf));
+  len = read(fd_socket, buf, sizeof(buf) - 1);
   if (len >= 0)
   {
     buf[len] = 0;
@@ -7824,7 +7870,7 @@ int write_smtp_header(int fd_socket, char *from, char *to, int auth_type, char *
   snprintf(buf, sizeof(buf), "MAIL FROM: <%s>\r\n", from);
   DBG(DBG_info2, "> %s", buf);
   bytes_written = write(fd_socket, buf, strlen(buf));
-  len = read(fd_socket, buf, sizeof(buf));
+  len = read(fd_socket, buf, sizeof(buf) - 1);
   if (len >= 0)
   {
     buf[len] = 0;
@@ -7864,7 +7910,7 @@ int write_smtp_header(int fd_socket, char *from, char *to, int auth_type, char *
 
     DBG(DBG_info2, "> %s", buf);
     bytes_written = write(fd_socket, buf, strlen(buf));
-    len = read(fd_socket, buf, sizeof(buf));
+    len = read(fd_socket, buf, sizeof(buf) - 1);
     if (len >= 0)
     {
       buf[len] = 0;
@@ -7897,7 +7943,7 @@ int write_smtp_header(int fd_socket, char *from, char *to, int auth_type, char *
   snprintf(buf, sizeof(buf), "DATA\r\n");
   DBG(DBG_info2, "> %s", buf);
   bytes_written = write(fd_socket, buf, strlen(buf));
-  len = read(fd_socket, buf, sizeof(buf));
+  len = read(fd_socket, buf, sizeof(buf) - 1);
   if (len >= 0)
   {
     buf[len] = 0;
@@ -7932,7 +7978,7 @@ int write_smtp_footer(int fd_socket)
   snprintf(buf, sizeof(buf), "\r\n.\r\n");
   DBG(DBG_info2, "> %s", buf);
   bytes_written = write(fd_socket, buf, strlen(buf));
-  len = read(fd_socket, buf, sizeof(buf));
+  len = read(fd_socket, buf, sizeof(buf) - 1);
   if (len >= 0)
   {
     buf[len] = 0;
diff --git a/src/xsane-scan.c b/src/xsane-scan.c
index 88f954a..b6a66d6 100644
--- a/src/xsane-scan.c
+++ b/src/xsane-scan.c
@@ -394,6 +394,13 @@ static void xsane_read_image_data(gpointer data, gint source, GdkInputCondition
               {
                long fpos = ftell(xsane.out);
 
+                if (fpos < 0)
+                {
+                  DBG(DBG_error, "xsane_read_image_data(): can't tell file "
+                                 "position");
+                  return;
+                }
+
                 fseek(xsane.out, 0, SEEK_CUR); /* sync between write and read */
                 bytes_read = fread(rgbbuf, 1, bytes - 1, xsane.out);
                 fseek(xsane.out, fpos, SEEK_SET);
@@ -1173,6 +1180,8 @@ void xsane_scan_done(SANE_Status status)
             {
               abort = 1;
             }
+
+            fclose(outfile);
           }
           else
           {
@@ -1182,8 +1191,6 @@ void xsane_scan_done(SANE_Status status)
             xsane_back_gtk_error(buf, TRUE);
             abort = 1;
           }
-
-          fclose(outfile);
         }
 
         fclose(infile);
@@ -1452,7 +1459,7 @@ void xsane_scan_done(SANE_Status status)
       }
       else
       {
-        type = "";
+        type = strdup("");
       }
 
       list_item = gtk_list_item_new_with_label(page);
@@ -1493,7 +1500,7 @@ void xsane_scan_done(SANE_Status status)
       }
       else
       {
-        type = "";
+        type = strdup("");
       }
 
       list_item = gtk_list_item_new_with_label(page);
@@ -1535,7 +1542,7 @@ void xsane_scan_done(SANE_Status status)
       }
       else
       {
-        type = "";
+        type = strdup("");
       }
 
       list_item = gtk_list_item_new_with_label(page);
diff --git a/src/xsane-setup.c b/src/xsane-setup.c
index a61833a..413a343 100644
--- a/src/xsane-setup.c
+++ b/src/xsane-setup.c
@@ -111,7 +111,7 @@ void xsane_new_printer(void)
 
   DBG(DBG_proc, "xsane_new_printer\n");
 
-  newprinters = realloc(preferences.printer, (preferences.printerdefinitions+1) * sizeof(void *));
+  newprinters = realloc(preferences.printer, (preferences.printerdefinitions+1) * sizeof(Preferences_printer_t *));
 
   if (newprinters) /* realloc returns NULL if failed, in this case the old memory keeps alive */
   {
@@ -309,7 +309,7 @@ static void xsane_setup_printer_callback(GtkWidget *widget, gpointer data)
 {
   DBG(DBG_proc, "xsane_setup_printer_callback\n");
 
-  preferences.printernr = (int) data;
+  preferences.printernr = GPOINTER_TO_INT(data);
   xsane_setup_printer_update();
 }
 
@@ -328,7 +328,7 @@ static void xsane_setup_printer_menu_build(GtkWidget *option_menu)
   {
     printer_item = gtk_menu_item_new_with_label(preferences.printer[i]->name);
     gtk_container_add(GTK_CONTAINER(printer_menu), printer_item);
-    g_signal_connect(GTK_OBJECT(printer_item), "activate", (GtkSignalFunc) xsane_setup_printer_callback, (void *) i);
+    g_signal_connect(GTK_OBJECT(printer_item), "activate", (GtkSignalFunc) xsane_setup_printer_callback, GINT_TO_POINTER(i));
     gtk_widget_show(printer_item);
   }
 
@@ -454,7 +454,7 @@ static void xsane_setup_filename_counter_len_callback(GtkWidget *widget, gpointe
 {
   DBG(DBG_proc, "xsane_setup_filename_counter_len_callback\n");
 
-  xsane_setup.filename_counter_len = (int) data;
+  xsane_setup.filename_counter_len = GPOINTER_TO_INT(data);
 }
 
 /* ---------------------------------------------------------------------------------------------------------------------- */
@@ -464,7 +464,7 @@ static void xsane_setup_tiff_compression16_callback(GtkWidget *widget, gpointer
 {
   DBG(DBG_proc, "xsane_setup_tiff_compression16_callback\n");
 
-  xsane_setup.tiff_compression16_nr = (int) data;
+  xsane_setup.tiff_compression16_nr = GPOINTER_TO_INT(data);
 }
 
 /* -------------------------------------- */
@@ -473,7 +473,7 @@ static void xsane_setup_tiff_compression8_callback(GtkWidget *widget, gpointer d
 {
   DBG(DBG_proc, "xsane_setup_tiff_compression8_callback\n");
 
-  xsane_setup.tiff_compression8_nr = (int) data;
+  xsane_setup.tiff_compression8_nr = GPOINTER_TO_INT(data);
 }
 
 /* -------------------------------------- */
@@ -481,7 +481,7 @@ static void xsane_setup_tiff_compression8_callback(GtkWidget *widget, gpointer d
 static void xsane_setup_tiff_compression1_callback(GtkWidget *widget, gpointer data)
 {
   DBG(DBG_proc, "xsane_setup_tiff_compression1_callback\n");
-  xsane_setup.tiff_compression1_nr = (int) data;
+  xsane_setup.tiff_compression1_nr = GPOINTER_TO_INT(data);
 }
 #endif
 
@@ -569,7 +569,7 @@ static void xsane_setup_color_management_apply_changes(GtkWidget *widget, gpoint
 {
   DBG(DBG_proc, "xsane_setup_colormagaement_apply_changes\n");
 
-  preferences.cms_intent = (int) gtk_object_get_data(GTK_OBJECT(gtk_menu_get_active(GTK_MENU(gtk_option_menu_get_menu(GTK_OPTION_MENU(xsane_setup.cms_intent_option_menu))))), "Selection");
+  preferences.cms_intent = GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(gtk_menu_get_active(GTK_MENU(gtk_option_menu_get_menu(GTK_OPTION_MENU(xsane_setup.cms_intent_option_menu))))), "Selection"));
   xsane_update_bool(xsane_setup.cms_bpc_button, &preferences.cms_bpc);
 
   if (xsane.scanner_default_color_icm_profile)
@@ -843,7 +843,7 @@ void xsane_close_setup_dialog_callback(GtkWidget *widget, gpointer data)
 
 static void xsane_permission_toggled(GtkWidget *widget, gpointer data)
 {
- int mask = (int) data;
+ int mask = GPOINTER_TO_INT(data);
  int *permission = 0;
  const gchar *name = gtk_widget_get_name(widget);
 
@@ -912,7 +912,7 @@ static void xsane_permission_box(GtkWidget *parent, gchar *name, gchar *descript
   xsane_back_gtk_set_tooltip(xsane.tooltips, button, DESC_PERMISSION_READ);
   gtk_widget_set_size_request(button, 26, -1);
   gtk_widget_set_name(button, name);
-  g_signal_connect(GTK_OBJECT(button), "toggled", (GtkSignalFunc) xsane_permission_toggled, (void *) 256);
+  g_signal_connect(GTK_OBJECT(button), "toggled", (GtkSignalFunc) xsane_permission_toggled, GINT_TO_POINTER(256));
   gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, FALSE, 1);
   gtk_widget_show(button);
   gtk_widget_set_sensitive(button, user_sensitivity);
@@ -922,7 +922,7 @@ static void xsane_permission_box(GtkWidget *parent, gchar *name, gchar *descript
   xsane_back_gtk_set_tooltip(xsane.tooltips, button, DESC_PERMISSION_WRITE);
   gtk_widget_set_size_request(button, 26, -1);
   gtk_widget_set_name(button, name);
-  g_signal_connect(GTK_OBJECT(button), "toggled", (GtkSignalFunc) xsane_permission_toggled, (void *) 128);
+  g_signal_connect(GTK_OBJECT(button), "toggled", (GtkSignalFunc) xsane_permission_toggled, GINT_TO_POINTER(128));
   gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, FALSE, 1);
   gtk_widget_show(button);
   gtk_widget_set_sensitive(button, user_sensitivity);
@@ -932,7 +932,7 @@ static void xsane_permission_box(GtkWidget *parent, gchar *name, gchar *descript
   xsane_back_gtk_set_tooltip(xsane.tooltips, button, DESC_PERMISSION_SEARCH);
   gtk_widget_set_size_request(button, 26, -1);
   gtk_widget_set_name(button, name);
-  g_signal_connect(GTK_OBJECT(button), "toggled", (GtkSignalFunc) xsane_permission_toggled, (void *) 64);
+  g_signal_connect(GTK_OBJECT(button), "toggled", (GtkSignalFunc) xsane_permission_toggled, GINT_TO_POINTER(64));
   gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, FALSE, 1);
   gtk_widget_show(button);
   gtk_widget_set_sensitive(button, x_sensitivity & user_sensitivity);
@@ -950,7 +950,7 @@ static void xsane_permission_box(GtkWidget *parent, gchar *name, gchar *descript
   xsane_back_gtk_set_tooltip(xsane.tooltips, button, DESC_PERMISSION_READ);
   gtk_widget_set_size_request(button, 26, -1);
   gtk_widget_set_name(button, name);
-  g_signal_connect(GTK_OBJECT(button), "toggled", (GtkSignalFunc) xsane_permission_toggled, (void *) 32);
+  g_signal_connect(GTK_OBJECT(button), "toggled", (GtkSignalFunc) xsane_permission_toggled, GINT_TO_POINTER(32));
   gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, FALSE, 1);
   gtk_widget_show(button);
 
@@ -959,7 +959,7 @@ static void xsane_permission_box(GtkWidget *parent, gchar *name, gchar *descript
   xsane_back_gtk_set_tooltip(xsane.tooltips, button, DESC_PERMISSION_WRITE);
   gtk_widget_set_size_request(button, 26, -1);
   gtk_widget_set_name(button, name);
-  g_signal_connect(GTK_OBJECT(button), "toggled", (GtkSignalFunc) xsane_permission_toggled, (void *) 16);
+  g_signal_connect(GTK_OBJECT(button), "toggled", (GtkSignalFunc) xsane_permission_toggled, GINT_TO_POINTER(16));
   gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, FALSE, 1);
   gtk_widget_show(button);
 
@@ -968,7 +968,7 @@ static void xsane_permission_box(GtkWidget *parent, gchar *name, gchar *descript
   xsane_back_gtk_set_tooltip(xsane.tooltips, button, DESC_PERMISSION_SEARCH);
   gtk_widget_set_size_request(button, 26, -1);
   gtk_widget_set_name(button, name);
-  g_signal_connect(GTK_OBJECT(button), "toggled", (GtkSignalFunc) xsane_permission_toggled, (void *) 8);
+  g_signal_connect(GTK_OBJECT(button), "toggled", (GtkSignalFunc) xsane_permission_toggled, GINT_TO_POINTER(8));
   gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, FALSE, 1);
   gtk_widget_show(button);
   gtk_widget_set_sensitive(button, x_sensitivity);
@@ -986,7 +986,7 @@ static void xsane_permission_box(GtkWidget *parent, gchar *name, gchar *descript
   xsane_back_gtk_set_tooltip(xsane.tooltips, button, DESC_PERMISSION_READ);
   gtk_widget_set_size_request(button, 26, -1);
   gtk_widget_set_name(button, name);
-  g_signal_connect(GTK_OBJECT(button), "toggled", (GtkSignalFunc) xsane_permission_toggled, (void *) 4);
+  g_signal_connect(GTK_OBJECT(button), "toggled", (GtkSignalFunc) xsane_permission_toggled, GINT_TO_POINTER(4));
   gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, FALSE, 1);
   gtk_widget_show(button);
 
@@ -995,7 +995,7 @@ static void xsane_permission_box(GtkWidget *parent, gchar *name, gchar *descript
   xsane_back_gtk_set_tooltip(xsane.tooltips, button, DESC_PERMISSION_WRITE);
   gtk_widget_set_size_request(button, 26, -1);
   gtk_widget_set_name(button, name);
-  g_signal_connect(GTK_OBJECT(button), "toggled", (GtkSignalFunc) xsane_permission_toggled, (void *) 2);
+  g_signal_connect(GTK_OBJECT(button), "toggled", (GtkSignalFunc) xsane_permission_toggled, GINT_TO_POINTER(2));
   gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, FALSE, 1);
   gtk_widget_show(button);
 
@@ -1004,7 +1004,7 @@ static void xsane_permission_box(GtkWidget *parent, gchar *name, gchar *descript
   xsane_back_gtk_set_tooltip(xsane.tooltips, button, DESC_PERMISSION_SEARCH);
   gtk_widget_set_size_request(button, 26, -1);
   gtk_widget_set_name(button, name);
-  g_signal_connect(GTK_OBJECT(button), "toggled", (GtkSignalFunc) xsane_permission_toggled, (void *) 1);
+  g_signal_connect(GTK_OBJECT(button), "toggled", (GtkSignalFunc) xsane_permission_toggled, GINT_TO_POINTER(1));
   gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, FALSE, 1);
   gtk_widget_show(button);
   gtk_widget_set_sensitive(button, x_sensitivity);
@@ -1750,7 +1750,7 @@ static void xsane_saving_notebook(GtkWidget *notebook)
     }
     filename_counter_len_item = gtk_menu_item_new_with_label(buf);
     gtk_container_add(GTK_CONTAINER(filename_counter_len_menu), filename_counter_len_item);
-    g_signal_connect(GTK_OBJECT(filename_counter_len_item), "activate", (GtkSignalFunc) xsane_setup_filename_counter_len_callback, (void *) i);
+    g_signal_connect(GTK_OBJECT(filename_counter_len_item), "activate", (GtkSignalFunc) xsane_setup_filename_counter_len_callback, GINT_TO_POINTER(i));
     gtk_widget_show(filename_counter_len_item);
     if (preferences.filename_counter_len == i)
     {
@@ -1983,7 +1983,7 @@ static void xsane_filetype_notebook(GtkWidget *notebook)
   {
     tiff_compression_item = gtk_menu_item_new_with_label(tiff_compression16_strings[i-1].name);
     gtk_container_add(GTK_CONTAINER(tiff_compression_menu), tiff_compression_item);
-    g_signal_connect(GTK_OBJECT(tiff_compression_item), "activate", (GtkSignalFunc) xsane_setup_tiff_compression16_callback, (void *) tiff_compression16_strings[i-1].number);
+    g_signal_connect(GTK_OBJECT(tiff_compression_item), "activate", (GtkSignalFunc) xsane_setup_tiff_compression16_callback, GINT_TO_POINTER(tiff_compression16_strings[i-1].number));
     gtk_widget_show(tiff_compression_item);
     if (tiff_compression16_strings[i-1].number == preferences.tiff_compression16_nr)
     {
@@ -2019,7 +2019,7 @@ static void xsane_filetype_notebook(GtkWidget *notebook)
   {
     tiff_compression_item = gtk_menu_item_new_with_label(tiff_compression8_strings[i-1].name);
     gtk_container_add(GTK_CONTAINER(tiff_compression_menu), tiff_compression_item);
-    g_signal_connect(GTK_OBJECT(tiff_compression_item), "activate", (GtkSignalFunc) xsane_setup_tiff_compression8_callback, (void *) tiff_compression8_strings[i-1].number);
+    g_signal_connect(GTK_OBJECT(tiff_compression_item), "activate", (GtkSignalFunc) xsane_setup_tiff_compression8_callback, GINT_TO_POINTER(tiff_compression8_strings[i-1].number));
     gtk_widget_show(tiff_compression_item);
     if (tiff_compression8_strings[i-1].number == preferences.tiff_compression8_nr)
     {
@@ -2056,7 +2056,7 @@ static void xsane_filetype_notebook(GtkWidget *notebook)
   {
     tiff_compression_item = gtk_menu_item_new_with_label(tiff_compression1_strings[i-1].name);
     gtk_container_add(GTK_CONTAINER(tiff_compression_menu), tiff_compression_item);
-    g_signal_connect(GTK_OBJECT(tiff_compression_item), "activate", (GtkSignalFunc) xsane_setup_tiff_compression1_callback, (void *) tiff_compression1_strings[i-1].number);
+    g_signal_connect(GTK_OBJECT(tiff_compression_item), "activate", (GtkSignalFunc) xsane_setup_tiff_compression1_callback, GINT_TO_POINTER(tiff_compression1_strings[i-1].number));
     gtk_widget_show(tiff_compression_item);
     if (tiff_compression1_strings[i-1].number == preferences.tiff_compression1_nr)
     {
@@ -2251,7 +2251,7 @@ static void xsane_fax_notebook(GtkWidget *notebook)
   for (i=0; i < sizeof(fax_program)/sizeof(fax_program_options_type); i++)
   {
     button = gtk_button_new_with_label(fax_program[i].identifier);
-    g_signal_connect(GTK_OBJECT(button), "clicked", (GtkSignalFunc) xsane_fax_notebook_set_faxprogram_default_callback, (void *) i);
+    g_signal_connect(GTK_OBJECT(button), "clicked", (GtkSignalFunc) xsane_fax_notebook_set_faxprogram_default_callback, GINT_TO_POINTER(i));
     gtk_box_pack_end(GTK_BOX(hbox), button, FALSE, FALSE, 10);
     gtk_widget_show(button);
   }
@@ -2557,7 +2557,7 @@ static void xsane_email_notebook(GtkWidget *notebook)
   {
     authentication_menu_item = gtk_menu_item_new_with_label(authentication_strings[i-1].name);
     gtk_container_add(GTK_CONTAINER(authentication_menu), authentication_menu_item);
-    g_signal_connect(GTK_OBJECT(authentication_menu_item), "activate", (GtkSignalFunc) xsane_setup_authentication_type_callback, (void *) authentication_strings[i-1].number);
+    g_signal_connect(GTK_OBJECT(authentication_menu_item), "activate", (GtkSignalFunc) xsane_setup_authentication_type_callback, GINT_TO_POINTER(authentication_strings[i-1].number));
     gtk_widget_show(authentication_menu_item);
     if (authentication_strings[i-1].number == preferences.email_authentication)
     {
@@ -2926,7 +2926,7 @@ static void xsane_display_notebook(GtkWidget *notebook)
   {
     show_range_mode_item = gtk_menu_item_new_with_label(show_range_mode_strings[i-1].name);
     gtk_container_add(GTK_CONTAINER(show_range_mode_menu), show_range_mode_item);
-    g_signal_connect(GTK_OBJECT(show_range_mode_item), "activate", (GtkSignalFunc) xsane_setup_show_range_mode_callback, (void *) show_range_mode_strings[i-1].number);
+    g_signal_connect(GTK_OBJECT(show_range_mode_item), "activate", (GtkSignalFunc) xsane_setup_show_range_mode_callback, GINT_TO_POINTER(show_range_mode_strings[i-1].number));
     gtk_widget_show(show_range_mode_item);
     if (show_range_mode_strings[i-1].number == preferences.show_range_mode)
     {
@@ -3129,7 +3129,7 @@ static void xsane_enhance_notebook_sensitivity(int lineart_mode)
 
 static void xsane_setup_authentication_type_callback(GtkWidget *widget, gpointer data)
 {
-  xsane_setup.email_authentication = (int) data;
+  xsane_setup.email_authentication = GPOINTER_TO_INT(data);
 
   gtk_widget_set_sensitive(xsane_setup.pop3_vbox, (xsane_setup.email_authentication == EMAIL_AUTH_POP3));
 }
@@ -3138,14 +3138,14 @@ static void xsane_setup_authentication_type_callback(GtkWidget *widget, gpointer
 
 static void xsane_setup_show_range_mode_callback(GtkWidget *widget, gpointer data)
 {
-  xsane_setup.show_range_mode = (int) data;
+  xsane_setup.show_range_mode = GPOINTER_TO_INT(data);
 }
 
 /* ---------------------------------------------------------------------------------------------------------------------- */
 
 static void xsane_setup_lineart_mode_callback(GtkWidget *widget, gpointer data)
 {
-  xsane_setup.lineart_mode = (int) data;
+  xsane_setup.lineart_mode = GPOINTER_TO_INT(data);
   xsane_enhance_notebook_sensitivity(xsane_setup.lineart_mode);
 }
 
@@ -3168,7 +3168,7 @@ static void xsane_setup_preview_pipette_range_callback(GtkWidget *widget, gpoint
 {
   DBG(DBG_proc, "xsane_setup_preview_pipette_range_callback\n");
 
-  xsane_setup.preview_pipette_range = (int) data;
+  xsane_setup.preview_pipette_range = GPOINTER_TO_INT(data);
 }
 
 /* ---------------------------------------------------------------------------------------------------------------------- */
@@ -3237,7 +3237,7 @@ static void xsane_enhance_notebook(GtkWidget *notebook)
   {
     lineart_mode_item = gtk_menu_item_new_with_label(lineart_mode_strings[i-1].name);
     gtk_container_add(GTK_CONTAINER(lineart_mode_menu), lineart_mode_item);
-    g_signal_connect(GTK_OBJECT(lineart_mode_item), "activate", (GtkSignalFunc) xsane_setup_lineart_mode_callback, (void *) lineart_mode_strings[i-1].number);
+    g_signal_connect(GTK_OBJECT(lineart_mode_item), "activate", (GtkSignalFunc) xsane_setup_lineart_mode_callback, GINT_TO_POINTER(lineart_mode_strings[i-1].number));
     gtk_widget_show(lineart_mode_item);
     if (lineart_mode_strings[i-1].number == xsane.lineart_mode)
     {
@@ -3461,7 +3461,7 @@ static void xsane_enhance_notebook(GtkWidget *notebook)
     snprintf(buf, sizeof(buf), "%d x %d pixel", j, j);
     preview_pipette_range_item = gtk_menu_item_new_with_label(buf);
     gtk_container_add(GTK_CONTAINER(preview_pipette_range_menu), preview_pipette_range_item);
-    g_signal_connect(GTK_OBJECT(preview_pipette_range_item), "activate", (GtkSignalFunc) xsane_setup_preview_pipette_range_callback, (void *) j);
+    g_signal_connect(GTK_OBJECT(preview_pipette_range_item), "activate", (GtkSignalFunc) xsane_setup_preview_pipette_range_callback, GINT_TO_POINTER(j));
     gtk_widget_show(preview_pipette_range_item);
     if (preferences.preview_pipette_range == j)
     {
@@ -3555,22 +3555,22 @@ static void xsane_color_management_notebook(GtkWidget *notebook)
   menu = gtk_menu_new();
 
   menu_item = gtk_menu_item_new_with_label(SUBMENU_ITEM_CMS_INTENT_PERCEPTUAL);
-  gtk_object_set_data(GTK_OBJECT(menu_item), "Selection", (void *) INTENT_PERCEPTUAL);
+  gtk_object_set_data(GTK_OBJECT(menu_item), "Selection", GINT_TO_POINTER(INTENT_PERCEPTUAL));
   gtk_container_add(GTK_CONTAINER(menu), menu_item);
   gtk_widget_show(menu_item);
 
   menu_item = gtk_menu_item_new_with_label(SUBMENU_ITEM_CMS_INTENT_RELATIVE_COLORIMETRIC);
-  gtk_object_set_data(GTK_OBJECT(menu_item), "Selection", (void *) INTENT_RELATIVE_COLORIMETRIC);
+  gtk_object_set_data(GTK_OBJECT(menu_item), "Selection", GINT_TO_POINTER(INTENT_RELATIVE_COLORIMETRIC));
   gtk_container_add(GTK_CONTAINER(menu), menu_item);
   gtk_widget_show(menu_item);
 
   menu_item = gtk_menu_item_new_with_label(SUBMENU_ITEM_CMS_INTENT_ABSOLUTE_COLORIMETRIC);
-  gtk_object_set_data(GTK_OBJECT(menu_item), "Selection", (void *) INTENT_ABSOLUTE_COLORIMETRIC);
+  gtk_object_set_data(GTK_OBJECT(menu_item), "Selection", GINT_TO_POINTER(INTENT_ABSOLUTE_COLORIMETRIC));
   gtk_container_add(GTK_CONTAINER(menu), menu_item);
   gtk_widget_show(menu_item);
 
   menu_item = gtk_menu_item_new_with_label(SUBMENU_ITEM_CMS_INTENT_SATURATION);
-  gtk_object_set_data(GTK_OBJECT(menu_item), "Selection", (void *) INTENT_SATURATION);
+  gtk_object_set_data(GTK_OBJECT(menu_item), "Selection", GINT_TO_POINTER(INTENT_SATURATION));
   gtk_container_add(GTK_CONTAINER(menu), menu_item);
   gtk_widget_show(menu_item);
 
diff --git a/src/xsane-text.h b/src/xsane-text.h
index ee4a222..44659b9 100644
--- a/src/xsane-text.h
+++ b/src/xsane-text.h
@@ -803,6 +803,7 @@ YOU ARE ALONE!\
 #define ERR_HEADER_CHILD_PROCESS_ERROR	_("Child process error")
 
 #define ERR_FAILED_CREATE_FILE		_("Failed to create file:")
+#define ERR_FAILED_CREATE_ENSURE_DIR	_("Failed to ensure path is a directory, or to create as one:")
 #define ERR_LOAD_DEVICE_SETTINGS	_("Error while loading device settings:")
 #define ERR_NO_DRC_FILE			_("is not a device-rc-file !!!")
 #define ERR_NETSCAPE_EXECUTE_FAIL	_("Failed to execute netscape!")
diff --git a/src/xsane-viewer.c b/src/xsane-viewer.c
index 844c077..6786e96 100644
--- a/src/xsane-viewer.c
+++ b/src/xsane-viewer.c
@@ -32,6 +32,8 @@
 #include "xsane-save.h"
 #include <gdk/gdkkeysyms.h>
 #include <sys/wait.h>
+#include <string.h>
+#include <errno.h>
 
 #ifndef PATH_MAX
 # define PATH_MAX       1024
@@ -506,8 +508,8 @@ static void xsane_viewer_scale_set_scale_value_and_adjustments(GtkAdjustment *ad
 
   *scale_val = adj_data->value;
 
-  image_width  = (int) gtk_object_get_data(GTK_OBJECT(adj_data), "image_width");
-  image_height = (int) gtk_object_get_data(GTK_OBJECT(adj_data), "image_height");
+  image_width  = GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(adj_data), "image_width"));
+  image_height = GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(adj_data), "image_height"));
 
   adj = (GtkAdjustment*) gtk_object_get_data(GTK_OBJECT(adj_data), "size-x-adjustment");
   if ((adj) && (image_width))
@@ -529,8 +531,8 @@ static void xsane_viewer_scale_set_size_x_value_and_adjustments(GtkAdjustment *a
  GtkAdjustment *adj;
  int image_width, image_height;
 
-  image_width  = (int) gtk_object_get_data(GTK_OBJECT(adj_data), "image_width");
-  image_height = (int) gtk_object_get_data(GTK_OBJECT(adj_data), "image_height");
+  image_width  = GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(adj_data), "image_width"));
+  image_height = GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(adj_data), "image_height"));
 
   if (!image_width)
   {
@@ -553,8 +555,8 @@ static void xsane_viewer_scale_set_size_y_value_and_adjustments(GtkAdjustment *a
  GtkAdjustment *adj;
  int image_width, image_height;
 
-  image_width  = (int) gtk_object_get_data(GTK_OBJECT(adj_data), "image_width");
-  image_height = (int) gtk_object_get_data(GTK_OBJECT(adj_data), "image_height");
+  image_width  = GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(adj_data), "image_width"));
+  image_height = GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(adj_data), "image_height"));
 
   if (!image_height)
   {
@@ -663,7 +665,7 @@ static void xsane_viewer_scale_callback(GtkWidget *window, gpointer data)
   button = gtk_check_button_new_with_label(BUTTON_SCALE_BIND);
   gtk_box_pack_start(GTK_BOX(vbox), button, FALSE, FALSE, 5);
   gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), v->bind_scale);
-  g_signal_connect(GTK_OBJECT(button), "toggled", (GtkSignalFunc) xsane_viewer_button_changed, (void *) &v->bind_scale);
+  g_signal_connect(GTK_OBJECT(button), "toggled", (GtkSignalFunc) xsane_viewer_button_changed, GINT_TO_POINTER(&v->bind_scale));
   g_signal_connect_after(GTK_OBJECT(button), "toggled", (GtkSignalFunc) xsane_viewer_scale_callback, (void *) v);
   gtk_widget_show(button);
 
@@ -715,20 +717,20 @@ static void xsane_viewer_scale_callback(GtkWidget *window, gpointer data)
     gtk_widget_show(spinbutton);
     xsane_back_gtk_set_tooltip(xsane.tooltips, spinbutton, DESC_SCALE_HEIGHT); 
 
-    gtk_object_set_data(GTK_OBJECT(scale_widget), "size-x-adjustment", (void *) adjustment_size_x);
-    gtk_object_set_data(GTK_OBJECT(scale_widget), "size-y-adjustment", (void *) adjustment_size_y);
-    gtk_object_set_data(GTK_OBJECT(scale_widget), "image_width",       (void *) image_info.image_width);
-    gtk_object_set_data(GTK_OBJECT(scale_widget), "image_height",      (void *) image_info.image_height);
+    gtk_object_set_data(GTK_OBJECT(scale_widget), "size-x-adjustment", GINT_TO_POINTER(adjustment_size_x));
+    gtk_object_set_data(GTK_OBJECT(scale_widget), "size-y-adjustment", GINT_TO_POINTER(adjustment_size_y));
+    gtk_object_set_data(GTK_OBJECT(scale_widget), "image_width",       GINT_TO_POINTER(image_info.image_width));
+    gtk_object_set_data(GTK_OBJECT(scale_widget), "image_height",      GINT_TO_POINTER(image_info.image_height));
 
-    gtk_object_set_data(GTK_OBJECT(adjustment_size_x), "scale-adjustment",   (void *) scale_widget);
-    gtk_object_set_data(GTK_OBJECT(adjustment_size_x), "size-y-adjustment",  (void *) adjustment_size_y);
-    gtk_object_set_data(GTK_OBJECT(adjustment_size_x), "image_width",        (void *) image_info.image_width);
-    gtk_object_set_data(GTK_OBJECT(adjustment_size_x), "image_height",       (void *) image_info.image_height);
+    gtk_object_set_data(GTK_OBJECT(adjustment_size_x), "scale-adjustment",   GINT_TO_POINTER(scale_widget));
+    gtk_object_set_data(GTK_OBJECT(adjustment_size_x), "size-y-adjustment",  GINT_TO_POINTER(adjustment_size_y));
+    gtk_object_set_data(GTK_OBJECT(adjustment_size_x), "image_width",        GINT_TO_POINTER(image_info.image_width));
+    gtk_object_set_data(GTK_OBJECT(adjustment_size_x), "image_height",       GINT_TO_POINTER(image_info.image_height));
 
-    gtk_object_set_data(GTK_OBJECT(adjustment_size_y), "scale-adjustment",   (void *) scale_widget);
-    gtk_object_set_data(GTK_OBJECT(adjustment_size_y), "size-x-adjustment",  (void *) adjustment_size_x);
-    gtk_object_set_data(GTK_OBJECT(adjustment_size_y), "image_width",        (void *) image_info.image_width);
-    gtk_object_set_data(GTK_OBJECT(adjustment_size_y), "image_height",       (void *) image_info.image_height);
+    gtk_object_set_data(GTK_OBJECT(adjustment_size_y), "scale-adjustment",   GINT_TO_POINTER(scale_widget));
+    gtk_object_set_data(GTK_OBJECT(adjustment_size_y), "size-x-adjustment",  GINT_TO_POINTER(adjustment_size_x));
+    gtk_object_set_data(GTK_OBJECT(adjustment_size_y), "image_width",        GINT_TO_POINTER(image_info.image_width));
+    gtk_object_set_data(GTK_OBJECT(adjustment_size_y), "image_height",       GINT_TO_POINTER(image_info.image_height));
   }
   else
   {
@@ -763,12 +765,12 @@ static void xsane_viewer_scale_callback(GtkWidget *window, gpointer data)
     xsane_back_gtk_set_tooltip(xsane.tooltips, spinbutton, DESC_SCALE_WIDTH); 
 
     gtk_object_set_data(GTK_OBJECT(scalex_widget), "size-x-adjustment",  (void *) adjustment_size_x);
-    gtk_object_set_data(GTK_OBJECT(scalex_widget), "image_width",        (void *) image_info.image_width);
-    gtk_object_set_data(GTK_OBJECT(scalex_widget), "image_height",       (void *) image_info.image_height);
+    gtk_object_set_data(GTK_OBJECT(scalex_widget), "image_width",        GINT_TO_POINTER(image_info.image_width));
+    gtk_object_set_data(GTK_OBJECT(scalex_widget), "image_height",       GINT_TO_POINTER(image_info.image_height));
 
     gtk_object_set_data(GTK_OBJECT(adjustment_size_x), "scale-adjustment",   (void *) scalex_widget);
-    gtk_object_set_data(GTK_OBJECT(adjustment_size_x), "image_width",        (void *) image_info.image_width);
-    gtk_object_set_data(GTK_OBJECT(adjustment_size_x), "image_height",       (void *) image_info.image_height);
+    gtk_object_set_data(GTK_OBJECT(adjustment_size_x), "image_width",        GINT_TO_POINTER(image_info.image_width));
+    gtk_object_set_data(GTK_OBJECT(adjustment_size_x), "image_height",       GINT_TO_POINTER(image_info.image_height));
 
 
     /* Y */
@@ -802,12 +804,12 @@ static void xsane_viewer_scale_callback(GtkWidget *window, gpointer data)
     xsane_back_gtk_set_tooltip(xsane.tooltips, spinbutton, DESC_SCALE_HEIGHT); 
 
     gtk_object_set_data(GTK_OBJECT(scaley_widget), "size-y-adjustment", (void *) adjustment_size_y);
-    gtk_object_set_data(GTK_OBJECT(scaley_widget), "image_width",       (void *) image_info.image_width);
-    gtk_object_set_data(GTK_OBJECT(scaley_widget), "image_height",      (void *) image_info.image_height);
+    gtk_object_set_data(GTK_OBJECT(scaley_widget), "image_width",       GINT_TO_POINTER(image_info.image_width));
+    gtk_object_set_data(GTK_OBJECT(scaley_widget), "image_height",      GINT_TO_POINTER(image_info.image_height));
 
     gtk_object_set_data(GTK_OBJECT(adjustment_size_y), "scale-adjustment",   (void *) scaley_widget);
-    gtk_object_set_data(GTK_OBJECT(adjustment_size_y), "image_width",        (void *) image_info.image_width);
-    gtk_object_set_data(GTK_OBJECT(adjustment_size_y), "image_height",       (void *) image_info.image_height);
+    gtk_object_set_data(GTK_OBJECT(adjustment_size_y), "image_width",        GINT_TO_POINTER(image_info.image_width));
+    gtk_object_set_data(GTK_OBJECT(adjustment_size_y), "image_height",       GINT_TO_POINTER(image_info.image_height));
   }
 
   /* Apply Cancel */
@@ -1094,6 +1096,7 @@ static void xsane_viewer_scale_image(GtkWidget *window, gpointer data)
   {
     DBG(DBG_error, "could not save file %s\n", outfilename);
     xsane_viewer_set_sensitivity(v, TRUE);
+    fclose(infile);
    return;
   }
 
@@ -1176,6 +1179,7 @@ static void xsane_viewer_despeckle_image(GtkWidget *window, gpointer data)
   {
     DBG(DBG_error, "could not save file %s\n", outfilename);
     xsane_viewer_set_sensitivity(v, TRUE);
+    fclose(infile);
    return;
   }
 
@@ -1253,6 +1257,7 @@ static void xsane_viewer_blur_image(GtkWidget *window, gpointer data)
   {
     DBG(DBG_error, "could not save file %s\n", outfilename);
     xsane_viewer_set_sensitivity(v, TRUE);
+    fclose(infile);
    return;
   }
 
@@ -1335,6 +1340,7 @@ static void xsane_viewer_rotate(Viewer *v, int rotation)
   {
     DBG(DBG_error, "could not save file %s\n", outfilename);
     xsane_viewer_set_sensitivity(v, TRUE);
+    fclose(infile);
 
    return;
   }
@@ -1444,7 +1450,7 @@ static void xsane_viewer_zoom_callback(GtkWidget *widget, gpointer data)
 
   DBG(DBG_proc, "xsane_viewer_zoom_callback\n");
 
-  val = (int) gtk_object_get_data(GTK_OBJECT(widget), "Selection");
+  val = GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(widget), "Selection"));
   v->zoom = (float) val / 100;
   DBG(DBG_info, "setting zoom factor to %f\n", v->zoom);
   xsane_viewer_read_image(v);
@@ -1721,7 +1727,7 @@ static void xsane_viewer_set_cms_proofing_callback(GtkWidget *widget, gpointer d
   g_signal_handlers_block_by_func(GTK_OBJECT(v->cms_proofing_widget[1]), (GtkSignalFunc) xsane_viewer_set_cms_proofing_callback, v);
   g_signal_handlers_block_by_func(GTK_OBJECT(v->cms_proofing_widget[2]), (GtkSignalFunc) xsane_viewer_set_cms_proofing_callback, v);
 
-  val = (int) gtk_object_get_data(GTK_OBJECT(widget), "Selection");
+  val = GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(widget), "Selection"));
 
   DBG(DBG_proc, "xsane_viewer_set_cms_proofing_callback (%d)\n", val);
 
@@ -1748,7 +1754,7 @@ static void xsane_viewer_set_cms_intent_callback(GtkWidget *widget, gpointer dat
   g_signal_handlers_block_by_func(GTK_OBJECT(v->cms_intent_widget[2]), (GtkSignalFunc) xsane_viewer_set_cms_intent_callback, v);
   g_signal_handlers_block_by_func(GTK_OBJECT(v->cms_intent_widget[3]), (GtkSignalFunc) xsane_viewer_set_cms_intent_callback, v);
 
-  val = (int) gtk_object_get_data(GTK_OBJECT(widget), "Selection");
+  val = GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(widget), "Selection"));
 
   DBG(DBG_proc, "xsane_viewer_set_cms_intent_callback (%d)\n", val);
 
@@ -1774,7 +1780,7 @@ static void xsane_viewer_set_cms_proofing_intent_callback(GtkWidget *widget, gpo
   g_signal_handlers_block_by_func(GTK_OBJECT(v->cms_proofing_intent_widget[0]), (GtkSignalFunc) xsane_viewer_set_cms_proofing_intent_callback, v);
   g_signal_handlers_block_by_func(GTK_OBJECT(v->cms_proofing_intent_widget[1]), (GtkSignalFunc) xsane_viewer_set_cms_proofing_intent_callback, v);
 
-  val = (int) gtk_object_get_data(GTK_OBJECT(widget), "Selection");
+  val = GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(widget), "Selection"));
 
   DBG(DBG_proc, "xsane_viewer_set_cms_proofing_intent_callback (%d)\n", val);
 
@@ -1806,7 +1812,7 @@ static void xsane_viewer_set_cms_gamut_alarm_color_callback(GtkWidget *widget, g
   g_signal_handlers_block_by_func(GTK_OBJECT(v->cms_gamut_alarm_color_widget[4]), (GtkSignalFunc) xsane_viewer_set_cms_gamut_alarm_color_callback, v);
   g_signal_handlers_block_by_func(GTK_OBJECT(v->cms_gamut_alarm_color_widget[5]), (GtkSignalFunc) xsane_viewer_set_cms_gamut_alarm_color_callback, v);
 
-  val = (int) gtk_object_get_data(GTK_OBJECT(widget), "Selection");
+  val = GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(widget), "Selection"));
 
   DBG(DBG_proc, "xsane_viewer_set_cms_gamut_alarm_color_callback (%d)\n", val);
 
@@ -1942,7 +1948,7 @@ static GtkWidget *xsane_viewer_color_management_build_menu(Viewer *v)
     gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(subitem), TRUE);
   }
   g_signal_connect(GTK_OBJECT(subitem), "toggled", (GtkSignalFunc) xsane_viewer_set_cms_proofing_callback, v);
-  gtk_object_set_data(GTK_OBJECT(subitem), "Selection", (void *) 0);
+  gtk_object_set_data(GTK_OBJECT(subitem), "Selection", GINT_TO_POINTER(0));
   gtk_widget_show(subitem);
   v->cms_proofing_widget[0] = subitem;
 
@@ -1953,7 +1959,7 @@ static GtkWidget *xsane_viewer_color_management_build_menu(Viewer *v)
     gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(subitem), TRUE);
   }
   g_signal_connect(GTK_OBJECT(subitem), "toggled", (GtkSignalFunc) xsane_viewer_set_cms_proofing_callback, v);
-  gtk_object_set_data(GTK_OBJECT(subitem), "Selection", (void *) 1);
+  gtk_object_set_data(GTK_OBJECT(subitem), "Selection", GINT_TO_POINTER(1));
   gtk_widget_show(subitem);
   v->cms_proofing_widget[1] = subitem;
 
@@ -1964,7 +1970,7 @@ static GtkWidget *xsane_viewer_color_management_build_menu(Viewer *v)
     gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(subitem), TRUE);
   }
   g_signal_connect(GTK_OBJECT(subitem), "toggled", (GtkSignalFunc) xsane_viewer_set_cms_proofing_callback, v);
-  gtk_object_set_data(GTK_OBJECT(subitem), "Selection", (void *) 2);
+  gtk_object_set_data(GTK_OBJECT(subitem), "Selection", GINT_TO_POINTER(2));
   gtk_widget_show(subitem);
   v->cms_proofing_widget[2] = subitem;
 
@@ -1985,7 +1991,7 @@ static GtkWidget *xsane_viewer_color_management_build_menu(Viewer *v)
     gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(subitem), TRUE);
   }
   g_signal_connect(GTK_OBJECT(subitem), "toggled", (GtkSignalFunc) xsane_viewer_set_cms_intent_callback, v);
-  gtk_object_set_data(GTK_OBJECT(subitem), "Selection", (void *) INTENT_PERCEPTUAL);
+  gtk_object_set_data(GTK_OBJECT(subitem), "Selection", GINT_TO_POINTER(INTENT_PERCEPTUAL));
   gtk_widget_show(subitem);
   v->cms_intent_widget[INTENT_PERCEPTUAL] = subitem;
 
@@ -1996,7 +2002,7 @@ static GtkWidget *xsane_viewer_color_management_build_menu(Viewer *v)
     gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(subitem), TRUE);
   }
   g_signal_connect(GTK_OBJECT(subitem), "toggled", (GtkSignalFunc) xsane_viewer_set_cms_intent_callback, v);
-  gtk_object_set_data(GTK_OBJECT(subitem), "Selection", (void *) INTENT_RELATIVE_COLORIMETRIC);
+  gtk_object_set_data(GTK_OBJECT(subitem), "Selection", GINT_TO_POINTER(INTENT_RELATIVE_COLORIMETRIC));
   gtk_widget_show(subitem);
   v->cms_intent_widget[INTENT_RELATIVE_COLORIMETRIC] = subitem;
 
@@ -2007,7 +2013,7 @@ static GtkWidget *xsane_viewer_color_management_build_menu(Viewer *v)
     gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(subitem), TRUE);
   }
   g_signal_connect(GTK_OBJECT(subitem), "toggled", (GtkSignalFunc) xsane_viewer_set_cms_intent_callback, v);
-  gtk_object_set_data(GTK_OBJECT(subitem), "Selection", (void *) INTENT_ABSOLUTE_COLORIMETRIC);
+  gtk_object_set_data(GTK_OBJECT(subitem), "Selection", GINT_TO_POINTER(INTENT_ABSOLUTE_COLORIMETRIC));
   gtk_widget_show(subitem);
   v->cms_intent_widget[INTENT_ABSOLUTE_COLORIMETRIC] = subitem;
 
@@ -2018,7 +2024,7 @@ static GtkWidget *xsane_viewer_color_management_build_menu(Viewer *v)
     gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(subitem), TRUE);
   }
   g_signal_connect(GTK_OBJECT(subitem), "toggled", (GtkSignalFunc) xsane_viewer_set_cms_intent_callback, v);
-  gtk_object_set_data(GTK_OBJECT(subitem), "Selection", (void *) INTENT_SATURATION);
+  gtk_object_set_data(GTK_OBJECT(subitem), "Selection", GINT_TO_POINTER(INTENT_SATURATION));
   gtk_widget_show(subitem);
   v->cms_intent_widget[INTENT_SATURATION] = subitem;
 
@@ -2039,7 +2045,7 @@ static GtkWidget *xsane_viewer_color_management_build_menu(Viewer *v)
     gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(subitem), TRUE);
   }
   g_signal_connect(GTK_OBJECT(subitem), "toggled", (GtkSignalFunc) xsane_viewer_set_cms_proofing_intent_callback, v);
-  gtk_object_set_data(GTK_OBJECT(subitem), "Selection", (void *) INTENT_RELATIVE_COLORIMETRIC);
+  gtk_object_set_data(GTK_OBJECT(subitem), "Selection", GINT_TO_POINTER(INTENT_RELATIVE_COLORIMETRIC));
   gtk_widget_show(subitem);
   v->cms_proofing_intent_widget[0] = subitem;
 
@@ -2050,7 +2056,7 @@ static GtkWidget *xsane_viewer_color_management_build_menu(Viewer *v)
     gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(subitem), TRUE);
   }
   g_signal_connect(GTK_OBJECT(subitem), "toggled", (GtkSignalFunc) xsane_viewer_set_cms_proofing_intent_callback, v);
-  gtk_object_set_data(GTK_OBJECT(subitem), "Selection", (void *) INTENT_ABSOLUTE_COLORIMETRIC);
+  gtk_object_set_data(GTK_OBJECT(subitem), "Selection", GINT_TO_POINTER(INTENT_ABSOLUTE_COLORIMETRIC));
   gtk_widget_show(subitem);
   v->cms_proofing_intent_widget[1] = subitem;
 
@@ -2078,7 +2084,7 @@ static GtkWidget *xsane_viewer_color_management_build_menu(Viewer *v)
     gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(subitem), TRUE);
   }
   g_signal_connect(GTK_OBJECT(subitem), "toggled", (GtkSignalFunc) xsane_viewer_set_cms_gamut_alarm_color_callback, v);
-  gtk_object_set_data(GTK_OBJECT(subitem), "Selection", (void *) 0);
+  gtk_object_set_data(GTK_OBJECT(subitem), "Selection", GINT_TO_POINTER(0));
   gtk_widget_show(subitem);
   v->cms_gamut_alarm_color_widget[0] = subitem;
 
@@ -2089,7 +2095,7 @@ static GtkWidget *xsane_viewer_color_management_build_menu(Viewer *v)
     gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(subitem), TRUE);
   }
   g_signal_connect(GTK_OBJECT(subitem), "toggled", (GtkSignalFunc) xsane_viewer_set_cms_gamut_alarm_color_callback, v);
-  gtk_object_set_data(GTK_OBJECT(subitem), "Selection", (void *) 1);
+  gtk_object_set_data(GTK_OBJECT(subitem), "Selection", GINT_TO_POINTER(1));
   gtk_widget_show(subitem);
   v->cms_gamut_alarm_color_widget[1] = subitem;
 
@@ -2100,7 +2106,7 @@ static GtkWidget *xsane_viewer_color_management_build_menu(Viewer *v)
     gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(subitem), TRUE);
   }
   g_signal_connect(GTK_OBJECT(subitem), "toggled", (GtkSignalFunc) xsane_viewer_set_cms_gamut_alarm_color_callback, v);
-  gtk_object_set_data(GTK_OBJECT(subitem), "Selection", (void *) 2);
+  gtk_object_set_data(GTK_OBJECT(subitem), "Selection", GINT_TO_POINTER(2));
   gtk_widget_show(subitem);
   v->cms_gamut_alarm_color_widget[2] = subitem;
 
@@ -2111,7 +2117,7 @@ static GtkWidget *xsane_viewer_color_management_build_menu(Viewer *v)
     gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(subitem), TRUE);
   }
   g_signal_connect(GTK_OBJECT(subitem), "toggled", (GtkSignalFunc) xsane_viewer_set_cms_gamut_alarm_color_callback, v);
-  gtk_object_set_data(GTK_OBJECT(subitem), "Selection", (void *) 3);
+  gtk_object_set_data(GTK_OBJECT(subitem), "Selection", GINT_TO_POINTER(3));
   gtk_widget_show(subitem);
   v->cms_gamut_alarm_color_widget[3] = subitem;
 
@@ -2122,7 +2128,7 @@ static GtkWidget *xsane_viewer_color_management_build_menu(Viewer *v)
     gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(subitem), TRUE);
   }
   g_signal_connect(GTK_OBJECT(subitem), "toggled", (GtkSignalFunc) xsane_viewer_set_cms_gamut_alarm_color_callback, v);
-  gtk_object_set_data(GTK_OBJECT(subitem), "Selection", (void *) 4);
+  gtk_object_set_data(GTK_OBJECT(subitem), "Selection", GINT_TO_POINTER(4));
   gtk_widget_show(subitem);
   v->cms_gamut_alarm_color_widget[4] = subitem;
 
@@ -2133,7 +2139,7 @@ static GtkWidget *xsane_viewer_color_management_build_menu(Viewer *v)
     gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(subitem), TRUE);
   }
   g_signal_connect(GTK_OBJECT(subitem), "toggled", (GtkSignalFunc) xsane_viewer_set_cms_gamut_alarm_color_callback, v);
-  gtk_object_set_data(GTK_OBJECT(subitem), "Selection", (void *) 5);
+  gtk_object_set_data(GTK_OBJECT(subitem), "Selection", GINT_TO_POINTER(5));
   gtk_widget_show(subitem);
   v->cms_gamut_alarm_color_widget[5] = subitem;
 
@@ -2158,7 +2164,7 @@ static int xsane_viewer_read_image_header(Viewer *v)
   if (!infile)
   {
     DBG(DBG_error, "could not load file %s\n", v->filename);
-   return -1;
+    return -1;
   }
 
   xsane_read_pnm_header(infile, &image_info);
@@ -2200,7 +2206,7 @@ static int xsane_viewer_read_image_header(Viewer *v)
 
 static int xsane_viewer_read_image(Viewer *v)
 {
- unsigned char *cms_row, *row, *src_row;
+ unsigned char *cms_row, *row = NULL, *src_row = NULL;
  int x, y;
  int last_y;
  int nread;
@@ -2211,6 +2217,7 @@ static int xsane_viewer_read_image(Viewer *v)
  float size;
  char *size_unit;
  int width, height;
+ int retval = 0;
 
 #ifdef HAVE_LIBLCMS
  cmsHPROFILE hInProfile = NULL;
@@ -2311,7 +2318,8 @@ static int xsane_viewer_read_image(Viewer *v)
 
       snprintf(buf, sizeof(buf), "%s\n%s %s: %s\n", ERR_CMS_CONVERSION, ERR_CMS_OPEN_ICM_FILE, CMS_SCANNER_ICM, image_info.icm_profile);
       xsane_back_gtk_error(buf, TRUE);
-     return -1;
+      retval = -1;
+      goto out;
     }
 
     hOutProfile = cmsOpenProfileFromFile(preferences.display_icm_profile, "r");
@@ -2323,7 +2331,8 @@ static int xsane_viewer_read_image(Viewer *v)
 
       snprintf(buf, sizeof(buf), "%s\n%s %s: %s\n", ERR_CMS_CONVERSION, ERR_CMS_OPEN_ICM_FILE, CMS_DISPLAY_ICM, preferences.display_icm_profile);
       xsane_back_gtk_error(buf, TRUE);
-     return -1;
+      retval = -1;
+      goto out;
     }
       
 
@@ -2352,7 +2361,8 @@ static int xsane_viewer_read_image(Viewer *v)
 
         snprintf(buf, sizeof(buf), "%s\n%s %s: %s\n", ERR_CMS_CONVERSION, ERR_CMS_OPEN_ICM_FILE, CMS_PROOF_ICM, cms_proof_icm_profile);
         xsane_back_gtk_error(buf, TRUE);
-       return -1;
+        retval = -1;
+        goto out;
       }
 
       hTransform = cmsCreateProofingTransform(hInProfile, cms_input_format,
@@ -2374,7 +2384,8 @@ static int xsane_viewer_read_image(Viewer *v)
 
       snprintf(buf, sizeof(buf), "%s\n%s\n", ERR_CMS_CONVERSION, ERR_CMS_CREATE_TRANSFORM);
       xsane_back_gtk_error(buf, TRUE);
-     return -1;
+      retval = -1;
+      goto out;
     }
   }
 #endif
@@ -2414,39 +2425,11 @@ static int xsane_viewer_read_image(Viewer *v)
     row     = malloc(((int) image_info.image_width * v->zoom) * image_info.channels);
   }
 
-#ifdef HAVE_LIBLCMS
-  if ((v->enable_color_management) && (v->cms_enable))
-  {
-    cms_row = malloc(((int) image_info.image_width * v->zoom) * image_info.channels);
-  }
-  else
-#endif
+  if (!row || !src_row)
   {
-    cms_row = row;
-  }
-
-  if (!row || !src_row || !cms_row)
-  {
-    if (src_row)
-    {
-      free(src_row);
-    }
-
-    if (row)
-    {
-      free(row);
-    }
-
-#ifdef HAVE_LIBLCMS
-    if ((cms_row) && (v->enable_color_management) && (v->cms_enable))
-    {
-      free(cms_row);
-    }
-#endif
-
-    fclose(infile);
-    DBG(DBG_error, "could not allocate memory\n");
-   return -1;
+   DBG(DBG_error, "could not allocate memory\n");
+   retval = -1;
+   goto out;
   }
 
 
@@ -2514,7 +2497,13 @@ static int xsane_viewer_read_image(Viewer *v)
        guint16 *src_row16 = (guint16 *) src_row;
        guint16 *dst_row16 = (guint16 *) row;
 
-        fseek(infile, pos0 + (((int) (y / v->zoom)) * image_info.image_width) * image_info.channels * 2, SEEK_SET);
+        if (fseek(infile, pos0 + (((int) (y / v->zoom)) * image_info.image_width) * image_info.channels * 2, SEEK_SET))
+        {
+          DBG(DBG_error, "could not seek in file '%s': '%s'\n", v->filename,
+              strerror(errno));
+          retval = -1;
+          goto out;
+        }
         nread = fread(src_row, 2 * image_info.channels, image_info.image_width, infile);
 
         if (image_info.channels > 1)
@@ -2541,10 +2530,22 @@ static int xsane_viewer_read_image(Viewer *v)
 #ifdef HAVE_LIBLCMS
     if ((v->enable_color_management) && (v->cms_enable))
     {
+      cms_row = malloc(((int) image_info.image_width * v->zoom) * image_info.channels);
+      if (! cms_row)
+      {
+        DBG(DBG_error, "could not allocate memory\n");
+        retval = -1;
+        goto out;
+      }
       cmsDoTransform(hTransform, row, cms_row, image_info.image_width * v->zoom);
+      gtk_preview_draw_row(GTK_PREVIEW(v->window), cms_row, 0, y, image_info.image_width * v->zoom);
+      free(cms_row);
     }
+    else
 #endif
-    gtk_preview_draw_row(GTK_PREVIEW(v->window), cms_row, 0, y, image_info.image_width * v->zoom);
+    {
+      gtk_preview_draw_row(GTK_PREVIEW(v->window), row, 0, y, image_info.image_width * v->zoom);
+    }
   }
 
   gtk_preview_put(GTK_PREVIEW(v->window), v->window->window, v->window->style->black_gc, 0, 0, 0, 0, 
@@ -2610,9 +2611,23 @@ static int xsane_viewer_read_image(Viewer *v)
     gtk_window_set_default_size(GTK_WINDOW(v->top), width, height);
   }
 
-  free(row);
-  free(src_row);
-  fclose(infile);
+out:
+  if (row)
+  {
+    free(row);
+  }
+
+  if (src_row)
+  {
+    free(src_row);
+  }
+
+  /* cms_row is freed directly after use */
+
+  if (infile)
+  {
+    fclose(infile);
+  }
 
 #ifdef HAVE_LIBLCMS
   if ((v->enable_color_management) && (v->cms_enable))
@@ -2621,7 +2636,7 @@ static int xsane_viewer_read_image(Viewer *v)
   }
 #endif
 
- return 0;
+  return retval;
 }
 
 #if 0 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
@@ -3066,7 +3081,7 @@ Viewer *xsane_viewer_new(char *filename, char *selection_filetype, int allow_red
     zoom_menu_item = gtk_menu_item_new_with_label(buf);
     gtk_menu_append(GTK_MENU(zoom_menu), zoom_menu_item);
     g_signal_connect(GTK_OBJECT(zoom_menu_item), "activate", (GtkSignalFunc) xsane_viewer_zoom_callback, v);
-    gtk_object_set_data(GTK_OBJECT(zoom_menu_item), "Selection", (void *) xsane_viewer_zoom[i]);
+    gtk_object_set_data(GTK_OBJECT(zoom_menu_item), "Selection", GINT_TO_POINTER(xsane_viewer_zoom[i]));
     gtk_widget_show(zoom_menu_item);
     if (v->zoom*100 == xsane_viewer_zoom[i])
     {
diff --git a/src/xsane.c b/src/xsane.c
index 02b4da4..a4a3f1e 100644
--- a/src/xsane.c
+++ b/src/xsane.c
@@ -585,7 +585,7 @@ static void xsane_show_batch_scan_callback(GtkWidget * widget)
 
 static void xsane_paper_orientation_callback(GtkWidget *widget, gpointer data)
 {
- int pos = (int) data;
+ int pos = GPOINTER_TO_INT(data);
 
   DBG(DBG_proc, "xsane_paper_orientation_callback\n");
 
@@ -601,7 +601,7 @@ static void xsane_printer_callback(GtkWidget *widget, gpointer data)
 
   DBG(DBG_proc, "xsane_printer_callback\n");
 
-  preferences.printernr = (int) data;
+  preferences.printernr = GPOINTER_TO_INT(data);
 
   switch (xsane.param.format)
   {
@@ -1165,7 +1165,7 @@ static void xsane_scanmode_menu_callback(GtkWidget *widget, gpointer data)
 
 static void xsane_cms_function_menu_callback(GtkWidget *widget, gpointer data)
 {
-  preferences.cms_function = (int) data;
+  preferences.cms_function = GPOINTER_TO_INT(data);
   DBG(DBG_proc, "xsane_cms_function_menu_callback(%d)\n", preferences.cms_function);
 }
 
@@ -1402,7 +1402,7 @@ GtkWidget *xsane_update_xsane_callback() /* creates the XSane option window */
 
 
       gtk_container_add(GTK_CONTAINER(paper_orientation_menu), paper_orientation_item);
-      g_signal_connect(GTK_OBJECT(paper_orientation_item), "activate", (GtkSignalFunc) xsane_paper_orientation_callback, (void *) i);
+      g_signal_connect(GTK_OBJECT(paper_orientation_item), "activate", (GtkSignalFunc) xsane_paper_orientation_callback, GINT_TO_POINTER(i));
 
       gtk_widget_show(paper_orientation_item);
     }
@@ -1436,7 +1436,7 @@ GtkWidget *xsane_update_xsane_callback() /* creates the XSane option window */
                                    GDK_F1+i, GDK_SHIFT_MASK,  DEF_GTK_MENU_ACCEL_VISIBLE | DEF_GTK_ACCEL_LOCKED);
       }
       gtk_container_add(GTK_CONTAINER(xsane_printer_menu), xsane_printer_item);
-      g_signal_connect(GTK_OBJECT(xsane_printer_item), "activate", (GtkSignalFunc) xsane_printer_callback, (void *) i);
+      g_signal_connect(GTK_OBJECT(xsane_printer_item), "activate", (GtkSignalFunc) xsane_printer_callback, GINT_TO_POINTER(i));
       gtk_widget_show(xsane_printer_item);
     }
 
@@ -1584,9 +1584,9 @@ GtkWidget *xsane_update_xsane_callback() /* creates the XSane option window */
     {
       xsane_medium_item = gtk_menu_item_new_with_label(preferences.medium[i]->name);
       gtk_menu_append(GTK_MENU(xsane_medium_menu), xsane_medium_item);
-      g_signal_connect(GTK_OBJECT(xsane_medium_item), "button_press_event", (GtkSignalFunc) xsane_medium_context_menu_callback, (void *) i);
-      g_signal_connect(GTK_OBJECT(xsane_medium_item), "activate", (GtkSignalFunc) xsane_set_medium_callback, (void *) i);
-      gtk_object_set_data(GTK_OBJECT(xsane_medium_item), "Selection", (void *) i);
+      g_signal_connect(GTK_OBJECT(xsane_medium_item), "button_press_event", (GtkSignalFunc) xsane_medium_context_menu_callback, GINT_TO_POINTER(i));
+      g_signal_connect(GTK_OBJECT(xsane_medium_item), "activate", (GtkSignalFunc) xsane_set_medium_callback, GINT_TO_POINTER(i));
+      gtk_object_set_data(GTK_OBJECT(xsane_medium_item), "Selection", GINT_TO_POINTER(i));
 
       gtk_widget_show(xsane_medium_item);
     }
@@ -2697,7 +2697,7 @@ static gint xsane_medium_move_up_callback(GtkWidget *widget, GtkWidget *medium_w
   
   DBG(DBG_proc, "xsane_medium_move_up_callback\n");
   
-  selection = (int) gtk_object_get_data(GTK_OBJECT(medium_widget), "Selection");
+  selection = GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(medium_widget), "Selection"));
 
   if (selection > 1) /* make sure "full range" stays at top */
   {
@@ -2732,7 +2732,7 @@ static gint xsane_medium_move_down_callback(GtkWidget *widget, GtkWidget *medium
   
   DBG(DBG_proc, "xsane_medium_move_up_callback\n");
   
-  selection = (int) gtk_object_get_data(GTK_OBJECT(medium_widget), "Selection");
+  selection = GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(medium_widget), "Selection"));
 
   if ((selection) && (selection < preferences.medium_definitions-1))
   {
@@ -2771,7 +2771,7 @@ static gint xsane_medium_rename_callback(GtkWidget *widget, GtkWidget *medium_wi
 
   DBG(DBG_proc, "xsane_medium_rename_callback\n");
 
-  selection = (int) gtk_object_get_data(GTK_OBJECT(medium_widget), "Selection");
+  selection = GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(medium_widget), "Selection"));
 
   oldname = strdup(preferences.medium[selection]->name);
 
@@ -2779,7 +2779,7 @@ static gint xsane_medium_rename_callback(GtkWidget *widget, GtkWidget *medium_wi
 
   /* set menu in correct state, is a bit strange this way but I do not have a better idea */
   old_medium_menu = gtk_option_menu_get_menu(GTK_OPTION_MENU(xsane.medium_widget));
-  old_selection = (int) gtk_object_get_data(GTK_OBJECT(gtk_menu_get_active(GTK_MENU(old_medium_menu))), "Selection");
+  old_selection = GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(gtk_menu_get_active(GTK_MENU(old_medium_menu))), "Selection"));
   gtk_menu_popdown(GTK_MENU(old_medium_menu));
   gtk_option_menu_set_history(GTK_OPTION_MENU(xsane.medium_widget), old_selection);
 
@@ -2814,13 +2814,13 @@ static gint xsane_medium_add_callback(GtkWidget *widget, GtkWidget *medium_widge
   DBG(DBG_proc, "xsane_medium_add_callback\n");
 
   /* add new item after selected item */
-  selection = 1 + (int) gtk_object_get_data(GTK_OBJECT(medium_widget), "Selection");
+  selection = 1 + GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(medium_widget), "Selection"));
 
   oldname = strdup(TEXT_NEW_MEDIA_NAME);
 
   /* set menu in correct state, is a bit strange this way but I do not have a better idea */
   old_medium_menu = gtk_option_menu_get_menu(GTK_OPTION_MENU(xsane.medium_widget));
-  old_selection = (int) gtk_object_get_data(GTK_OBJECT(gtk_menu_get_active(GTK_MENU(old_medium_menu))), "Selection");
+  old_selection = GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(gtk_menu_get_active(GTK_MENU(old_medium_menu))), "Selection"));
   gtk_menu_popdown(GTK_MENU(old_medium_menu));
   gtk_option_menu_set_history(GTK_OPTION_MENU(xsane.medium_widget), old_selection);
 
@@ -2895,7 +2895,7 @@ static gint xsane_medium_delete_callback(GtkWidget *widget, GtkWidget *medium_wi
 
   DBG(DBG_proc, "xsane_medium_delete_callback\n");
 
-  selection = (int) gtk_object_get_data(GTK_OBJECT(medium_widget), "Selection");
+  selection = GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(medium_widget), "Selection"));
 
   if (selection) /* full range can not be deleted */
   {
@@ -2951,7 +2951,7 @@ static gint xsane_medium_context_menu_callback(GtkWidget *widget, GdkEvent *even
                                                                                                 
   DBG(DBG_proc, "xsane_medium_context_menu_callback\n");
                                                                                                 
-  selection = (int) gtk_object_get_data(GTK_OBJECT(widget), "Selection");
+  selection = GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(widget), "Selection"));
                                                                                                 
   if (event->type == GDK_BUTTON_PRESS)
   {
@@ -3022,7 +3022,7 @@ static gint xsane_medium_context_menu_callback(GtkWidget *widget, GdkEvent *even
 
 static void xsane_set_medium_callback(GtkWidget *widget, gpointer data)
 {
- int medium_nr = (int) data;
+ int medium_nr = GPOINTER_TO_INT(data);
 
   if (medium_nr != preferences.medium_nr)
   {
@@ -3205,11 +3205,11 @@ static void xsane_set_update_policy_callback(GtkWidget *widget, gpointer data)
   DBG(DBG_proc, "xsane_set_update_policy_callback\n");
 
   g_signal_handlers_block_by_func(GTK_OBJECT(xsane.update_policy_continu), (GtkSignalFunc) xsane_set_update_policy_callback,
-                                   (void *) GTK_UPDATE_CONTINUOUS);
+                                   GINT_TO_POINTER(GTK_UPDATE_CONTINUOUS));
   g_signal_handlers_block_by_func(GTK_OBJECT(xsane.update_policy_discont), (GtkSignalFunc) xsane_set_update_policy_callback,
-                                   (void *) GTK_UPDATE_DISCONTINUOUS);
+                                   GINT_TO_POINTER(GTK_UPDATE_DISCONTINUOUS));
   g_signal_handlers_block_by_func(GTK_OBJECT(xsane.update_policy_delayed), (GtkSignalFunc) xsane_set_update_policy_callback,
-                                   (void *) GTK_UPDATE_DELAYED);
+                                   GINT_TO_POINTER(GTK_UPDATE_DELAYED));
 
   if (policy == GTK_UPDATE_CONTINUOUS)
   {
@@ -3231,11 +3231,11 @@ static void xsane_set_update_policy_callback(GtkWidget *widget, gpointer data)
   }
 
   g_signal_handlers_unblock_by_func(GTK_OBJECT(xsane.update_policy_continu), (GtkSignalFunc) xsane_set_update_policy_callback,
-                                     (void *) GTK_UPDATE_CONTINUOUS);
+                                     GINT_TO_POINTER(GTK_UPDATE_CONTINUOUS));
   g_signal_handlers_unblock_by_func(GTK_OBJECT(xsane.update_policy_discont), (GtkSignalFunc) xsane_set_update_policy_callback,
-                                     (void *) GTK_UPDATE_DISCONTINUOUS);
+                                     GINT_TO_POINTER(GTK_UPDATE_DISCONTINUOUS));
   g_signal_handlers_unblock_by_func(GTK_OBJECT(xsane.update_policy_delayed), (GtkSignalFunc) xsane_set_update_policy_callback,
-                                     (void *) GTK_UPDATE_DELAYED);
+                                     GINT_TO_POINTER(GTK_UPDATE_DELAYED));
 
   preferences.gtk_update_policy = policy;
   xsane_pref_save();
@@ -3389,7 +3389,7 @@ static void xsane_info_dialog(GtkWidget *widget, gpointer data)
     snprintf(buf, sizeof(buf), "%d bit", (int) (0.5 + log(opt->constraint.range->max+1.0) / log(2.0)));
     label = xsane_info_table_text_new(table, buf, 1, 2);
   }
-  else if ((!xsane.xsane_channels > 1) && (xsane.scanner_gamma_gray)) /* gray gamma correction by scanner */
+  else if ((!(xsane.xsane_channels > 1)) && (xsane.scanner_gamma_gray)) /* gray gamma correction by scanner */
   {
    const SANE_Option_Descriptor *opt;
 
@@ -3807,6 +3807,13 @@ static void xsane_close_fds_for_exec(signed int first_fd_to_leave_open, ...)
 
   open_max = (int) sysconf (_SC_OPEN_MAX);
 
+  if (open_max < 0)
+  {
+    DBG(DBG_error, "xsane_close_fds_for_exec(): Can't determine maximum "
+                   "number of open files.");
+    return;
+  }
+
   close_fds = malloc (open_max);
 
   memset (close_fds, 1, open_max);
@@ -4147,7 +4154,7 @@ static GtkWidget *xsane_view_build_menu(void)
   {
     gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(subitem), TRUE);
   }
-  g_signal_connect(GTK_OBJECT(subitem), "toggled", (GtkSignalFunc) xsane_set_update_policy_callback, (void *) GTK_UPDATE_CONTINUOUS);
+  g_signal_connect(GTK_OBJECT(subitem), "toggled", (GtkSignalFunc) xsane_set_update_policy_callback, GINT_TO_POINTER(GTK_UPDATE_CONTINUOUS));
   gtk_widget_show(subitem);
   xsane.update_policy_continu = subitem;
 
@@ -4157,7 +4164,7 @@ static GtkWidget *xsane_view_build_menu(void)
   {
     gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(subitem), TRUE);
   }
-  g_signal_connect(GTK_OBJECT(subitem), "toggled", (GtkSignalFunc) xsane_set_update_policy_callback, (void *) GTK_UPDATE_DISCONTINUOUS);
+  g_signal_connect(GTK_OBJECT(subitem), "toggled", (GtkSignalFunc) xsane_set_update_policy_callback, GINT_TO_POINTER(GTK_UPDATE_DISCONTINUOUS));
   gtk_widget_show(subitem);
   xsane.update_policy_discont = subitem;
 
@@ -4167,7 +4174,7 @@ static GtkWidget *xsane_view_build_menu(void)
   {
     gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(subitem), TRUE);
   }
-  g_signal_connect(GTK_OBJECT(subitem), "toggled", (GtkSignalFunc) xsane_set_update_policy_callback, (void *) GTK_UPDATE_DELAYED);
+  g_signal_connect(GTK_OBJECT(subitem), "toggled", (GtkSignalFunc) xsane_set_update_policy_callback, GINT_TO_POINTER(GTK_UPDATE_DELAYED));
   gtk_widget_show(subitem);
   xsane.update_policy_delayed = subitem;
 
@@ -5709,8 +5716,8 @@ static void xsane_choose_device(void)
       gtk_widget_add_accelerator(button, "clicked", device_selection_accelerator_group, GDK_F1+i, 0, DEF_GTK_ACCEL_LOCKED);
     }
 
-    g_signal_connect(GTK_OBJECT(button), "button_press_event", (GtkSignalFunc) xsane_select_device_by_mouse_callback, (void *) (long) i);
-    g_signal_connect(GTK_OBJECT(button), "clicked", (GtkSignalFunc) xsane_select_device_by_key_callback, (void *) (long) i);
+    g_signal_connect(GTK_OBJECT(button), "button_press_event", (GtkSignalFunc) xsane_select_device_by_mouse_callback, GINT_TO_POINTER(i));
+    g_signal_connect(GTK_OBJECT(button), "clicked", (GtkSignalFunc) xsane_select_device_by_key_callback, GINT_TO_POINTER(i));
     gtk_box_pack_start(GTK_BOX(device_vbox), button, TRUE, TRUE, 0);
     gtk_widget_show(button);
     owner = gtk_radio_button_group(GTK_RADIO_BUTTON(button));;
-- 
1.9.0

