Skip to content

It's not safe to Load InitType with type name only #382

@rugbbyli

Description

@rugbbyli

Sorry, this issue is for ClojureCLR (https://github.com/arcadia-unity/clojure-clr), but that project does not accept issue, so i write it here.

When ClojureCLR invoke a clojure code, it iterate over all assemblies and find the type with initClassName matched. source code here:

        internal static bool TryLoadInitType(string relativePath)
        {
            var initClassName = InitClassName(relativePath);
            Type initType = null;
            foreach (var asm in AppDomain.CurrentDomain.GetAssemblies())
            {
#if CLR2
                if(asm.ManifestModule is ModuleBuilder)
#else
                if (asm.IsDynamic)
#endif
                    continue;
                initType = asm.GetType(initClassName);
                if (initType != null)
                    break;
            }
            if (initType == null)
                return false;

            InvokeInitType(initType.Assembly, initType);
            return true;
        }

But it's not safe, as there may be another type have a same name , and would be wrongly loaded.

To prevent this situation, we could double check using excepted assembly name, like this:

        internal static bool TryLoadInitType(string relativePath)
        {
            var initClassName = InitClassName(relativePath);
            Type initType = null;
            var asmName = relativePath.Replace('/', '.') + ".clj";
            foreach (var asm in AppDomain.CurrentDomain.GetAssemblies())
            {
#if CLR2
                if(asm.ManifestModule is ModuleBuilder)
#else
                if (asm.IsDynamic)
#endif
                    continue;
                if(!asm.GetName(false).Name.StartsWith(asmName))
                    continue;
                initType = asm.GetType(initClassName);
                if (initType != null)
                    break;
            }
            if (initType == null)
                return false;

            InvokeInitType(initType.Assembly, initType);
            return true;
        }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions