Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix null termination for generated marshalling of read-only ref string #59726

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,9 @@ protected override StatementSyntax GenerateStackallocOnlyValueMarshalling(
SyntaxToken byteLengthIdentifier,
SyntaxToken stackAllocPtrIdentifier)
{
// ((ReadOnlySpan<char>)<managed>).CopyTo(new Span<char>(<stackAllocPtr>, <managed>.Length + 1));
return
string managedIdentifier = context.GetIdentifiers(info).managed;
return Block(
// ((ReadOnlySpan<char>)<managed>).CopyTo(new Span<char>(<stackAllocPtr>, <managed>.Length));
ExpressionStatement(
InvocationExpression(
MemberAccessExpression(
Expand All @@ -186,7 +187,7 @@ protected override StatementSyntax GenerateStackallocOnlyValueMarshalling(
GenericName(Identifier("System.ReadOnlySpan"),
TypeArgumentList(SingletonSeparatedList<TypeSyntax>(
PredefinedType(Token(SyntaxKind.CharKeyword))))),
IdentifierName(context.GetIdentifiers(info).managed))),
IdentifierName(managedIdentifier))),
IdentifierName("CopyTo")),
ArgumentList(
SeparatedList(new[] {
Expand All @@ -198,8 +199,31 @@ protected override StatementSyntax GenerateStackallocOnlyValueMarshalling(
ArgumentList(
SeparatedList(new[]{
Argument(IdentifierName(stackAllocPtrIdentifier)),
Argument(IdentifierName(byteLengthIdentifier))})),
initializer: null))}))));
Argument(
MemberAccessExpression(
SyntaxKind.SimpleMemberAccessExpression,
IdentifierName(managedIdentifier),
IdentifierName("Length")))})),
initializer: null))})))),
// ((char*)<stackAllocPtr>)[<managed>.Length] = '\0';
ExpressionStatement(
AssignmentExpression(
SyntaxKind.SimpleAssignmentExpression,
ElementAccessExpression(
ParenthesizedExpression(
CastExpression(
PointerType(PredefinedType(Token(SyntaxKind.CharKeyword))),
IdentifierName(stackAllocPtrIdentifier))),
BracketedArgumentList(
SingletonSeparatedList<ArgumentSyntax>(
Argument(
MemberAccessExpression(
SyntaxKind.SimpleMemberAccessExpression,
IdentifierName(managedIdentifier),
IdentifierName("Length")))))),
LiteralExpression(
SyntaxKind.CharacterLiteralExpression,
Literal('\0')))));
}

protected override ExpressionSyntax GenerateFreeExpression(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public partial class SetLastError
[GeneratedDllImport(NativeExportsNE_Binary, EntryPoint = "set_error", SetLastError = true)]
public static partial int SetError(int error, byte shouldSetError);

[GeneratedDllImport(NativeExportsNE_Binary, EntryPoint = "set_error_return_string", SetLastError = true)]
[GeneratedDllImport(NativeExportsNE_Binary, EntryPoint = "set_error", SetLastError = true)]
[return: MarshalUsing(typeof(SetLastErrorMarshaller))]
public static partial int SetError_CustomMarshallingSetsError(int error, byte shouldSetError);

Expand Down