From edea92223cadc27613b9955e865bdfc3dd684102 Mon Sep 17 00:00:00 2001 From: Matthew Fernandez Date: Mon, 25 Apr 2022 22:07:52 -0700 Subject: [PATCH] cgraph: remove 'itos' This function was unsafe to use in the way described. It relied on semantics that are not guaranteed under C99. That is, the lifetime extension of a struct member of an rvalue. This changes under C11 to something that would make this not problematic. But it is unlikely Graphviz will be able to migrate to C11 in the foreseeable future as MSVC is lacking C11 support. Gitlab: closes #2229 --- CHANGELOG.md | 1 + lib/cgraph/itos.h | 21 ------------- lib/cgraph/test_itos.c | 69 ------------------------------------------ rtest/test_itos.py | 23 -------------- 4 files changed, 1 insertion(+), 113 deletions(-) delete mode 100644 lib/cgraph/test_itos.c delete mode 100644 rtest/test_itos.py diff --git a/CHANGELOG.md b/CHANGELOG.md index 0eb34b143..1eb9ec596 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -40,6 +40,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 Graphviz 2.47.0. - Error on more than 128 cluster subgraphs #2080 - `dot2gxl` no longer crashes on input `` #2092 +- remove itos #2229 ### Removed diff --git a/lib/cgraph/itos.h b/lib/cgraph/itos.h index 5d0d24d6c..ad0b43646 100644 --- a/lib/cgraph/itos.h +++ b/lib/cgraph/itos.h @@ -1,25 +1,4 @@ #pragma once -#include - // maximum number of bytes needed to print a NUL-terminated int enum { CHARS_FOR_NUL_TERM_INT = 12 }; - -// return type of itos below -struct itos_ { - char str[CHARS_FOR_NUL_TERM_INT]; -}; - -/** convert an integer to a string - * - * Intended usage is something like: - * agattr(g, AGEDGE, "hello", itos(42).str) - * - * @param i Integer to convert - * @return Stringized conversion wrapped in a struct - */ -static inline struct itos_ itos(int i) { - struct itos_ s; - (void)snprintf(s.str, sizeof(s.str), "%d", i); - return s; -} diff --git a/lib/cgraph/test_itos.c b/lib/cgraph/test_itos.c deleted file mode 100644 index d6d604d98..000000000 --- a/lib/cgraph/test_itos.c +++ /dev/null @@ -1,69 +0,0 @@ -// basic unit tester for itos - -#ifdef NDEBUG -#error this is not intended to be compiled with assertions off -#endif - -#include -#include -#include -#include -#include -#include - -static void test_0(void) { assert(strcmp(itos(0).str, "0") == 0); } - -static void test_1(void) { assert(strcmp(itos(1).str, "1") == 0); } - -static void test_neg_1(void) { assert(strcmp(itos(-1).str, "-1") == 0); } - -static void test_min(void) { - - int r = snprintf(NULL, 0, "%d", INT_MIN); - assert(r > 0); - - char *buffer = malloc(sizeof(char) * ((size_t)r + 1)); - assert(buffer != NULL); - - (void)sprintf(buffer, "%d", INT_MIN); - - assert(strcmp(itos(INT_MIN).str, buffer) == 0); - - free(buffer); -} - -static void test_max(void) { - - int r = snprintf(NULL, 0, "%d", INT_MAX); - assert(r > 0); - - char *buffer = malloc(sizeof(char) * ((size_t)r + 1)); - assert(buffer != NULL); - - (void)sprintf(buffer, "%d", INT_MAX); - - assert(strcmp(itos(INT_MAX).str, buffer) == 0); - - free(buffer); -} - -int main(void) { - -#define RUN(t) \ - do { \ - printf("running test_%s... ", #t); \ - fflush(stdout); \ - test_##t(); \ - printf("OK\n"); \ - } while (0) - - RUN(0); - RUN(1); - RUN(neg_1); - RUN(min); - RUN(max); - -#undef RUN - - return EXIT_SUCCESS; -} diff --git a/rtest/test_itos.py b/rtest/test_itos.py deleted file mode 100644 index 459258722..000000000 --- a/rtest/test_itos.py +++ /dev/null @@ -1,23 +0,0 @@ -"""test ../lib/cgraph/itos.h""" - -import os -from pathlib import Path -import sys - -sys.path.append(os.path.dirname(__file__)) -from gvtest import run_c # pylint: disable=wrong-import-position - -def test_itos(): - """run the itos unit tests""" - - # locate the itos unit tests - src = Path(__file__).parent.resolve() / "../lib/cgraph/test_itos.c" - assert src.exists() - - # locate lib directory that needs to be in the include path - lib = Path(__file__).parent.resolve() / "../lib" - - # extra C flags this compilation needs - cflags = ['-I', lib] - - _, _ = run_c(src, cflags=cflags) -- 2.40.0