~ubuntu-branches/ubuntu/maverick/vim/maverick

« back to all changes in this revision

Viewing changes to src/fileio.c

  • Committer: Bazaar Package Importer
  • Author(s): Colin Watson
  • Date: 2008-06-26 13:42:18 UTC
  • mfrom: (1.1.5 upstream)
  • Revision ID: james.westby@ubuntu.com-20080626134218-703edeyb8k70qpbz
Tags: 1:7.1.314-3ubuntu1
* Resynchronise with Debian. Remaining changes:
  - Enable detection of GNU screen as a mouse-capable terminal.
  - Add NoDisplay=true to gvim.desktop.
  - Drop vim-lesstif package and lesstif2-dev build-dependency.
  - Build-depend on libxt-dev.
  - Enable Python interpreter on basic builds.
  - Create a .pot file for translations.
  - Disable autoindent, line-wrapping, and backup files by default.
* Fixes various vulnerabilities due to improper quoting of 'execute'
  arguments (LP: #240216).
* Drop fixes for upgrade problems from Ubuntu 6.06 LTS; direct upgrades
  from 6.06 to 8.10 will not be supported.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1288
1288
#ifdef FEAT_MBYTE
1289
1289
                    else if (conv_restlen > 0)
1290
1290
                    {
1291
 
                        /* Reached end-of-file but some trailing bytes could
1292
 
                         * not be converted.  Truncated file? */
1293
 
                        if (conv_error == 0)
1294
 
                            conv_error = linecnt;
1295
 
                        if (bad_char_behavior != BAD_DROP)
1296
 
                        {
 
1291
                        /*
 
1292
                         * Reached end-of-file but some trailing bytes could
 
1293
                         * not be converted.  Truncated file?
 
1294
                         */
 
1295
 
 
1296
                        /* When we did a conversion report an error. */
 
1297
                        if (fio_flags != 0
 
1298
# ifdef USE_ICONV
 
1299
                                || iconv_fd != (iconv_t)-1
 
1300
# endif
 
1301
                           )
 
1302
                        {
 
1303
                            if (conv_error == 0)
 
1304
                                conv_error = curbuf->b_ml.ml_line_count
 
1305
                                                                - linecnt + 1;
 
1306
                        }
 
1307
                        /* Remember the first linenr with an illegal byte */
 
1308
                        else if (illegal_byte == 0)
 
1309
                            illegal_byte = curbuf->b_ml.ml_line_count
 
1310
                                                                - linecnt + 1;
 
1311
                        if (bad_char_behavior == BAD_DROP)
 
1312
                        {
 
1313
                            *(ptr - conv_restlen) = NUL;
 
1314
                            conv_restlen = 0;
 
1315
                        }
 
1316
                        else
 
1317
                        {
 
1318
                            /* Replace the trailing bytes with the replacement
 
1319
                             * character if we were converting; if we weren't,
 
1320
                             * leave the UTF8 checking code to do it, as it
 
1321
                             * works slightly differently. */
 
1322
                            if (bad_char_behavior != BAD_KEEP && (fio_flags != 0
 
1323
# ifdef USE_ICONV
 
1324
                                    || iconv_fd != (iconv_t)-1
 
1325
# endif
 
1326
                               ))
 
1327
                            {
 
1328
                                while (conv_restlen > 0)
 
1329
                                {
 
1330
                                    *(--ptr) = bad_char_behavior;
 
1331
                                    --conv_restlen;
 
1332
                                }
 
1333
                            }
1297
1334
                            fio_flags = 0;      /* don't convert this */
1298
1335
# ifdef USE_ICONV
1299
1336
                            if (iconv_fd != (iconv_t)-1)
1302
1339
                                iconv_fd = (iconv_t)-1;
1303
1340
                            }
1304
1341
# endif
1305
 
                            if (bad_char_behavior == BAD_KEEP)
1306
 
                            {
1307
 
                                /* Keep the trailing bytes as-is. */
1308
 
                                size = conv_restlen;
1309
 
                                ptr -= conv_restlen;
1310
 
                            }
1311
 
                            else
1312
 
                            {
1313
 
                                /* Replace the trailing bytes with the
1314
 
                                 * replacement character. */
1315
 
                                size = 1;
1316
 
                                *--ptr = bad_char_behavior;
1317
 
                            }
1318
 
                            conv_restlen = 0;
1319
1342
                        }
1320
1343
                    }
1321
1344
#endif
1397
1420
                    goto retry;
1398
1421
                }
1399
1422
            }
1400
 
#endif
1401
 
            /*
1402
 
             * Break here for a read error or end-of-file.
1403
 
             */
1404
 
            if (size <= 0)
1405
 
                break;
1406
 
 
1407
 
#ifdef FEAT_MBYTE
1408
1423
 
1409
1424
            /* Include not converted bytes. */
1410
1425
            ptr -= conv_restlen;
1411
1426
            size += conv_restlen;
1412
1427
            conv_restlen = 0;
 
1428
#endif
 
1429
            /*
 
1430
             * Break here for a read error or end-of-file.
 
1431
             */
 
