GenerateNullChecks.refscript

This page describes the GenerateNullChecks RefactorScript shipped with VisualGDB.

Overview

This script generates NULL checks for all function arguments that are pointers or arrays.
It will automatically appear as a smart tag when editing methods.

Script


[Main, Description("Generate NULL checks"), SuggestFor(Function)]
generator GenerateNullChecks(Function func, bool withBraces = false)
{
    set returnType = func.ReturnType;
    foreach(arg in func.Arguments)
    {
        if (arg.Type.Resolved.IsPointerOrReference || arg.Type.Resolved.IsArray)
        {
>if ($arg.ShortName == NULL)
            if (withBraces)
>{
            set isError_t = returnType.Referenced.Name == "error_t";
            set isInt = returnType.Referenced.Name == "int";
            if (isError_t || isInt)
>    return EINVAL;
            else if (returnType.Referenced.Name == "void")
>    return;
            else
>    return -1;
            if (withBraces)
>}
>
        }
    }
}

Sample Input

typedef void *PVOID;

int TestFunction(int nonPointer, char *pointer, PVOID anotherPointer)
{
}

Sample Output

if (pointer == NULL)
{
    return EINVAL;
}

if (anotherPointer == NULL)
{
    return EINVAL;
}

A detailed reference on RefactorScript syntax and data model is available here.
See this page for a full list of RefactorScripts shipped with VisualGDB.