Bug 5732 - String arguments to C# Attributes which contain NUL are truncated
Summary: String arguments to C# Attributes which contain NUL are truncated
Alias: None
Product: Runtime
Classification: Mono
Component: Reflection ()
Version: unspecified
Hardware: PC Linux
: --- normal
Target Milestone: ---
Assignee: Bugzilla
Depends on:
Reported: 2012-06-19 05:23 UTC by Weeble
Modified: 2012-10-22 17:49 UTC (History)
3 users (show)

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

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 Weeble 2012-06-19 05:23:59 UTC
Observed because some of my NUnit tests use TestCaseAttribute arguments which contain NUL characters. When an attribute argument is a string containing a NUL value, at runtime the the argument will be truncated at the NUL. The following test program prints this on Mono 2.11 on Linux:


but this on Windows .NET 4.0:


The code:

using System;

class StringAttribute : Attribute
    public string Value { get; private set; }
    public StringAttribute(string aString)
        Value = aString;

class Program
    public static void Main()
        StringAttribute a = (StringAttribute)typeof(Program).GetCustomAttributes(typeof(StringAttribute), false)[0];
Comment 1 Weeble 2012-06-19 11:52:05 UTC
I think this is happening in g_utf8_to_utf16. As far as I can tell, the relevant chain of events is:

create_custom_attr runs and deserializes each attribute parameter by calling load_cattr_value.

load_cattr_value deserializes a string by first reading its length in UTF8 bytes, then invoking mono_string_new_len to read the bytes into a string.

mono_string_new_len invokes g_utf8_to_utf16, specifying the length. However, g_utf8_to_utf16 stops as soon as it encounters a nul, even when the length is specified. It's not clear whether this is its intended behaviour: its documentation says "if len < 0, then the string is nul-terminated", but it does not say whether nuls will be considered part of the string when len > 0.


This demonstrates that g_utf8_to_utf16 does indeed truncate such a string:

#include <glib.h>

    char* utf8 = "foo\0bar";
    glong length = 7;
    glong read = 0;
    glong written = 0;
    GError* error = 0;
    gunichar2* utf16 = g_utf8_to_utf16 (utf8, length, &read, &written, &error);
    if (error)
        g_print ("Error!\n");
        g_print ("read: %li written: %li\n", read, written);
Comment 2 Weeble 2012-06-21 19:02:03 UTC
I've started working on a fix: https://github.com/weeble/mono/commits/bug5732

My fix changes mono_string_new_len to always use the full length of UTF8 bytes, even if they include nuls. That's the behaviour needed in this case. However, perhaps it would be more appropriate to introduce a new distinct function for this? Do there need to be two different versions, one that stops at the first nul and one that doesn't? I'm not sure what else mono_string_new_len is used for and which behaviour they need.

Also, are there any C unit-tests somewhere? I've added a C# test that checks the behaviour by using an attribute, but I'd like to include more thorough tests of the C function on its own.
Comment 3 Zoltan Varga 2012-06-22 06:36:11 UTC
That commit looks good. The tests should be put into custom-attr.cs, we have too many test files already.
We don't have C tests. Please state that the commit is licensed under the MIT/X11 license in the commit message, i.e. 'License: MIT/X11' or something, and send a pull request.
Comment 4 Weeble 2012-06-23 15:24:38 UTC
Done: https://github.com/mono/mono/pull/346
Comment 5 James Bellinger 2012-09-10 20:45:56 UTC
Hmm I believe this bug still exists..
Comment 6 Jeffrey Stedfast 2012-10-22 17:11:20 UTC
James: are you using Mono 2.11?