Skip to content

fix(osx): consolidate error checks in alt_clock_gettime for clarity #4803

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

zqw86713
Copy link

@zqw86713 zqw86713 commented Jul 5, 2025

Summary

Fixes an issue in alt_clock_gettime() where the return value of host_get_clock_service() and clock_get_time() was not checked. This could lead to undefined behavior or crashes on macOS if the system call fails.

Problem

The original implementation assumes host_get_clock_service() and clock_get_time() always succeed:

host_get_clock_service (mach_host_self (), clock_id, &cclock);
clock_get_time (cclock, &mts);

However, these system calls can fail under certain conditions (e.g., lack of privileges or system resource issues). Without checking their return values, the subsequent use of uninitialized mts can lead to undefined behavior.

Fix

This PR introduces proper error handling:

  • Checks the return value of host_get_clock_service()
  • Checks the return value of clock_get_time()
  • Returns -1 on failure, consistent with typical POSIX time functions

Changes

  • Added error checks for host_get_clock_service() and clock_get_time() in alt_clock_gettime()
  • Preserved the original behavior when calls succeed
  • Ensured mach_port_deallocate() is always called after host_get_clock_service()

Platforms Affected

  • macOS only
  • Specifically affects macOS < 10.12 using alt_clock_gettime()

Testing

  • Manual verification on macOS 10.11
  • Confirmed the fallback path works correctly when system call fails (simulated)

Related Issues

N/A — this is a proactive fix identified during code audit

Checklist

  • Code compiles correctly
  • No new compiler warnings
  • No breaking API changes
  • Comments added where needed
  • Change is scoped to macOS-specific logic

@zqw86713 zqw86713 changed the title fix(osx): consolidate error checks in alt_clock_gettime for clarity fix(osx): consolidate error checks in alt_clock_gettime for clarity Jul 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant