minor memory handling fixes

- check success after calloc()
- fix memleak in find_start_line()
merge-requests/1/head
Stphane Voltz 2009-10-01 06:22:25 +02:00
rodzic ce0ba551ca
commit 255c83e1d7
1 zmienionych plików z 138 dodań i 122 usunięć

Wyświetl plik

@ -82,8 +82,7 @@ static SANE_Bool low_is_home_line (unsigned char *buffer);
static SANE_Status low_get_start_loc (SANE_Int resolution,
SANE_Int * vert_start,
SANE_Int * hor_start,
SANE_Int offset,
Lexmark_Device * dev);
SANE_Int offset, Lexmark_Device * dev);
static void low_rewind (Lexmark_Device * dev, SANE_Byte * regs);
static SANE_Status low_start_mvmt (SANE_Int devnum);
static SANE_Status low_stop_mvmt (SANE_Int devnum);
@ -419,10 +418,12 @@ lexmark_low_wake_up (Lexmark_Device * dev)
*
*/
#ifdef DEEP_DEBUG
static void write_pnm_file(char *title, int pixels, int lines, int color, unsigned char *data)
static void
write_pnm_file (char *title, int pixels, int lines, int color,
unsigned char *data)
{
FILE * fdbg;
int x,y;
FILE *fdbg;
int x, y;
fdbg = fopen (title, "wb");
if (fdbg == NULL)
@ -465,7 +466,7 @@ sanei_lexmark_low_init (Lexmark_Device * dev)
int i;
SANE_Status status;
DBG_INIT();
DBG_INIT ();
status = SANE_STATUS_UNSUPPORTED;
DBG (2, "low_init: start\n");
@ -810,7 +811,7 @@ low_usb_bulk_write (SANE_Int devnum, SANE_Byte * cmd, size_t * size)
{
DBG (5,
"low_usb_bulk_write: returned %s (size = %lu, expected %lu)\n",
sane_strstatus (status), (u_long) *size, (u_long) cmd_size);
sane_strstatus (status), (u_long) * size, (u_long) cmd_size);
/* F.O. should reset the pipe here... */
}
return status;
@ -832,11 +833,11 @@ low_usb_bulk_read (SANE_Int devnum, SANE_Byte * buf, size_t * size)
{
DBG (5,
"low_usb_bulk_read: returned %s (size = %lu, expected %lu)\n",
sane_strstatus (status), (u_long) *size, (u_long) exp_size);
sane_strstatus (status), (u_long) * size, (u_long) exp_size);
/* F.O. should reset the pipe here... */
}
DBG (7, "low_usb_bulk_read: returned size = %lu (required %lu)\n",
(u_long) *size, (u_long) exp_size);
(u_long) * size, (u_long) exp_size);
return status;
}
@ -877,7 +878,7 @@ low_clr_c6 (SANE_Int devnum)
/* Clear register 0xC6 */
/* cmd_size = 0x05;
return low_usb_bulk_write (devnum, clearC6_command_block, &cmd_size);*/
return low_usb_bulk_write (devnum, clearC6_command_block, &cmd_size); */
reg = 0x00;
status = rts88xx_write_reg (devnum, 0xc6, &reg);
@ -1129,7 +1130,7 @@ sanei_lexmark_low_open_device (Lexmark_Device * dev)
size = 4;
low_usb_bulk_write (dev->devnum, command_block, &size);
size = 0xFF;
memset(shadow_regs,0,sizeof(shadow_regs));
memset (shadow_regs, 0, sizeof (shadow_regs));
low_usb_bulk_read (dev->devnum, shadow_regs, &size);
#ifdef DEEP_DEBUG
if (DBG_LEVEL > 2)
@ -1781,6 +1782,10 @@ sanei_lexmark_low_search_home_fwd (Lexmark_Device * dev)
/* create buffer for scan data */
buffer = calloc (2500, sizeof (char));
if (buffer == NULL)
{
return SANE_FALSE;
}
/* Tell the scanner to send the data */
/* Write: 91 00 09 c4 */
@ -1997,6 +2002,10 @@ sanei_lexmark_low_search_home_bwd (Lexmark_Device * dev)
/* create buffer to hold up to 10 lines of scan data */
buffer = calloc (10 * 2500, sizeof (char));
if (buffer == NULL)
{
return SANE_FALSE;
}
home_line_count = 0;
in_home_region = SANE_FALSE;
@ -2468,6 +2477,10 @@ sanei_lexmark_low_find_start_line (Lexmark_Device * dev)
/* create buffer for scan data */
buffer = calloc (5192, sizeof (char));
if (buffer == NULL)
{
return -1;
}
/* Tell the scanner to send the data */
/* Write: 91 00 14 48 */
@ -2546,14 +2559,15 @@ sanei_lexmark_low_find_start_line (Lexmark_Device * dev)
}
} /* end for buffer */
free (buffer);
DBG (2, "sanei_lexmark_low_find_start_line: end.\n");
return whiteLineCount;
}
SANE_Status
sanei_lexmark_low_set_scan_regs (Lexmark_Device * dev, SANE_Int resolution, SANE_Int offset,
SANE_Bool calibrated)
sanei_lexmark_low_set_scan_regs (Lexmark_Device * dev, SANE_Int resolution,
SANE_Int offset, SANE_Bool calibrated)
{
SANE_Bool isColourScan;
@ -3091,7 +3105,7 @@ sanei_lexmark_low_set_scan_regs (Lexmark_Device * dev, SANE_Int resolution, SANE
dev->shadow_regs[0x38] = 0x03;
/* dev->shadow_regs[0x40] = 0x90;
dev->shadow_regs[0x50] = 0x20;*/
dev->shadow_regs[0x50] = 0x20; */
/* no data compression */
dev->shadow_regs[0x40] = 0x80;
dev->shadow_regs[0x50] = 0x00;
@ -4036,7 +4050,8 @@ sanei_lexmark_low_read_scan_data (SANE_Byte * data, SANE_Int size,
status = low_poll_data (dev->devnum);
if (status != SANE_STATUS_GOOD)
{
DBG (1, "sanei_lexmark_low_read_scan_data: time-out while waiting for data.\n");
DBG (1,
"sanei_lexmark_low_read_scan_data: time-out while waiting for data.\n");
return status;
}
@ -4579,7 +4594,7 @@ sanei_lexmark_low_offset_calibration (Lexmark_Device * dev)
SANE_Status status = SANE_STATUS_GOOD;
int i, lines = 8, yoffset = 2;
int pixels;
int failed=0;
int failed = 0;
/* offsets */
int ro = 0, go = 0, bo = 0;
/* averages */
@ -4650,14 +4665,14 @@ sanei_lexmark_low_offset_calibration (Lexmark_Device * dev)
}
#ifdef DEEP_DEBUG
sprintf (title, "offset%02x.pnm", ro);
write_pnm_file(title,pixels,lines,rts88xx_is_color (regs),data);
write_pnm_file (title, pixels, lines, rts88xx_is_color (regs), data);
#endif
average = average_area (regs, data, pixels, lines, &ra, &ga, &ba);
}
if (i == 0)
{
DBG (2, "sanei_lexmark_low_offset_calibration: failed !\n");
failed=1;
failed = 1;
}
/* increase gain and scan again */
@ -4676,11 +4691,12 @@ sanei_lexmark_low_offset_calibration (Lexmark_Device * dev)
}
average = average_area (regs, data, pixels, lines, &ra, &ga, &ba);
#ifdef DEEP_DEBUG
write_pnm_file("offset-final.pnm",pixels,lines,rts88xx_is_color (regs),data);
write_pnm_file ("offset-final.pnm", pixels, lines, rts88xx_is_color (regs),
data);
#endif
/* this "law" is a guess, may (should?) be changed ... */
if(!failed)
if (!failed)
{
if (ro > ra)
dev->offset.red = ro - ra;
@ -4694,9 +4710,9 @@ sanei_lexmark_low_offset_calibration (Lexmark_Device * dev)
}
else
{
dev->offset.red=dev->sensor->offset_fallback;
dev->offset.green=dev->sensor->offset_fallback;
dev->offset.blue=dev->sensor->offset_fallback;
dev->offset.red = dev->sensor->offset_fallback;
dev->offset.green = dev->sensor->offset_fallback;
dev->offset.blue = dev->sensor->offset_fallback;
}
DBG (7,
"sanei_lexmark_low_offset_calibration: offset=(0x%02x,0x%02x,0x%02x).\n",
@ -4763,8 +4779,7 @@ sanei_lexmark_low_gain_calibration (Lexmark_Device * dev)
|| (!rts88xx_is_color (regs)
&& (ga < dev->sensor->gray_gain_target))) && (i < 25))
{
status =
low_simple_scan (dev, regs, sx, pixels, yoffset, lines, &data);
status = low_simple_scan (dev, regs, sx, pixels, yoffset, lines, &data);
if (status != SANE_STATUS_GOOD)
{
DBG (1,
@ -4775,7 +4790,7 @@ sanei_lexmark_low_gain_calibration (Lexmark_Device * dev)
}
#ifdef DEEP_DEBUG
sprintf (title, "gain%02d.pnm", i);
write_pnm_file(title,pixels,lines,rts88xx_is_color (regs),data);
write_pnm_file (title, pixels, lines, rts88xx_is_color (regs), data);
#endif
average = average_area (regs, data, pixels, lines, &ra, &ga, &ba);
free (data);
@ -4918,7 +4933,8 @@ sanei_lexmark_low_shading_calibration (Lexmark_Device * dev)
DBG (7, "sanei_lexmark_low_shading_calibration: yoffset=%d.\n", yoffset);
#ifdef DEEP_DEBUG
write_pnm_file("shading.pnm",pixels,lines,rts88xx_is_color (regs),data);
write_pnm_file ("shading.pnm", pixels, lines, rts88xx_is_color (regs),
data);
#endif
/* computes coefficients */
@ -4985,7 +5001,8 @@ sanei_lexmark_low_shading_calibration (Lexmark_Device * dev)
}
#ifdef DEEP_DEBUG
write_pnm_file("shading_bwd.pnm",pixels,lines,rts88xx_is_color (regs),data);
write_pnm_file ("shading_bwd.pnm", pixels, lines, rts88xx_is_color (regs),
data);
#endif
free (data);
@ -5084,14 +5101,14 @@ sanei_lexmark_low_assign_sensor (Lexmark_Device * dev)
/* assign model description, based on USB id, and register content when
* available */
SANE_Status
sanei_lexmark_low_assign_model (Lexmark_Device * dev, SANE_String_Const devname,
SANE_Int vendor, SANE_Int product,
SANE_Byte mainboard)
sanei_lexmark_low_assign_model (Lexmark_Device * dev,
SANE_String_Const devname, SANE_Int vendor,
SANE_Int product, SANE_Byte mainboard)
{
int dn;
SANE_Bool found = SANE_FALSE;
DBG_INIT();
DBG_INIT ();
DBG (2, "sanei_lexmark_low_assign_model: start\n");
DBG (3,
@ -5133,8 +5150,7 @@ sanei_lexmark_low_assign_model (Lexmark_Device * dev, SANE_String_Const devname,
dev->model = model_list[dn];
dev->sane.type = "flatbed scanner";
DBG (3, "sanei_lexmark_low_assign_model: assigned %s\n",
dev->model.model);
DBG (3, "sanei_lexmark_low_assign_model: assigned %s\n", dev->model.model);
/* init sensor data */
return sanei_lexmark_low_assign_sensor (dev);