freetype-commit
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[freetype2-demos] master a30f8a0 04/13: [ftinspect] Make `ftcFaceID' con


From: Werner LEMBERG
Subject: [freetype2-demos] master a30f8a0 04/13: [ftinspect] Make `ftcFaceID' conversion safe.
Date: Thu, 11 May 2017 15:26:23 -0400 (EDT)

branch: master
commit a30f8a0310a81676335ae22a6cba4f27875dc7e5
Author: Philipp Kerling <address@hidden>
Commit: Werner Lemberg <address@hidden>

    [ftinspect] Make `ftcFaceID' conversion safe.
    
    * src/ftinspect.cpp: Include `stdint.h'.
    (faceRequester): Implement it.
---
 ChangeLog         |  7 +++++++
 src/ftinspect.cpp | 17 +++++++++++++----
 2 files changed, 20 insertions(+), 4 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index c80ccf8..9c5c0ef 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,12 @@
 2017-05-11  Philipp Kerling  <address@hidden>
 
+       [ftinspect] Make `ftcFaceID' conversion safe.
+
+       * src/ftinspect.cpp: Include `stdint.h'.
+       (faceRequester): Implement it.
+
+2017-05-11  Philipp Kerling  <address@hidden>
+
        [ftinspect] Synchronize dpi, target, pxlMode types with FreeType.
 
        * src/ftinspect.h (Engine): Make `dpi' unsigned int.
diff --git a/src/ftinspect.cpp b/src/ftinspect.cpp
index a0305a3..21a43f6 100644
--- a/src/ftinspect.cpp
+++ b/src/ftinspect.cpp
@@ -4,6 +4,7 @@
 
 #include "ftinspect.h"
 
+#include <stdint.h>
 #include <cstdint>
 #include <cmath>
 #include <limits>
@@ -76,9 +77,17 @@ faceRequester(FTC_FaceID ftcFaceID,
               FT_Face* faceP)
 {
   MainGUI* gui = static_cast<MainGUI*>(requestData);
-  // in C++ it's tricky to convert a void pointer back to an integer
-  // without warnings related to 32bit vs. 64bit pointer size
-  int val = static_cast<int>((char*)ftcFaceID - (char*)0);
+
+  // `ftcFaceID' is actually an integer
+  // -> first convert pointer to same-width integer, then discard superfluous
+  //    bits (e.g., on x86_64 where pointers are wider than int)
+  int val = static_cast<int>(reinterpret_cast<intptr_t>(ftcFaceID));
+  // make sure this does not cause information loss
+  Q_ASSERT_X(sizeof(void*) >= sizeof(int),
+             "faceRequester",
+             "Pointer size must be at least the size of int"
+             " in order to treat FTC_FaceID correctly");
+
   const FaceID& faceID = gui->engine->faceIDMap.key(val);
 
   // this is the only place where we have to check the validity of the font
@@ -536,7 +545,7 @@ void
 Engine::update()
 {
   // Spinbox value cannot become negative
-  dpi = static_cast<unsigned int> (gui->dpiSpinBox->value());
+  dpi = static_cast<unsigned int>(gui->dpiSpinBox->value());
 
   if (gui->unitsComboBox->currentIndex() == MainGUI::Units_px)
   {



reply via email to

[Prev in Thread] Current Thread [Next in Thread]