]> git.lyx.org Git - features.git/commitdiff
Fix font_metrics::width crashing on strings it doesn't understand
authorJohn Spray <spray@lyx.org>
Wed, 3 Aug 2005 07:47:54 +0000 (07:47 +0000)
committerJohn Spray <spray@lyx.org>
Wed, 3 Aug 2005 07:47:54 +0000 (07:47 +0000)
git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@10386 a592a061-630c-0410-9148-cb99ea01b6c8

src/frontends/gtk/ChangeLog
src/frontends/gtk/xftFontMetrics.C

index c4d4ea96adbb8d46391cc84e99237c5a13715ba8..96fdac833c0fff9871112251ef7981b2efacff1c 100644 (file)
@@ -1,3 +1,8 @@
+2005-07-02  John Spray  <spray@lyx.org>
+       * xftFontMetrics.C: Add error checking to call to mbstowcs 
+       in font_metrics::width, estimate width for strings that  
+       mbstowcs doesn't like.
+
 2005-07-31  John Spray  <spray_john@users.sf.net>
        * GToc.C: unbreak viewing lists other than TOC by storing 
        integer index of entries in Type combobox.
index 8000beade67382b85f8af4901de52e550b6b87ed..694906c10bac275ec59d449eec15a2d3f7006524 100644 (file)
@@ -27,6 +27,7 @@
 #include "codeConvert.h"
 
 #include "support/lstrings.h"
+#include "debug.h"
 
 #include <gtkmm.h>
 
@@ -225,15 +226,20 @@ int width(wchar_t c,LyXFont const & f)
 int width(char const * s, size_t n, LyXFont const & f)
 {
        boost::scoped_array<wchar_t> wcs(new wchar_t[n]);
-       size_t len;
+       int len; // Signed to handle error retvals
        if (fontLoader.isSpecial(f)) {
                unsigned char const * us =
                        reinterpret_cast<unsigned char const *>(s);
                len = n;
                std::copy(us, us + n, wcs.get());
-       } else
+       } else {
                len = mbstowcs(wcs.get(), s, n);
-       return width(wcs.get(), len, f);
+               if (len < 0) {
+                       lyxerr[Debug::FONT] << "Invalid multibyte encoding! '" << s << "'\n";
+                       return n * width("0", 1, f);
+               }
+       }
+       return width(wcs.get(), static_cast<size_t>(len), f);
 }