Merge branch 'scanadf_segv' into 'master'

scanadf.c: implement dedicated exit function to prevent SIGSEGV

Closes #19

See merge request sane-project/frontends!1
merge-requests/2/head
Olaf Meeuwissen 2020-09-27 09:42:13 +00:00
commit 572980c815
1 zmienionych plików z 42 dodań i 18 usunięć

Wyświetl plik

@ -126,6 +126,8 @@ static int resolution_opt = -1, x_resolution_opt = -1, y_resolution_opt = -1;
static SANE_Word window_val[2]; static SANE_Word window_val[2];
static int window_val_user[2]; /* is width/height user-specified? */ static int window_val_user[2]; /* is width/height user-specified? */
static void scanadf_exit (int);
static const char usage[] = "Usage: %s [OPTION]...\n\ static const char usage[] = "Usage: %s [OPTION]...\n\
\n\ \n\
Start image acquisition on a scanner device and write image data to\n\ Start image acquisition on a scanner device and write image data to\n\
@ -381,7 +383,7 @@ parse_scalar (const SANE_Option_Descriptor * opt, const char * str,
fprintf (stderr, fprintf (stderr,
"%s: option --%s: bad option value (rest of option: %s)\n", "%s: option --%s: bad option value (rest of option: %s)\n",
prog_name, opt->name, str); prog_name, opt->name, str);
exit (1); scanadf_exit (1);
} }
str = end; str = end;
@ -482,7 +484,7 @@ parse_vector (const SANE_Option_Descriptor * opt, const char * str,
{ {
fprintf (stderr, "%s: option --%s: closing bracket missing " fprintf (stderr, "%s: option --%s: closing bracket missing "
"(rest of option: %s)\n", prog_name, opt->name, str); "(rest of option: %s)\n", prog_name, opt->name, str);
exit (1); scanadf_exit (1);
} }
str = end + 1; str = end + 1;
} }
@ -493,20 +495,20 @@ parse_vector (const SANE_Option_Descriptor * opt, const char * str,
{ {
fprintf (stderr, "%s: option --%s: index %d out of range [0..%ld]\n", fprintf (stderr, "%s: option --%s: index %d out of range [0..%ld]\n",
prog_name, opt->name, index, (long) vector_length - 1); prog_name, opt->name, index, (long) vector_length - 1);
exit (1); scanadf_exit (1);
} }
/* read value */ /* read value */
str = parse_scalar (opt, str, &value); str = parse_scalar (opt, str, &value);
if (!str) if (!str)
exit (1); scanadf_exit (1);
if (*str && *str != '-' && *str != ',') if (*str && *str != '-' && *str != ',')
{ {
fprintf (stderr, fprintf (stderr,
"%s: option --%s: illegal separator (rest of option: %s)\n", "%s: option --%s: illegal separator (rest of option: %s)\n",
prog_name, opt->name, str); prog_name, opt->name, str);
exit (1); scanadf_exit (1);
} }
/* store value: */ /* store value: */
@ -672,6 +674,28 @@ fetch_options (SANE_Device * device)
} }
} }
static void
scanadf_exit (int status)
{
if (device)
{
if (verbose > 1)
fprintf (stderr, "Closing device\n");
sane_close (device);
}
if (verbose > 1)
fprintf (stderr, "Calling sane_exit\n");
sane_exit ();
if (all_options)
free (all_options);
if (option_number)
free (option_number);
if (verbose > 1)
fprintf (stderr, "scanimage: finished\n");
exit (status);
}
static void static void
set_option (SANE_Handle device, int optnum, void * valuep) set_option (SANE_Handle device, int optnum, void * valuep)
{ {
@ -691,7 +715,7 @@ set_option (SANE_Handle device, int optnum, void * valuep)
{ {
fprintf (stderr, "%s: setting of option --%s failed (%s)\n", fprintf (stderr, "%s: setting of option --%s failed (%s)\n",
prog_name, opt->name, sane_strstatus (status)); prog_name, opt->name, sane_strstatus (status));
exit (1); scanadf_exit (1);
} }
if ((info & SANE_INFO_INEXACT) && opt->size == sizeof (SANE_Word)) if ((info & SANE_INFO_INEXACT) && opt->size == sizeof (SANE_Word))
@ -726,7 +750,7 @@ process_backend_option (SANE_Handle device, int optnum, const char * optarg)
{ {
fprintf (stderr, "%s: attempted to set inactive option %s\n", fprintf (stderr, "%s: attempted to set inactive option %s\n",
prog_name, opt->name); prog_name, opt->name);
exit (1); scanadf_exit (1);
} }
if ((opt->cap & SANE_CAP_AUTOMATIC) && strncasecmp (optarg, "auto", 4) == 0) if ((opt->cap & SANE_CAP_AUTOMATIC) && strncasecmp (optarg, "auto", 4) == 0)
@ -737,7 +761,7 @@ process_backend_option (SANE_Handle device, int optnum, const char * optarg)
{ {
fprintf (stderr, "%s: failed to set option --%s to automatic (%s)\n", fprintf (stderr, "%s: failed to set option --%s to automatic (%s)\n",
prog_name, opt->name, sane_strstatus (status)); prog_name, opt->name, sane_strstatus (status));
exit (1); scanadf_exit (1);
} }
return; return;
} }
@ -757,7 +781,7 @@ process_backend_option (SANE_Handle device, int optnum, const char * optarg)
{ {
fprintf (stderr, "%s: option --%s: bad option value `%s'\n", fprintf (stderr, "%s: option --%s: bad option value `%s'\n",
prog_name, opt->name, optarg); prog_name, opt->name, optarg);
exit (1); scanadf_exit (1);
} }
} }
break; break;
@ -773,7 +797,7 @@ process_backend_option (SANE_Handle device, int optnum, const char * optarg)
if (!vector) if (!vector)
{ {
fprintf (stderr, "%s: out of memory\n", prog_name); fprintf (stderr, "%s: out of memory\n", prog_name);
exit (1); scanadf_exit (1);
} }
} }
parse_vector (opt, optarg, vector, vector_length); parse_vector (opt, optarg, vector, vector_length);
@ -1439,12 +1463,12 @@ main (int argc, char **argv)
{ {
fprintf (stderr, "%s: sane_get_devices() failed: %s\n", fprintf (stderr, "%s: sane_get_devices() failed: %s\n",
prog_name, sane_strstatus (status)); prog_name, sane_strstatus (status));
exit (1); scanadf_exit (1);
} }
if (!device_list[0]) if (!device_list[0])
{ {
fprintf (stderr, "%s: no SANE devices found\n", prog_name); fprintf (stderr, "%s: no SANE devices found\n", prog_name);
exit (1); scanadf_exit (1);
} }
devname = device_list[0]->name; devname = device_list[0]->name;
} }
@ -1457,7 +1481,7 @@ main (int argc, char **argv)
if (help) if (help)
device = 0; device = 0;
else else
exit (1); scanadf_exit (1);
} }
if (device) if (device)
@ -1469,7 +1493,7 @@ main (int argc, char **argv)
{ {
fprintf (stderr, "%s: unable to determine option count\n", fprintf (stderr, "%s: unable to determine option count\n",
prog_name); prog_name);
exit (1); scanadf_exit (1);
} }
all_options_len = num_dev_options + NELEMS(basic_options) + 1; all_options_len = num_dev_options + NELEMS(basic_options) + 1;
@ -1480,7 +1504,7 @@ main (int argc, char **argv)
{ {
fprintf (stderr, "%s: out of memory in fetch_options()\n", fprintf (stderr, "%s: out of memory in fetch_options()\n",
prog_name); prog_name);
exit (1); scanadf_exit (1);
} }
fetch_options (device); fetch_options (device);
@ -1510,7 +1534,7 @@ main (int argc, char **argv)
if (!full_optstring) if (!full_optstring)
{ {
fprintf (stderr, "%s: out of memory\n", prog_name); fprintf (stderr, "%s: out of memory\n", prog_name);
exit (1); scanadf_exit (1);
} }
strcpy (full_optstring, BASE_OPTSTRING); strcpy (full_optstring, BASE_OPTSTRING);
@ -1530,7 +1554,7 @@ main (int argc, char **argv)
{ {
case ':': case ':':
case '?': case '?':
exit (1); /* error message is printed by getopt_long() */ scanadf_exit (1); /* error message is printed by getopt_long() */
case 'd': case 'h': case 'v': case 'V': case 'T': case 'd': case 'h': case 'v': case 'V': case 'T':
case 'o': case 'S': case 's': case 'e': case 'p': case 'r': case 'o': case 'S': case 's': case 'e': case 'p': case 'r':
@ -1639,7 +1663,7 @@ List of available devices:", prog_name);
} }
} }
fputc ('\n', stdout); fputc ('\n', stdout);
exit (0); scanadf_exit (0);
} }
signal (SIGHUP, sighandler); signal (SIGHUP, sighandler);