Skip to content

internal_format在处理空字符串时会抛出IndexOutOfRangeException异常 #14

@jaemok0421

Description

@jaemok0421

rt,当Format传空字符串作为参数时,会导致抛出越界异常。看上去是下面ptr_result[i++] = arg[k++]的部分,由于arg.Length为0,因此只要进到这里就会由于调用arg[k++]导致异常。
看起来在处理字符串之前先针对arg.Length为0的情况做个处理就可以了。

` for (int i = 0, j = 0, x = 0; ; x++) // x < num_args
{
brace_idx = internal_index_of(input, '{', brace_idx + 3);
if (brace_idx == -1)
{
break;
}
next_input_idx = brace_idx;
int arg_idx = input[brace_idx + 1] - '0';
string arg = g_format_args[arg_idx]._value;
if (brace_idx == -1)
throw new InvalidOperationException("没有发现大括号{ for argument " + arg);
if (brace_idx + 2 >= input.Length || input[brace_idx + 2] != '}')
throw new InvalidOperationException("没有发现大括号} for argument " + arg);

            fixed (char* ptr_input = input)
            {
                fixed (char* ptr_result = res_value)
                {
                    for (int k = 0; i < new_len;)
                    {
                        if (j < brace_idx)
                        {
                            ptr_result[i++] = ptr_input[j++];
                            ++next_output_idx;
                        }
                        else
                        {
                            ptr_result[i++] = arg[k++];  // throw exception here if arg.Length == 0
                            ++next_output_idx;
                            if (k == arg.Length)
                            {
                                j += 3;
                                break;
                            }
                        }
                    }
                }
            }
        }`

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions