(root)/
glib-2.79.0/
fuzzing/
fuzz_uri_escape.c
       1  /*
       2   * Copyright 2020 Endless OS Foundation, LLC
       3   * Copyright 2020 Red Hat, Inc.
       4   *
       5   * SPDX-License-Identifier: LGPL-2.1-or-later
       6   *
       7   * This library is free software; you can redistribute it and/or
       8   * modify it under the terms of the GNU Lesser General Public
       9   * License as published by the Free Software Foundation; either
      10   * version 2.1 of the License, or (at your option) any later version.
      11   *
      12   * This library is distributed in the hope that it will be useful,
      13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
      14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      15   * Lesser General Public License for more details.
      16   *
      17   * You should have received a copy of the GNU Lesser General Public
      18   * License along with this library; if not, see <http://www.gnu.org/licenses/>.
      19   */
      20  
      21  #include "fuzz.h"
      22  
      23  static void
      24  test_bytes (const guint8 *data,
      25              gsize         size)
      26  {
      27    GError *error = NULL;
      28    GBytes *unescaped_bytes = NULL;
      29    gchar *escaped_string = NULL;
      30  
      31    if (size > G_MAXSSIZE)
      32      return;
      33  
      34    unescaped_bytes = g_uri_unescape_bytes ((const gchar *) data, (gssize) size, NULL, &error);
      35    if (unescaped_bytes == NULL)
      36      {
      37        g_assert_nonnull (error);
      38        g_clear_error (&error);
      39        return;
      40      }
      41  
      42    escaped_string = g_uri_escape_bytes (g_bytes_get_data (unescaped_bytes, NULL),
      43                                         g_bytes_get_size (unescaped_bytes),
      44                                         NULL);
      45    g_bytes_unref (unescaped_bytes);
      46  
      47    if (escaped_string == NULL)
      48      return;
      49  
      50    g_free (escaped_string);
      51  }
      52  
      53  static void
      54  test_string (const guint8 *data,
      55               gsize         size)
      56  {
      57    gchar *unescaped_string = NULL;
      58    gchar *escaped_string = NULL;
      59  
      60    unescaped_string = g_uri_unescape_segment ((const gchar *) data, (const gchar *) data + size, NULL);
      61    if (unescaped_string == NULL)
      62      return;
      63  
      64    escaped_string = g_uri_escape_string (unescaped_string, NULL, TRUE);
      65    g_free (unescaped_string);
      66  
      67    if (escaped_string == NULL)
      68      return;
      69  
      70    g_free (escaped_string);
      71  }
      72  
      73  int
      74  LLVMFuzzerTestOneInput (const unsigned char *data, size_t size)
      75  {
      76    fuzz_set_logging_func ();
      77  
      78    /* Bytes form */
      79    test_bytes (data, size);
      80  
      81    /* String form (doesn’t do %-decoding) */
      82    test_string (data, size);
      83  
      84    return 0;
      85  }