Bug 225335 - Add bounds checks around calls to GlyphBuffer::stringOffsetAt()
Summary: Add bounds checks around calls to GlyphBuffer::stringOffsetAt()
Status: RESOLVED FIXED
Alias: None
Product: WebKit
Classification: Unclassified
Component: New Bugs (show other bugs)
Version: WebKit Nightly Build
Hardware: Unspecified Unspecified
: P2 Normal
Assignee: Myles C. Maxfield
URL:
Keywords: InRadar
Depends on:
Blocks:
 
Reported: 2021-05-03 16:36 PDT by Myles C. Maxfield
Modified: 2021-05-11 01:24 PDT (History)
5 users (show)

See Also:


Attachments
Patch (2.63 KB, patch)
2021-05-03 17:07 PDT, Myles C. Maxfield
ews-feeder: commit-queue-
Details | Formatted Diff | Diff
Patch (2.67 KB, patch)
2021-05-03 17:11 PDT, Myles C. Maxfield
simon.fraser: review+
Details | Formatted Diff | Diff
Patch for committing (6.81 KB, patch)
2021-05-08 15:47 PDT, Myles C. Maxfield
ews-feeder: commit-queue-
Details | Formatted Diff | Diff
Patch for committing (6.83 KB, patch)
2021-05-08 15:52 PDT, Myles C. Maxfield
no flags Details | Formatted Diff | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Myles C. Maxfield 2021-05-03 16:36:04 PDT
Add bounds checks around calls to GlyphBuffer::stringOffsetAt()
Comment 1 Myles C. Maxfield 2021-05-03 17:07:08 PDT
Created attachment 427622 [details]
Patch
Comment 2 Myles C. Maxfield 2021-05-03 17:07:38 PDT
<rdar://problem/75663608>
Comment 3 Myles C. Maxfield 2021-05-03 17:11:48 PDT
Created attachment 427624 [details]
Patch
Comment 4 Geoffrey Garen 2021-05-03 18:54:46 PDT
r=me too
Comment 5 Geoffrey Garen 2021-05-03 19:01:54 PDT
Comment on attachment 427624 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=427624&action=review

> Source/WebCore/platform/graphics/WidthIterator.cpp:398
>          auto stringOffset = glyphBuffer.stringOffsetAt(i);

Given the observation that stringOffsetAt may return an invalid value, indicating that CoreText did not find the string offset we wanted:

(1) Can we just check for kCFNotFound (i.e., -1); that would be a lot more direct and clear. Do we benefit from this more abstract comparison here, and do we expect CoreText to return whacky values other than kCFNotFound for some reason?

(2) Can stringOffsetAt return an Optional<GlyphBufferStringOffset> in order to become self-documenting?

I don't think these questions block landing; but would be good to address.
Comment 6 Myles C. Maxfield 2021-05-08 15:24:47 PDT
Comment on attachment 427624 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=427624&action=review

>> Source/WebCore/platform/graphics/WidthIterator.cpp:398
>>          auto stringOffset = glyphBuffer.stringOffsetAt(i);
> 
> Given the observation that stringOffsetAt may return an invalid value, indicating that CoreText did not find the string offset we wanted:
> 
> (1) Can we just check for kCFNotFound (i.e., -1); that would be a lot more direct and clear. Do we benefit from this more abstract comparison here, and do we expect CoreText to return whacky values other than kCFNotFound for some reason?
> 
> (2) Can stringOffsetAt return an Optional<GlyphBufferStringOffset> in order to become self-documenting?
> 
> I don't think these questions block landing; but would be good to address.

(1) Given that these values can come from another framework, I think we should be defensive.

(2) It can! Good idea.
Comment 7 Myles C. Maxfield 2021-05-08 15:47:41 PDT
Created attachment 428094 [details]
Patch for committing
Comment 8 Myles C. Maxfield 2021-05-08 15:52:47 PDT
Created attachment 428095 [details]
Patch for committing
Comment 9 EWS 2021-05-08 17:14:09 PDT
Committed r277232 (237501@main): <https://commits.webkit.org/237501@main>

All reviewed patches have been landed. Closing bug and clearing flags on attachment 428095 [details].
Comment 10 Lauro Moura 2021-05-09 17:42:14 PDT
Comment on attachment 428095 [details]
Patch for committing

View in context: https://bugs.webkit.org/attachment.cgi?id=428095&action=review

> Source/WebCore/platform/graphics/GlyphBuffer.h:81
> +        if (result < 0 || static_cast<unsigned>(result) >= stringLength)

This is raising -Wtype-limits warnings on non-CG platforms due to GlyphBufferStringOffset being 'unsigned' there. What would be the proper way to silence them? Maybe move the result<0 comparison to a USE(CG) block?

Example:

In file included from WebCore/PrivateHeaders/WebCore/Font.h:30,                                                                                                                                                    
                 from WebCore/PrivateHeaders/WebCore/FontCascade.h:28,                                                                                                                                             
                 from WebCore/PrivateHeaders/WebCore/GraphicsContext.h:32,                                                                                                                                         
                 from WebCore/PrivateHeaders/WebCore/FrameView.h:31,                                                                                                                                               
                 from ../../Source/WebKit/WebProcess/InjectedBundle/API/glib/WebKitWebPage.cpp:56:                                                                                                                 
WebCore/PrivateHeaders/WebCore/GlyphBuffer.h: In member function ‘WTF::Optional<unsigned int> WebCore::GlyphBuffer::checkedStringOffsetAt(unsigned int, unsigned int) const’:                                      
WebCore/PrivateHeaders/WebCore/GlyphBuffer.h:81:20: warning: comparison of unsigned expression in ‘< 0’ is always false [-Wtype-limits]                                                                            
   81 |         if (result < 0 || static_cast<unsigned>(result) >= stringLength)                                                                                                                                   
      |