diff options
Diffstat (limited to 'libcpp/charset.c')
-rw-r--r-- | libcpp/charset.c | 30 |
1 files changed, 26 insertions, 4 deletions
diff --git a/libcpp/charset.c b/libcpp/charset.c index abb5c8d4da4..485e838c937 100644 --- a/libcpp/charset.c +++ b/libcpp/charset.c @@ -1318,13 +1318,19 @@ cpp_interpret_string (cpp_reader *pfile, const cpp_string *from, size_t count, struct _cpp_strbuf tbuf; const uchar *p, *base, *limit; size_t i; + /* APPLE LOCAL begin pascal strings */ + size_t width = CPP_OPTION (pfile, wchar_precision); + size_t cwidth = CPP_OPTION (pfile, char_precision); + size_t pascal_string_max_length = width_to_mask (wide ? width : cwidth); + size_t pascal_string_length_byte_size = ((wide ? width : cwidth)/cwidth); + /* APPLE LOCAL end pascal strings */ struct cset_converter cvt = wide ? pfile->wide_cset_desc : pfile->narrow_cset_desc; tbuf.asize = MAX (OUTBUF_BLOCK_SIZE, from->len); tbuf.text = xmalloc (tbuf.asize); /* APPLE LOCAL pascal strings */ - tbuf.len = (pascal_p ? 1 : 0); /* Reserve space for Pascal length byte. */ + tbuf.len = (pascal_p ? pascal_string_length_byte_size : 0); /* Reserve space for Pascal length byte. */ for (i = 0; i < count; i++) { @@ -1363,9 +1369,25 @@ cpp_interpret_string (cpp_reader *pfile, const cpp_string *from, size_t count, /* For Pascal strings, compute the length byte. */ if (pascal_p) { - *tbuf.text = (unsigned char) (tbuf.len - 1); - if (tbuf.len > 256) - cpp_error (pfile, CPP_DL_ERROR, "Pascal string is too long"); + if (wide) + { + /* Conversion routine uses tbuf.len as the starting point in destination + buffer. However we are adding string lenght at the beginning. Save tbuf.len + and restore it later. */ + size_t saved_tbuf_len = tbuf.len; + unsigned char uclen = (unsigned char) (saved_tbuf_len/pascal_string_length_byte_size - 1); + tbuf.len = 0; + APPLY_CONVERSION (cvt, &uclen, 1, &tbuf); + tbuf.len = saved_tbuf_len; + if (tbuf.len/pascal_string_length_byte_size > pascal_string_max_length) + cpp_error (pfile, CPP_DL_ERROR, "Pascal string is too long"); + } + else + { + *tbuf.text = (unsigned char) (tbuf.len - 1); + if (tbuf.len > 256) + cpp_error (pfile, CPP_DL_ERROR, "Pascal string is too long"); + } } /* APPLE LOCAL end pascal strings */ |