Commit 210ab85a authored by Avi Drissman's avatar Avi Drissman Committed by Commit Bot

Fix type punning in font_matcher_mac

It is undefined behavior in C++ to access memory of one type with
a pointer of another type. Use bit_cast to safely do it.

Bug: none
Change-Id: I170c81e0cc7dc5b7bcf63b0776758c4c65a748bf
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2340180
Auto-Submit: Avi Drissman <avi@chromium.org>
Reviewed-by: default avatarDominik Röttsches <drott@chromium.org>
Commit-Queue: Avi Drissman <avi@chromium.org>
Cr-Commit-Position: refs/heads/master@{#796438}
parent 7817b3c5
......@@ -32,6 +32,8 @@
#import <AppKit/AppKit.h>
#import <Foundation/Foundation.h>
#import <math.h>
#include "base/bit_cast.h"
#include "base/mac/foundation_util.h"
#include "base/mac/scoped_cftyperef.h"
#include "base/mac/scoped_nsobject.h"
......@@ -59,17 +61,16 @@ static CGFloat toFontWeight(blink::FontSelectionValue font_weight) {
0x3fe3d70a40000000, // NSFontWeightBlack
};
if (font_weight <= 50 || font_weight >= 950)
return ns_font_weights[3];
return bit_cast<CGFloat>(ns_font_weights[3]);
size_t select_weight = roundf(font_weight / 100) - 1;
DCHECK_GE(select_weight, 0ul);
DCHECK_LE(select_weight, base::size(ns_font_weights));
CGFloat* return_weight =
reinterpret_cast<CGFloat*>(&ns_font_weights[select_weight]);
return *return_weight;
}
return bit_cast<CGFloat>(ns_font_weights[select_weight]);
}
} // namespace
namespace blink {
const NSFontTraitMask SYNTHESIZED_FONT_TRAITS =
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment