Skip to content

Commit

Permalink
ios: Remove duplicate implementations of ReadStringSysctlByName
Browse files Browse the repository at this point in the history
Bug: crashpad: 480
Change-Id: Ie37c557d2170f6d96968ec4922ec52bfc6ad8136
Reviewed-on: https://chromium-review.googlesource.com/c/crashpad/crashpad/+/5580854
Reviewed-by: Mark Mentovai <mark@chromium.org>
  • Loading branch information
Justin Cohen committed Jun 4, 2024
1 parent cd0b7c2 commit 0bebda6
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 36 deletions.
17 changes: 5 additions & 12 deletions test/ios/crash_type_xctest.mm
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@
#include "build/build_config.h"
#include "client/length_delimited_ring_buffer.h"
#import "test/ios/host/cptest_shared_object.h"
#include "util/mac/sysctl.h"
#include "util/mach/exception_types.h"
#include "util/mach/mach_extensions.h"

namespace crashpad {
namespace {

#if TARGET_OS_SIMULATOR
Expand All @@ -36,22 +38,13 @@ bool IsMacOSVersion143OrGreaterAndiOS16OrLess() {
return false;
}

size_t buf_len;
static constexpr char name[] = "kern.osversion";
if (sysctlbyname(name, nullptr, &buf_len, nullptr, 0) != 0) {
return false;
}

std::string build(buf_len - 1, '\0');
if (sysctlbyname(name, &build[0], &buf_len, nullptr, 0) != 0) {
return false;
}

std::string build = crashpad::ReadStringSysctlByName("kern.osversion", false);
return std::stoi(build.substr(0, 2)) > 22;
}
#endif

} // namespace
} // namespace crashpad

@interface CPTestTestCase : XCTestCase {
XCUIApplication* app_;
Expand Down Expand Up @@ -352,7 +345,7 @@ - (void)testCrashWithAnnotations {
#if TARGET_OS_SIMULATOR
// This test will fail on older (<iOS17 simulators) when running on macOS 14.3
// or newer due to a bug in Simulator. crbug.com/328282286
if (IsMacOSVersion143OrGreaterAndiOS16OrLess()) {
if (crashpad::IsMacOSVersion143OrGreaterAndiOS16OrLess()) {
return;
}
#endif
Expand Down
4 changes: 2 additions & 2 deletions util/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,8 @@ crashpad_static_library("util") {

if (crashpad_is_apple) {
sources += [
"mac/sysctl.cc",
"mac/sysctl.h",
"mac/xattr.cc",
"mac/xattr.h",
"mach/composite_mach_message_server.cc",
Expand Down Expand Up @@ -349,8 +351,6 @@ crashpad_static_library("util") {
"mac/mac_util.h",
"mac/service_management.cc",
"mac/service_management.h",
"mac/sysctl.cc",
"mac/sysctl.h",
"mach/bootstrap.cc",
"mach/bootstrap.h",
"mach/child_port_handshake.cc",
Expand Down
25 changes: 3 additions & 22 deletions util/ios/ios_system_data_collector.mm
Original file line number Diff line number Diff line change
Expand Up @@ -27,30 +27,11 @@
#include "base/strings/stringprintf.h"
#include "base/strings/sys_string_conversions.h"
#include "build/build_config.h"
#include "util/mac/sysctl.h"
#include "util/misc/clock.h"

namespace {

std::string ReadStringSysctlByName(const char* name) {
size_t buf_len;
if (sysctlbyname(name, nullptr, &buf_len, nullptr, 0) != 0) {
PLOG(WARNING) << "sysctlbyname (size) " << name;
return std::string();
}

if (buf_len == 0) {
return std::string();
}

std::string value(buf_len - 1, '\0');
if (sysctlbyname(name, &value[0], &buf_len, nullptr, 0) != 0) {
PLOG(WARNING) << "sysctlbyname " << name;
return std::string();
}

return value;
}

template <typename T, void (T::*M)(void)>
void AddObserver(CFStringRef notification_name, T* observer) {
CFNotificationCenterAddObserver(
Expand Down Expand Up @@ -97,7 +78,7 @@ void AddObserver(CFStringRef notification_name, T* observer) {
patch_version_ = base::saturated_cast<int>(version.patchVersion);
processor_count_ =
base::saturated_cast<int>([[NSProcessInfo processInfo] processorCount]);
build_ = ReadStringSysctlByName("kern.osversion");
build_ = ReadStringSysctlByName("kern.osversion", false);
bundle_identifier_ =
base::SysNSStringToUTF8([[NSBundle mainBundle] bundleIdentifier]);
// If CRASHPAD_IS_IOS_APP_EXTENSION is defined, then the code is compiled with
Expand All @@ -110,7 +91,7 @@ void AddObserver(CFStringRef notification_name, T* observer) {
#endif

#if defined(ARCH_CPU_X86_64)
cpu_vendor_ = ReadStringSysctlByName("machdep.cpu.vendor");
cpu_vendor_ = ReadStringSysctlByName("machdep.cpu.vendor", false);
#endif
uint32_t addressable_bits = 0;
size_t len = sizeof(uint32_t);
Expand Down

0 comments on commit 0bebda6

Please sign in to comment.