servo: Fix segfault in QuartzNativeFont.create; improve error handling slightly
authorBrian J. Burg <burg@cs.washington.edu>
Mon, 10 Sep 2012 22:35:17 -0700
changeset 361894 9120594508f27e8b6fcd49a0b74224b41fc281eb
parent 361893 966a38a16636fa4fbfde45548b7eecda61f77337
child 361895 76a460591e0b24e6444a1d9f732fc9e4ada39e85
push id10863
push userjlorenzo@mozilla.com
push dateMon, 06 Mar 2017 23:02:23 +0000
treeherdermozilla-aurora@0931190cd725 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
servo: Fix segfault in QuartzNativeFont.create; improve error handling slightly Source-Repo: https://github.com/servo/servo Source-Revision: 1292fa29658e24d96cc146ebc4fc05a26de6b88e
servo/src/servo/text/native_font/quartz_native_font.rs
--- a/servo/src/servo/text/native_font/quartz_native_font.rs
+++ b/servo/src/servo/text/native_font/quartz_native_font.rs
@@ -1,26 +1,26 @@
 use cocoa;
 
 export QuartzNativeFont, with_test_native_font, create;
 
 import libc::size_t;
 import ptr::null;
-import unsafe::reinterpret_cast;
 import glyph::GlyphIndex;
 import cocoa::cg::{
     CGDataProviderRef,
     CGFontRef
 };
 import cocoa::cg::cg::{
     CGDataProviderCreateWithData,
     CGDataProviderRelease,
     CGFontCreateWithDataProvider,
     CGFontRelease
 };
+use unsafe::transmute;
 
 mod coretext {
 
     type CTFontRef = *u8;
     type UniChar = libc::c_ushort;
     type CGGlyph = libc::c_ushort;
     type CFIndex = libc::c_long;
 
@@ -44,17 +44,17 @@ mod coretext {
     extern mod coretext {
         fn CTFontCreateWithGraphicsFont(graphicsFont: CGFontRef, size: CGFloat, matrix: *CGAffineTransform, attributes: CTFontDescriptorRef) -> CTFontRef;
         fn CTFontGetGlyphsForCharacters(font: CTFontRef, characters: *UniChar, glyphs: *CGGlyph, count: CFIndex) -> bool;
         fn CTFontGetAdvancesForGlyphs(font: CTFontRef, orientation: CTFontOrientation, glyphs: *CGGlyph, advances: *CGSize, count: CFIndex) -> libc::c_double;
         fn CFRelease(font: CTFontRef);
     }
 }
 
-struct QuartzNativeFont/& {
+struct QuartzNativeFont {
     fontprov: CGDataProviderRef,
     cgfont: CGFontRef,
 
     drop {
         assert self.cgfont.is_not_null();
         assert self.fontprov.is_not_null();
 
         CGFontRelease(self.cgfont);
@@ -72,17 +72,17 @@ fn QuartzNativeFont(fontprov: CGDataProv
     }
 }
 
 impl QuartzNativeFont {
     fn glyph_index(codepoint: char) -> Option<GlyphIndex> {
 
         import coretext::{UniChar, CGGlyph, CFIndex};
         import coretext::coretext::{CFRelease, CTFontGetGlyphsForCharacters};
-         
+
         let ctfont = ctfont_from_cgfont(self.cgfont);
         assert ctfont.is_not_null();
         let characters: ~[UniChar] = ~[codepoint as UniChar];
         let glyphs: ~[mut CGGlyph] = ~[mut 0 as CGGlyph];
         let count: CFIndex = 1;
 
         let result = do vec::as_buf(characters) |character_buf, _l| {
             do vec::as_buf(glyphs) |glyph_buf, _l| {
@@ -114,39 +114,41 @@ impl QuartzNativeFont {
         };
 
         CFRelease(ctfont);
 
         return Some(advance as int);
     }
 }
 
-fn ctfont_from_cgfont(+cgfont: CGFontRef) -> coretext::CTFontRef unsafe {
+fn ctfont_from_cgfont(cgfont: CGFontRef) -> coretext::CTFontRef {
     import coretext::CGFloat;
     import coretext::coretext::CTFontCreateWithGraphicsFont;
 
     assert cgfont.is_not_null();
     CTFontCreateWithGraphicsFont(cgfont, 21f as CGFloat, null(), null())
 }
 
 fn create(buf: &~[u8]) -> Result<QuartzNativeFont, ()> {
     let fontprov = vec::as_buf(*buf, |cbuf, len| {
         CGDataProviderCreateWithData(
             null(),
-            unsafe { reinterpret_cast(&cbuf) },
+            unsafe { transmute(&cbuf) },
             len as size_t,
             null())
     });
     // FIXME: Error handling
     assert fontprov.is_not_null();
     let cgfont = CGFontCreateWithDataProvider(fontprov);
-    // FIXME: Error handling
-    assert cgfont.is_not_null();
 
-    return Ok(QuartzNativeFont(fontprov, cgfont));
+    match cgfont.is_not_null() {
+        true => Ok(QuartzNativeFont(fontprov, cgfont)),
+        false => Err(())
+    }
+    
 }
 
 fn with_test_native_font(f: fn@(nf: &NativeFont)) {
     import font::test_font_bin;
     import unwrap_result = result::unwrap;
 
     let buf = test_font_bin();
     let res = create(&buf);