Repository: jerryscript-project/jerryscript
File: jerry-core/ecma/builtin-objects/ecma-builtin-global.c (C)

Code before:
      {
        ecma_stringbuilder_destroy (&builder);
        return ecma_raise_uri_error (ECMA_ERR_MSG ("Unicode surrogate pair missing."));
      }
    }

    lit_utf8_size_t utf_size = lit_code_point_to_utf8 (cp, octets);

    if (utf_size == 1)
    {
      if (ecma_builtin_global_object_character_is_in (octets[0], unescaped_uri_bitset_p))
      {
        ecma_stringbuilder_append_byte (&builder, octets[0]);
      }
      else
      {
        ecma_builtin_global_object_byte_to_hex (result_chars, octets[0]);
        ecma_stringbuilder_append_raw (&builder, result_chars, 3);
      }
    }
    else
    {
      for (uint32_t i = 0; i < utf_size; i++)
      {
        ecma_builtin_global_object_byte_to_hex (result_chars, octets[i]);
        ecma_stringbuilder_append_raw (&builder, result_chars, 3);
      }
    }
  }

  return ecma_make_string_value (ecma_stringbuilder_finalize (&builder));
} /* ecma_builtin_global_object_encode_uri_helper */

#if ENABLED (JERRY_BUILTIN_ANNEXB)

/**
 * Maximum value of a byte.
 */
#define ECMA_ESCAPE_MAXIMUM_BYTE_VALUE (255)

/**
 * Format is a percent sign followed by lowercase u and four hex digits.
 */
#define ECMA_ESCAPE_ENCODED_UNICODE_CHARACTER_SIZE (6)

/**
 * Escape characters bitset:
 *   One bit for each character between 0 - 127.
 *   Bit is set if the character does not need to be converted to %xx form.
 *   These characters are: a-z A-Z 0-9 @ * _ + - . /
 */

Review comment (suggestion):
Move the declaration of `result_chars` here (inside the for loop). Also assert that the `utf_sizte` is not greater that the size of the buffer.

Code after:
        ecma_stringbuilder_destroy (&builder);
        return ecma_raise_uri_error (ECMA_ERR_MSG ("Unicode surrogate pair missing."));
      }
    }

    lit_utf8_size_t utf_size = lit_code_point_to_utf8 (cp, octets);
    lit_utf8_byte_t result_chars[URI_ENCODED_BYTE_SIZE];

    if (utf_size == 1)
    {
      if (ecma_builtin_global_object_character_is_in (octets[0], unescaped_uri_bitset_p))
      {
        ecma_stringbuilder_append_byte (&builder, octets[0]);
      }
      else
      {
        ecma_builtin_global_object_byte_to_hex (result_chars, octets[0]);
        ecma_stringbuilder_append_raw (&builder, result_chars, URI_ENCODED_BYTE_SIZE);
      }
    }
    else
    {
      for (uint32_t i = 0; i < utf_size; i++)
      {
        JERRY_ASSERT (utf_size <= LIT_UTF8_MAX_BYTES_IN_CODE_POINT);
        ecma_builtin_global_object_byte_to_hex (result_chars, octets[i]);
        ecma_stringbuilder_append_raw (&builder, result_chars, URI_ENCODED_BYTE_SIZE);
      }
    }
  }

  return ecma_make_string_value (ecma_stringbuilder_finalize (&builder));
} /* ecma_builtin_global_object_encode_uri_helper */

#if ENABLED (JERRY_BUILTIN_ANNEXB)

/**
 * Maximum value of a byte.
 */
#define ECMA_ESCAPE_MAXIMUM_BYTE_VALUE (255)

/**
 * Format is a percent sign followed by lowercase u and four hex digits.
 */
#define ECMA_ESCAPE_ENCODED_UNICODE_CHARACTER_SIZE (6)

/**
 * Escape characters bitset:
 *   One bit for each character between 0 - 127.
 *   Bit is set if the character does not need to be converted to %xx form.
 *   These characters are: a-z A-Z 0-9 @ * _ + - . /