Skip to content

Commit

Permalink
force libcyaml subproject
Browse files Browse the repository at this point in the history
Force use of libcyaml subproject. Use patched version that fixes an
issue with loading out-of-bounds floats. See
tlsa/libcyaml#150 for more info.
  • Loading branch information
alex-tee committed Mar 12, 2021
1 parent c455d34 commit 91d0aa4
Show file tree
Hide file tree
Showing 5 changed files with 163 additions and 5 deletions.
4 changes: 3 additions & 1 deletion meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -757,8 +757,10 @@ have_libsoundio = libsoundio_dep.found ()
if (have_libsoundio)
cdata.set('HAVE_LIBSOUNDIO', 1)
endif
# force patched version of libcyaml from subproject
# see https://github.com/tlsa/libcyaml/issues/150
cyaml_dep = dependency(
'libcyaml', required: false, version: '>=1.1.0',
'libcyaml', required: false, version: '>=99.1.0',
static: all_static)
if not cyaml_dep.found()
cyaml_proj = subproject('libcyaml')
Expand Down
5 changes: 2 additions & 3 deletions subprojects/libcyaml.wrap
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
[wrap-git]
url = https://git.zrythm.org/git/zrythm-cyaml
revision = 4d488780893b24cfd30d990291358cd45b60cfff

url = https://git.sr.ht/~alextee/zrythm-libcyaml
revision = load_floats_from_doubles
1 change: 1 addition & 0 deletions tests/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ if get_option ('tests')
'utils/io': { parallel: true },
'utils/string': { parallel: true },
'utils/ui': { parallel: true },
'utils/yaml': { parallel: true },
'zrythm_app': { parallel: true },
'zrythm': { parallel: true },
}
Expand Down
2 changes: 1 addition & 1 deletion tests/utils/string.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2020 Alexandros Theodotou <alex at zrythm dot org>
* Copyright (C) 2020-2021 Alexandros Theodotou <alex at zrythm dot org>
*
* This file is part of Zrythm
*
Expand Down
156 changes: 156 additions & 0 deletions tests/utils/yaml.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
/*
* Copyright (C) 2021 Alexandros Theodotou <alex at zrythm dot org>
*
* This file is part of Zrythm
*
* Zrythm is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Zrythm is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Zrythm. If not, see <https://www.gnu.org/licenses/>.
*/

#include "zrythm-test-config.h"

#include <stdlib.h>

#include "utils/objects.h"
#include "utils/string.h"
#include "utils/yaml.h"

#include <glib.h>

typedef struct float_struct
{
float fval;
} float_struct;

static const cyaml_schema_field_t
float_struct_fields_schema[] =
{
YAML_FIELD_FLOAT (float_struct, fval),

CYAML_FIELD_END
};

static const cyaml_schema_value_t
float_struct_schema =
{
CYAML_VALUE_MAPPING (
CYAML_FLAG_POINTER,
float_struct, float_struct_fields_schema),
};

SERIALIZE_INC (
float_struct, float_struct)
DESERIALIZE_INC (
float_struct, float_struct)
PRINT_YAML_INC (
float_struct, float_struct)
SERIALIZE_SRC (
float_struct, float_struct)
DESERIALIZE_SRC (
float_struct, float_struct)
PRINT_YAML_SRC (
float_struct, float_struct)

static void
test_load_precise_float (void)
{
yaml_set_log_level (CYAML_LOG_DEBUG);

float_struct my_struct;
my_struct.fval = 12;
char * ret =
float_struct_serialize (&my_struct);
g_assert_cmpstr (
ret, ==, "---\nfval: 12\n...\n");
g_free (ret);

my_struct.fval = 1.55331e-40f;
ret =
float_struct_serialize (&my_struct);
g_assert_cmpstr (
ret, ==, "---\nfval: 1.55331e-40\n...\n");
g_message ("\n%s", ret);

const char * str1 = "---\nfval: 1.55331e-40\n...\n";
float_struct * ret2 =
float_struct_deserialize (str1);
g_message ("loaded val %g", ret2->fval);

#if 0

my_struct.fval = 1.55331e-40f;
ret =
float_struct_serialize (&my_struct);
g_assert_cmpstr (
ret, ==, "---\nfval: 1e-37\n...\n");
g_message ("\n%s", ret);

const char * str1 = "---\nfval: 1e-37\n...\n";
float_struct * ret2 =
float_struct_deserialize (str1);
(void) ret2;
#endif

#if 0

g_message ("FLT_ROUNDS %d", FLT_ROUNDS);
g_message ("FLT_EVAL_METHOD %d", FLT_EVAL_METHOD);
if (my_struct.fval < FLT_MIN)
{
my_struct.fval = 1e+37;
}
else if (my_struct.fval > FLT_MAX)
{
my_struct.fval = 1e-37;
}
char fval_str[60];
sprintf (fval_str, "%g", my_struct.fval);

errno = 0;
char * end = NULL;
float new_val = strtof (fval_str, &end);
g_message ("new val for %s: %g", fval_str, (double) new_val);
if (errno == ERANGE)
{
g_warning ("ERANGE1");
}

errno = 0;
end = NULL;
new_val = strtof ("1.55331e-40", &end);
g_message ("new val: %f", (double) new_val);
if (errno == ERANGE)
{
g_warning ("ERANGE2");
}

const char * str1 = "---\nfval: 1.55331e-40\n...\n";
float_struct * ret2 =
float_struct_deserialize (str1);
g_warning ("%f", (double) ret2->fval);
#endif
}

int
main (int argc, char *argv[])
{
g_test_init (&argc, &argv, NULL);

#define TEST_PREFIX "/utils/yaml/"

g_test_add_func (
TEST_PREFIX "test load precise float",
(GTestFunc) test_load_precise_float);

return g_test_run ();
}

0 comments on commit 91d0aa4

Please sign in to comment.