mirror of
https://bitbucket.org/chromiumembedded/cef
synced 2025-06-05 21:39:12 +02:00
Check C API structure sizes before copying values to C++ classes (fixes issue #3238)
This commit is contained in:
committed by
Marshall Greenblatt
parent
db9298fd3e
commit
5c0895e27f
37
libcef_dll/template_util.h
Normal file
37
libcef_dll/template_util.h
Normal file
@@ -0,0 +1,37 @@
|
||||
// Copyright (c) 2022 The Chromium Embedded Framework Authors. All rights
|
||||
// reserved. Use of this source code is governed by a BSD-style license that
|
||||
// can be found in the LICENSE file.
|
||||
|
||||
#ifndef CEF_LIBCEF_DLL_TEMPLATE_UTIL_H_
|
||||
#define CEF_LIBCEF_DLL_TEMPLATE_UTIL_H_
|
||||
#pragma once
|
||||
|
||||
#include <type_traits>
|
||||
|
||||
namespace template_util {
|
||||
|
||||
// Used to detect whether the given C struct has a size_t size field or has a
|
||||
// base field and a base field has a size field.
|
||||
template <typename T, typename = void>
|
||||
struct HasValidSize {
|
||||
bool operator()(const T*) { return true; }
|
||||
};
|
||||
template <typename T>
|
||||
struct HasValidSize<
|
||||
T,
|
||||
typename std::enable_if_t<std::is_same<decltype(T::size), size_t>::value>> {
|
||||
bool operator()(const T* s) { return s->size == sizeof(*s); }
|
||||
};
|
||||
template <typename T>
|
||||
struct HasValidSize<T, decltype(void(T::base.size))> {
|
||||
bool operator()(const T* s) { return s->base.size == sizeof(*s); }
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
inline bool has_valid_size(const T* s) {
|
||||
return HasValidSize<T>()(s);
|
||||
}
|
||||
|
||||
} // namespace template_util
|
||||
|
||||
#endif // CEF_LIBCEF_DLL_TEMPLATE_UTIL_H_
|
Reference in New Issue
Block a user