Bug 20674 - Marshalling structs with strings fails
Summary: Marshalling structs with strings fails
Alias: None
Product: Runtime
Classification: Mono
Component: Interop ()
Version: 3.2.x
Hardware: PC All
: --- normal
Target Milestone: ---
Assignee: Bugzilla
Depends on:
Reported: 2014-06-18 06:46 UTC by Martin Green
Modified: 2014-08-05 10:30 UTC (History)
5 users (show)

Is this bug a regression?: ---
Last known good build:

Project with a few simple tests for this issue (5.79 KB, application/x-zip-compressed)
2014-06-18 06:46 UTC, Martin Green

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description Martin Green 2014-06-18 06:46:22 UTC
Created attachment 7104 [details]
Project with a few simple tests for this issue

Description of Problem:
Marshalling structs with strings fails when there is no null terminator on the string.

Steps to reproduce the problem:
1. Create a struct that contains a string set to MarshalAs ByValTStr.
2. Create a buffer from a string with no terminator.
3. Use Marshal.PtrToStructure to get an object and inspect the string.

Actual Results:
Appears to read past the end of the buffer. String is often null but sometimes contains the correct chars but with extra appended to it.

Expected Results:
Should read the string correctly.

How often does this happen? 

Additional Information:
See attached project.
Only tested with Xamarin Studio 4.2.2 (Mono 3.3?) and .Net 4.5 on Windows 7, but this was found in our live system on Mono 2.10 Linux.
Comment 1 Zoltan Varga 2014-06-29 09:25:02 UTC
Fixed in mono master faa73b12907c703464ec52ccd11fc2f9a9dd8bd3.
Comment 2 Mohit Kheterpal 2014-08-05 10:30:38 UTC
I have checked this issue and run the attached project, it runs successfully as shown in screencst : http://screencast.com/t/achQSxl2p

Hence closing this issue.