summaryrefslogtreecommitdiff
path: root/source/xap/xsane/xsane-0.999-coverity.patch
blob: f6350fe985f0c60d81378644bc760769d0dd84e2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
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