Skip to content

Conversation

@hamishknight
Copy link
Contributor

@hamishknight hamishknight commented Dec 7, 2025

Make sure we don't scan off the end of the buffer, and scan for the delimiter before attempting the lookahead.

rdar://165774720

Make sure we don't scan off the end of the buffer, and scan for the
delimiter before attempting the lookahead.
@hamishknight
Copy link
Contributor Author

@swift-ci please test

@hamishknight
Copy link
Contributor Author

@swift-ci please SourceKit stress test

const char *TmpPtr = CurPtr;
if (*(TmpPtr - 1) == '"' &&
diagnoseZeroWidthMatchAndAdvance('"', TmpPtr, Diags) &&
diagnoseZeroWidthMatchAndAdvance('"', TmpPtr, Diags)) {
Copy link
Member

@rintaro rintaro Dec 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No check for EndPtr because we guarantee *EndPtr is 0?
But maybe we could early check like:

if (CurPtr + 2 >= EndPtr) return false;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah exactly, e.g delimiterMatches does

  while (diagnoseZeroWidthMatchAndAdvance('#', TmpPtr, Diags)) {}

I don't mind adding an extra check though

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. Considering delimiterMatches does that, I don't think extra check is needed.

const char *&CurPtr, const char *EndPtr,
DiagnosticEngine *Diags,
bool IsOpening = false) {
auto scanDelimiter = [&]() -> const char * {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

scanDelimiter is used only once. Why not

const char *DelimEnd = nullptr;
{
    // CurPtr here points to the character after `"`.
    const char *TmpPtr = CurPtr;
    if (*(TmpPtr - 1) == '"' &&
        diagnoseZeroWidthMatchAndAdvance('"', TmpPtr, Diags) &&
        diagnoseZeroWidthMatchAndAdvance('"', TmpPtr, Diags)) {
      DelimEnd = TmpPtr;
    } else {
      return false;
    }
}

If it's just a matter of the code style, I'm fine with the lambda.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah personally I prefer the lambda, shame C++ doesn't have guard statements 😄

Copy link
Member

@rintaro rintaro Dec 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or just like swift-syntax counterpart

// CurPtr here points to the character after `"`.
const char *DelimEnd = CurPtr;
if (*(DelimEnd - 1) == '"' &&
    diagnoseZeroWidthMatchAndAdvance('"', DelimEnd, Diags) &&
    diagnoseZeroWidthMatchAndAdvance('"', DelimEnd, Diags)) {
 // Found the delimiter
} else {
  return false;
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An empty if body feels weird to me, I don't mind changing it though

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay. I'm fine with as-is

@hamishknight hamishknight merged commit 20bee09 into swiftlang:main Dec 9, 2025
5 of 6 checks passed
@hamishknight hamishknight deleted the overrun branch December 9, 2025 20:26
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.

2 participants