Set correct restype for ca_element_count#51
Open
jacquelinegarrahan wants to merge 1 commit into
Open
Conversation
The C signature is "unsigned long ca_element_count(chid)" (cadef.h), but no restype was declared, so ctypes defaulted to c_int (32-bit signed). On LP64 platforms (Linux/macOS) an element count that does not fit in a signed 32-bit int is read from only the low 32 bits, yielding a wrong or negative count; if those low bits happen to be zero, the expect_connected(0) errcheck would also falsely raise Disconnected. Declare restype = ctypes.c_ulong to match the C return type, consistent with the explicit restype already set on its sibling ca_field_type. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Set correct
restypeforca_element_countBranch:
fix/ca-element-count-restype→masterSummary
src/python/epicscorelibs/ca/cadef.pydeclares the ctypes binding forca_element_countbut never sets itsrestype:The C signature (verified in
modules/ca/src/client/cadef.h:227) is:With no
restype, ctypes defaults toc_int(32-bit signed). On LP64platforms (Linux/macOS), where
unsigned longis 64-bit, a return value thatdoesn't fit in a signed 32-bit int is read from only the low 32 bits — giving a
wrong or negative count. If those low 32 bits happen to be zero, the
expect_connected(0)errcheck would additionally raise a spuriousDisconnected.The sibling binding
ca_field_typesets an explicitrestype; this one wassimply overlooked.
Fix
Add
ca_element_count.restype = ctypes.c_ulong(matching the Cunsigned long).Verification
cadef.pybyte-compiles. The truncation is demonstrated independently with alibc
unsigned longfunction, which reproduces the exact defect and the fix:Risk
Very low. For the common case of small element counts (< 2^31) behavior is
identical; the fix only changes the previously-wrong result for pathologically
large arrays and removes a latent sign/truncation hazard on 64-bit hosts.