1432
            if (size <= 0)
 
1433
                break;
 
1434
 
 
1435
#ifdef FEAT_MBYTE
1413
1436
 
1414
1437
# ifdef USE_ICONV
1415
1438
            if (iconv_fd != (iconv_t)-1)
1872
1895
                size = (long)((ptr + real_size) - dest);
1873
1896
                ptr = dest;
1874
1897
            }
1875
 
            else if (enc_utf8 && conv_error == 0 && !curbuf->b_p_bin)
 
1898
            else if (enc_utf8 && !curbuf->b_p_bin)
1876
1899
            {
1877
 
                /* Reading UTF-8: Check if the bytes are valid UTF-8.
1878
 
                 * Need to start before "ptr" when part of the character was
1879
 
                 * read in the previous read() call. */
1880
 
                for (p = ptr - utf_head_off(buffer, ptr); ; ++p)
 
1900
                int  incomplete_tail = FALSE;
 
1901
 
 
1902
                /* Reading UTF-8: Check if the bytes are valid UTF-8. */
 
1903
                for (p = ptr; ; ++p)
1881
1904
                {
1882
1905
                    int  todo = (int)((ptr + size) - p);
1883
1906
                    int  l;
1891
1914
                         * read() will get the next bytes, we'll check it
1892
1915
                         * then. */
1893
1916
                        l = utf_ptr2len_len(p, todo);
1894
 
                        if (l > todo)
 
1917
                        if (l > todo && !incomplete_tail)
1895
1918
                        {
1896
 
                            /* Incomplete byte sequence, the next read()
1897
 
                             * should get them and check the bytes. */
1898
 
                            p += todo;
1899
 
                            break;
 
1919
                            /* Avoid retrying with a different encoding when
 
1920
                             * a truncated file is more likely, or attempting
 
1921
                             * to read the rest of an incomplete sequence when
 
1922
                             * we have already done so. */
 
1923
                            if (p > ptr || filesize > 0)
 
1924
                                incomplete_tail = TRUE;
 
1925
                            /* Incomplete byte sequence, move it to conv_rest[]
 
1926
                             * and try to read the rest of it, unless we've
 
1927
                             * already done so. */
 
1928
                            if (p > ptr)
 
1929
                            {
 
1930
                                conv_restlen = todo;
 
1931
                                mch_memmove(conv_rest, p, conv_restlen);
 
1932
                                size -= conv_restlen;
 
1933
                                break;
 
1934
                            }
1900
1935
                        }
1901
 
                        if (l == 1)
 
1936
                        if (l == 1 || l > todo)
1902
1937
                        {
1903
1938
                            /* Illegal byte.  If we can try another encoding
1904
 
                             * do that. */
1905
 
                            if (can_retry)
 
1939
                             * do that, unless at EOF where a truncated
 
1940
                             * file is more likely than a conversion error. */
 
1941
                            if (can_retry && !incomplete_tail)
1906
1942
                                break;
1907
 
 
 
1943
# ifdef USE_ICONV
 
1944
                            /* When we did a conversion report an error. */
 
1945
                            if (iconv_fd != (iconv_t)-1 && conv_error == 0)
 
1946
                                conv_error = readfile_linenr(linecnt, ptr, p);
 
1947
# endif
1908
1948
                            /* Remember the first linenr with an illegal byte */
1909
 
                            if (illegal_byte == 0)
 
1949
                            if (conv_error == 0 && illegal_byte == 0)
1910
1950
                                illegal_byte = readfile_linenr(linecnt, ptr, p);
1911
 
# ifdef USE_ICONV
1912
 
                            /* When we did a conversion report an error. */
1913
 
                            if (iconv_fd != (iconv_t)-1 && conv_error == 0)
1914
 
                                conv_error = readfile_linenr(linecnt, ptr, p);
1915
 
# endif
1916
1951
 
1917
1952
                            /* Drop, keep or replace the bad byte. */
1918
1953
                            if (bad_char_behavior == BAD_DROP)
1919
1954
                            {
1920
 
                                mch_memmove(p, p+1, todo - 1);
 
1955
                                mch_memmove(p, p + 1, todo - 1);
1921
1956
                                --p;
1922
1957
                                --size;
1923
1958
                            }
1924
1959
                            else if (bad_char_behavior != BAD_KEEP)
1925
1960
                                *p = bad_char_behavior;
1926
1961
                        }
1927
 
                        p += l - 1;
 
1962
                        else
 
1963
                            p += l - 1;
1928
1964
                    }
1929
1965
                }
1930
 
                if (p < ptr + size)
 
1966
                if (p < ptr + size && !incomplete_tail)
1931
1967
                {
1932
1968
                    /* Detected a UTF-8 error. */
1933
1969
rewind_retry:
4316
4352
#endif
4317
4353
 
4318
4354
#ifdef HAVE_SELINUX
4319
 
    /* Probably need to set the security context */
 
4355
    /* Probably need to set the security context. */
4320
4356
    if (!backup_copy)
4321
4357
        mch_copy_sec(backup, wfname);
4322
4358
#endif