Skip to content

Fix crash: guard DrawingContext against a null image buffer (SIGSEGV)#2217

Open
ankuper wants to merge 1 commit into
TelegramMessenger:masterfrom
ankuper:fix/drawing-context-null-buffer
Open

Fix crash: guard DrawingContext against a null image buffer (SIGSEGV)#2217
ankuper wants to merge 1 commit into
TelegramMessenger:masterfrom
ankuper:fix/drawing-context-null-buffer

Conversation

@ankuper

@ankuper ankuper commented Jul 2, 2026

Copy link
Copy Markdown

Why

ASCGImageBuffer.initWithLength: calls malloc(length) with no null check. When length is oversized (e.g. from a bad drawingSize computed during chat photo layout), malloc can return NULL.

CGContext(data: nil, ...) inside DrawingContext.init silently succeeds anyway — CoreGraphics falls back to its own internally-managed buffer, masking the allocation failure. The later memset(self.bytes, 0, self.length) in the same initializer then writes through the still-null imageBuffer.mutableBytes pointer and segfaults.

Confirmed via an on-device crash report: ESR = Data Abort, byte write Translation fault, FAR (fault address) = 0x0, with __bzero and ASCGImageBuffer visible in nearby registers — a real device crash while scrolling a chat with photos.

Fix

Fail the initializer gracefully (return nil) as soon as the buffer allocation comes back null, matching the other guarded failure paths already present in this same initializer (non-positive size, CGContext creation failure). Callers of DrawingContext.init already handle a nil result throughout the codebase.

Testing

Verified against the crash report that triggered this (register state showed a null-pointer write inside memset/ASCGImageBuffer). Built and ran on-device (iPhone, iOS 26.5) across normal chat/photo usage after the fix; no functional regression, no follow-up allocation-related crashes observed.

ASCGImageBuffer.initWithLength: calls malloc(length) with no null check.
When length is oversized (e.g. from a bad drawingSize during chat photo
layout), malloc can return NULL. CGContext(data: nil, ...) silently
succeeds anyway — CoreGraphics falls back to its own internally-managed
buffer, masking the failure. The later memset(self.bytes, 0, self.length)
in DrawingContext.init then writes through the null imageBuffer pointer
and segfaults (confirmed via crash report: ESR = Data Abort byte write
Translation fault, FAR = 0x0, __bzero on the stack, ASCGImageBuffer
visible in nearby registers — device crash while scrolling chats,
build 33193).

Fail the init gracefully (return nil) as soon as the buffer comes back
null, matching the other guarded failure paths already in this
initializer (non-positive size, CGContext creation failure).
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