Bug 225335

Summary: Add bounds checks around calls to GlyphBuffer::stringOffsetAt()
Product: WebKit Reporter: Myles C. Maxfield <mmaxfield>
Component: New BugsAssignee: Myles C. Maxfield <mmaxfield>
Status: RESOLVED FIXED    
Severity: Normal CC: cgarcia, ggaren, lmoura, simon.fraser, webkit-bug-importer
Priority: P2 Keywords: InRadar
Version: WebKit Nightly Build   
Hardware: Unspecified   
OS: Unspecified   
See Also: https://bugs.webkit.org/show_bug.cgi?id=225642
Attachments:
Description Flags
Patch
ews-feeder: commit-queue-
Patch
simon.fraser: review+
Patch for committing
ews-feeder: commit-queue-
Patch for committing none

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)                                                                                                                                   
      |