diff --git a/src/Microsoft.Windows.CsWin32/Generator.FriendlyOverloads.cs b/src/Microsoft.Windows.CsWin32/Generator.FriendlyOverloads.cs index 461194ea..2d300582 100644 --- a/src/Microsoft.Windows.CsWin32/Generator.FriendlyOverloads.cs +++ b/src/Microsoft.Windows.CsWin32/Generator.FriendlyOverloads.cs @@ -423,12 +423,14 @@ private IEnumerable DeclareFriendlyOverloads(MethodDefi // wstrParam1 arguments[param.SequenceNumber - 1] = Argument(localWstrName); - // if (buffer.LastIndexOf('\0') == -1) throw new ArgumentException("Required null terminator is missing.", "Param1"); + // if (buffer != null && buffer.LastIndexOf('\0') == -1) throw new ArgumentException("Required null terminator is missing.", "Param1"); InvocationExpressionSyntax lastIndexOf = InvocationExpression( MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, origName, IdentifierName(nameof(MemoryExtensions.LastIndexOf))), ArgumentList().AddArguments(Argument(LiteralExpression(SyntaxKind.CharacterLiteralExpression, Literal('\0'))))); + ExpressionSyntax lastIndexOfEqualsMinusOne = BinaryExpression(SyntaxKind.EqualsExpression, lastIndexOf, LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal(-1))); + ExpressionSyntax bufferNotNull = BinaryExpression(SyntaxKind.NotEqualsExpression, origName, LiteralExpression(SyntaxKind.NullLiteralExpression)); leadingOutsideTryStatements.Add(IfStatement( - BinaryExpression(SyntaxKind.EqualsExpression, lastIndexOf, LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal(-1))), + BinaryExpression(SyntaxKind.LogicalAndExpression, bufferNotNull, lastIndexOfEqualsMinusOne), ThrowStatement(ObjectCreationExpression(IdentifierName(nameof(ArgumentException))).AddArgumentListArguments( Argument(LiteralExpression(SyntaxKind.StringLiteralExpression, Literal("Required null terminator missing."))), Argument(LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(externParam.Identifier.ValueText))))))); diff --git a/src/Microsoft.Windows.CsWin32/templates/PInvokeClassHelperMethods.cs b/src/Microsoft.Windows.CsWin32/templates/PInvokeClassHelperMethods.cs index 0faca926..33ff31df 100644 --- a/src/Microsoft.Windows.CsWin32/templates/PInvokeClassHelperMethods.cs +++ b/src/Microsoft.Windows.CsWin32/templates/PInvokeClassHelperMethods.cs @@ -5,7 +5,7 @@ internal class PInvokeClassHelperMethods { private static void EnsureNullTerminated(Span buffer, string parameterName) { - if (buffer.LastIndexOf('\0') == -1) + if (buffer != null && buffer.LastIndexOf('\0') == -1) { throw new ArgumentException("Required null terminator is missing.", parameterName); }