From 3bcc0cefe984a3dc1e333a580c3d5b0f9cb8a6b6 Mon Sep 17 00:00:00 2001 From: Saleem Abdulrasool Date: Thu, 28 Dec 2023 09:41:12 -0800 Subject: [PATCH] clean up C4244 warnings on MSVC builds C4244 appertains to implicit truncation of integer values [1]. Theoretically, these warnings could be detected using `-fsanitize=implicit-unsigned-integer-truncation` and `-fsanitize=implicit-signed-integer-truncation` as well as statically using `-Werror=implicit-int-conversion`. [1] https://learn.microsoft.com/en-us/cpp/error-messages/compiler-warnings/compiler-warning-levels-3-and-4-c4244?view=msvc-170 --- CMakeLists.txt | 2 +- src/blocks.c | 8 ++++---- src/commonmark.c | 6 +++--- src/node.c | 2 +- src/render.c | 4 ++-- 5 files changed, 11 insertions(+), 11 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 9ed7d6847..1fec2e61d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -46,7 +46,7 @@ add_compile_options($<$:-DCMARK_DEBUG_NODES>) # so that CMark may be used in projects with non-C languages. function(cmark_add_compile_options target) if(MSVC) - target_compile_options(${target} PRIVATE /W4 /wd4706) + target_compile_options(${target} PRIVATE /W4 /wd4706 /we4244) if(MSVC_VERSION LESS 1800) target_compile_options(${target} PRIVATE /TP) endif() diff --git a/src/blocks.c b/src/blocks.c index 83f3785d3..829b0036e 100644 --- a/src/blocks.c +++ b/src/blocks.c @@ -607,7 +607,7 @@ static void S_parser_feed(cmark_parser *parser, const unsigned char *buffer, process = true; } - chunk_len = (eol - buffer); + chunk_len = (bufsize_t)(eol - buffer); if (process) { if (parser->linebuf.size > 0) { cmark_strbuf_put(&parser->linebuf, buffer, chunk_len); @@ -994,7 +994,7 @@ static void open_new_blocks(cmark_parser *parser, cmark_node **container, hashpos++; } - (*container)->as.heading.level = level; + (*container)->as.heading.level = (int8_t)level; (*container)->as.heading.setext = false; (*container)->as.heading.internal_offset = matched; @@ -1004,7 +1004,7 @@ static void open_new_blocks(cmark_parser *parser, cmark_node **container, parser->first_nonspace + 1); (*container)->as.code.fenced = true; (*container)->as.code.fence_char = peek_at(input, parser->first_nonspace); - (*container)->as.code.fence_length = (matched > 255) ? 255 : matched; + (*container)->as.code.fence_length = (matched > 255) ? 255 : (uint8_t)matched; (*container)->as.code.fence_offset = (int8_t)(parser->first_nonspace - parser->offset); (*container)->as.code.info = NULL; @@ -1032,7 +1032,7 @@ static void open_new_blocks(cmark_parser *parser, cmark_node **container, if (has_content) { (*container)->type = (uint16_t)CMARK_NODE_HEADING; - (*container)->as.heading.level = lev; + (*container)->as.heading.level = (int8_t)lev; (*container)->as.heading.setext = true; S_advance_offset(parser, input, input->len - 1 - parser->offset, false); } diff --git a/src/commonmark.c b/src/commonmark.c index f5536f92c..4cce545b7 100644 --- a/src/commonmark.c +++ b/src/commonmark.c @@ -50,18 +50,18 @@ static CMARK_INLINE void outc(cmark_renderer *renderer, cmark_escaping escape, (renderer->begin_content && (c == '.' || c == ')') && follows_digit && (nextc == 0 || cmark_isspace(nextc))))) || (escape == URL && - (c == '`' || c == '<' || c == '>' || cmark_isspace(c) || c == '\\' || + (c == '`' || c == '<' || c == '>' || cmark_isspace((char)c) || c == '\\' || c == ')' || c == '(')) || (escape == TITLE && (c == '`' || c == '<' || c == '>' || c == '"' || c == '\\'))); if (needs_escaping) { - if (escape == URL && cmark_isspace(c)) { + if (escape == URL && cmark_isspace((char)c)) { // use percent encoding for spaces snprintf(encoded, ENCODED_SIZE, "%%%2X", c); cmark_strbuf_puts(renderer->buffer, encoded); renderer->column += 3; - } else if (cmark_ispunct(c)) { + } else if (cmark_ispunct((char)c)) { cmark_render_ascii(renderer, "\\"); cmark_render_code_point(renderer, c); } else { // render as entity diff --git a/src/node.c b/src/node.c index a07b8c0ad..070e3a2d1 100644 --- a/src/node.c +++ b/src/node.c @@ -349,7 +349,7 @@ int cmark_node_set_heading_level(cmark_node *node, int level) { switch (node->type) { case CMARK_NODE_HEADING: - node->as.heading.level = level; + node->as.heading.level = (int8_t)level; return 1; default: diff --git a/src/render.c b/src/render.c index f71b048b6..9f8f6026b 100644 --- a/src/render.c +++ b/src/render.c @@ -96,13 +96,13 @@ static void S_out(cmark_renderer *renderer, const char *source, bool wrap, // we need to escape a potential list marker after // a digit: renderer->begin_content = - renderer->begin_content && cmark_isdigit(c) == 1; + renderer->begin_content && cmark_isdigit((char)c) == 1; } } else { (renderer->outc)(renderer, escape, c, nextc); renderer->begin_line = false; renderer->begin_content = - renderer->begin_content && cmark_isdigit(c) == 1; + renderer->begin_content && cmark_isdigit((char)c) == 1; } // If adding the character went beyond width, look for an