From 8f4e42c14a1c0785b0c094ae46ce5a32ce4b08cb Mon Sep 17 00:00:00 2001 From: Eric Conlon <37287+ejconlon@users.noreply.github.com> Date: Sun, 31 Mar 2024 14:07:57 -0700 Subject: [PATCH 1/2] modernize --- core/carp_pattern.h | 10 +++++----- core/carp_string.h | 28 ++++++++++++++-------------- src/Concretize.hs | 1 + src/Emit.hs | 1 + src/Eval.hs | 1 + src/GenerateConstraints.hs | 2 +- src/InitialTypes.hs | 1 + src/Obj.hs | 1 + stack.yaml | 2 +- 9 files changed, 26 insertions(+), 21 deletions(-) diff --git a/core/carp_pattern.h b/core/carp_pattern.h index c855320df..a91ec94b3 100644 --- a/core/carp_pattern.h +++ b/core/carp_pattern.h @@ -280,7 +280,7 @@ String Pattern_internal_match(PatternMatchState *ms, String s, String p) { if (s) { p += 4; goto init; /* return match(ms, s, p + 4); */ - } /* else fail (s == NULL) */ + } /* else fail (s == NULL) */ break; } case 'f': { /* frontier? */ @@ -341,7 +341,7 @@ String Pattern_internal_match(PatternMatchState *ms, String s, String p) { break; } default: - dflt : { /* Pattern class plus optional suffix */ + dflt: { /* Pattern class plus optional suffix */ String ep = Pattern_internal_classend( ms, p); /* points to optional suffix */ /* does not match at least once? */ @@ -574,7 +574,7 @@ String Pattern_internal_add_char(String a, Char b) { int len = strlen(a) + 2; String buffer = CARP_MALLOC(len); - snprintf(buffer, len-1, "%s%c", a, b); + snprintf(buffer, len - 1, "%s%c", a, b); CARP_FREE(a); return buffer; } @@ -645,7 +645,7 @@ String Pattern_substitute(Pattern *p, String *s, String *t, int ns) { int l = strlen(res) + strlen(str) + 1; String buffer = CARP_MALLOC(l); - snprintf(buffer, l-1, "%s%s", res, str); + snprintf(buffer, l - 1, "%s%s", res, str); CARP_FREE(res); return buffer; } @@ -671,7 +671,7 @@ String Pattern_str(Pattern *p) { String Pattern_prn(Pattern *p) { int n = strlen(*p) + 4; String buffer = CARP_MALLOC(n); - snprintf(buffer, n-1, "#\"%s\"", *p); + snprintf(buffer, n - 1, "#\"%s\"", *p); return buffer; } diff --git a/core/carp_string.h b/core/carp_string.h index 1d73c2418..ea398b854 100644 --- a/core/carp_string.h +++ b/core/carp_string.h @@ -139,7 +139,7 @@ char String_char_MINUS_at(const String *s, int i) { String String_format(const String *str, const String *s) { int size = snprintf(NULL, 0, *str, *s) + 1; String buffer = CARP_MALLOC(size); - sprintf(buffer, *str, *s); + snprintf(buffer, size, *str, *s); return buffer; } @@ -230,7 +230,7 @@ String Bool_str(bool b) { String Bool_format(const String *str, bool b) { int size = snprintf(NULL, 0, *str, b) + 1; String buffer = CARP_MALLOC(size); - sprintf(buffer, *str, b); + snprintf(buffer, size, *str, b); return buffer; } @@ -258,21 +258,21 @@ String Char_prn(Char c) { String Char_format(const String *str, char b) { int size = snprintf(NULL, 0, *str, b) + 1; String buffer = CARP_MALLOC(size); - sprintf(buffer, *str, b); + snprintf(buffer, size, *str, b); return buffer; } String Double_str(double x) { int size = snprintf(NULL, 0, "%g", x) + 1; String buffer = CARP_MALLOC(size); - sprintf(buffer, "%g", x); + snprintf(buffer, size, "%g", x); return buffer; } String Double_format(const String *s, double x) { int size = snprintf(NULL, 0, *s, x) + 1; String buffer = CARP_MALLOC(size); - sprintf(buffer, *s, x); + snprintf(buffer, size, *s, x); return buffer; } @@ -285,14 +285,14 @@ bool Double_from_MINUS_string_MINUS_internal(const String *s, double *target) { String Float_str(float x) { int size = snprintf(NULL, 0, "%gf", x) + 1; String buffer = CARP_MALLOC(size); - sprintf(buffer, "%gf", x); + snprintf(buffer, size, "%gf", x); return buffer; } String Float_format(const String *str, float x) { int size = snprintf(NULL, 0, *str, x) + 1; String buffer = CARP_MALLOC(size); - sprintf(buffer, *str, x); + snprintf(buffer, size, *str, x); return buffer; } @@ -305,14 +305,14 @@ bool Float_from_MINUS_string_MINUS_internal(const String *s, float *target) { String Int_str(int x) { int size = snprintf(NULL, 0, "%d", x) + 1; String buffer = CARP_MALLOC(size); - sprintf(buffer, "%d", x); + snprintf(buffer, size, "%d", x); return buffer; } String Int_format(const String *str, int x) { int size = snprintf(NULL, 0, *str, x) + 1; String buffer = CARP_MALLOC(size); - sprintf(buffer, *str, x); + snprintf(buffer, size, *str, x); return buffer; } @@ -325,14 +325,14 @@ bool Int_from_MINUS_string_MINUS_internal(const String *s, int *target) { String Long_str(Long x) { int size = snprintf(NULL, 0, "%" PRIi64, x) + 1; String buffer = CARP_MALLOC(size); - sprintf(buffer, "%" PRIi64, x); + snprintf(buffer, size, "%" PRIi64, x); return buffer; } String Long_format(const String *str, Long x) { int size = snprintf(NULL, 0, *str, x) + 1; String buffer = CARP_MALLOC(size); - sprintf(buffer, *str, x); + snprintf(buffer, size, *str, x); return buffer; } @@ -345,14 +345,14 @@ bool Long_from_MINUS_string_MINUS_internal(const String *s, Long *target) { String Byte_str(uint8_t x) { int size = snprintf(NULL, 0, "%ub", x) + 1; String buffer = CARP_MALLOC(size); - sprintf(buffer, "%ub", x); + snprintf(buffer, size, "%ub", x); return buffer; } String Byte_format(const String *str, uint8_t x) { int size = snprintf(NULL, 0, *str, x) + 1; String buffer = CARP_MALLOC(size); - sprintf(buffer, *str, x); + snprintf(buffer, size, *str, x); return buffer; } @@ -386,6 +386,6 @@ int String_index_MINUS_of(const String *s, char c) { String Pointer_strp(void *in) { int size = snprintf(NULL, 0, "%p", in) + 1; String buffer = CARP_MALLOC(size); - sprintf(buffer, "%p", in); + snprintf(buffer, size, "%p", in); return buffer; } diff --git a/src/Concretize.hs b/src/Concretize.hs index 426641665..46550c7e2 100644 --- a/src/Concretize.hs +++ b/src/Concretize.hs @@ -26,6 +26,7 @@ where import AssignTypes import Constraints import Control.Applicative +import Control.Monad (foldM, unless) import Control.Monad.State import Data.Either (fromRight) import Data.List (foldl') diff --git a/src/Emit.hs b/src/Emit.hs index a41c85d33..800588560 100644 --- a/src/Emit.hs +++ b/src/Emit.hs @@ -12,6 +12,7 @@ module Emit ) where +import Control.Monad (unless, when, zipWithM_) import Control.Monad.State import Data.Char (ord) import Data.Functor ((<&>)) diff --git a/src/Eval.hs b/src/Eval.hs index 660b69a08..10df0b797 100644 --- a/src/Eval.hs +++ b/src/Eval.hs @@ -7,6 +7,7 @@ import Commands import Context import Control.Applicative import Control.Exception +import Control.Monad (foldM, when) import Control.Monad.State import Data.Either (fromRight) import Data.Foldable (foldlM, foldrM) diff --git a/src/GenerateConstraints.hs b/src/GenerateConstraints.hs index 3474423d6..bb9a4c5db 100644 --- a/src/GenerateConstraints.hs +++ b/src/GenerateConstraints.hs @@ -2,7 +2,7 @@ module GenerateConstraints (genConstraints) where import Constraints import Control.Arrow hiding (arr) -import Control.Monad.State +import Control.Monad (join) import Data.List as List import Data.Maybe (catMaybes, fromMaybe, mapMaybe) import Info diff --git a/src/InitialTypes.hs b/src/InitialTypes.hs index a4d1f251d..88ccc746a 100644 --- a/src/InitialTypes.hs +++ b/src/InitialTypes.hs @@ -1,5 +1,6 @@ module InitialTypes where +import Control.Monad (foldM, join, replicateM) import Control.Monad.State import Data.Either (fromRight) import Env as E diff --git a/src/Obj.hs b/src/Obj.hs index 1372fbfa6..71fbfc21b 100644 --- a/src/Obj.hs +++ b/src/Obj.hs @@ -5,6 +5,7 @@ module Obj where import Control.Applicative +import Control.Monad (zipWithM) import Control.Monad.State import Data.Char import Data.Hashable diff --git a/stack.yaml b/stack.yaml index 0b83203ad..de6cebb72 100644 --- a/stack.yaml +++ b/stack.yaml @@ -15,7 +15,7 @@ # resolver: # name: custom-snapshot # location: "./custom-snapshot.yaml" -resolver: lts-19.2 +resolver: lts-22.14 # User packages to be built. # Various formats can be used as shown in the example below. From e54f7e061dc3190945f2f808bba01e18d40ebcb1 Mon Sep 17 00:00:00 2001 From: Eric Conlon <37287+ejconlon@users.noreply.github.com> Date: Sun, 31 Mar 2024 15:02:05 -0700 Subject: [PATCH 2/2] fix all sprintf -> snprintf --- core/carp_pattern.h | 8 ++++---- src/ArrayTemplates.hs | 6 +++--- src/BoxTemplates.hs | 8 ++++---- src/Deftype.hs | 4 ++-- src/StructUtils.hs | 4 ++-- src/Sumtypes.hs | 4 ++-- 6 files changed, 17 insertions(+), 17 deletions(-) diff --git a/core/carp_pattern.h b/core/carp_pattern.h index a91ec94b3..0b59ddf84 100644 --- a/core/carp_pattern.h +++ b/core/carp_pattern.h @@ -568,13 +568,13 @@ Array Pattern_match_MINUS_all_MINUS_groups(Pattern *p, String *s) { String Pattern_internal_add_char(String a, Char b) { if (!a) { String buffer = CARP_MALLOC(2); - snprintf(buffer, 1, "%c", b); + snprintf(buffer, 2, "%c", b); return buffer; } int len = strlen(a) + 2; String buffer = CARP_MALLOC(len); - snprintf(buffer, len - 1, "%s%c", a, b); + snprintf(buffer, len, "%s%c", a, b); CARP_FREE(a); return buffer; } @@ -645,7 +645,7 @@ String Pattern_substitute(Pattern *p, String *s, String *t, int ns) { int l = strlen(res) + strlen(str) + 1; String buffer = CARP_MALLOC(l); - snprintf(buffer, l - 1, "%s%s", res, str); + snprintf(buffer, l, "%s%s", res, str); CARP_FREE(res); return buffer; } @@ -671,7 +671,7 @@ String Pattern_str(Pattern *p) { String Pattern_prn(Pattern *p) { int n = strlen(*p) + 4; String buffer = CARP_MALLOC(n); - snprintf(buffer, n - 1, "#\"%s\"", *p); + snprintf(buffer, n, "#\"%s\"", *p); return buffer; } diff --git a/src/ArrayTemplates.hs b/src/ArrayTemplates.hs index 35fc40651..391f60b01 100644 --- a/src/ArrayTemplates.hs +++ b/src/ArrayTemplates.hs @@ -669,7 +669,7 @@ strTy typeEnv env (StructTy _ [innerType]) = TokC " String buffer = CARP_MALLOC(size);\n", TokC " String bufferPtr = buffer;\n", TokC "\n", - TokC " sprintf(buffer, \"[\");\n", + TokC " snprintf(buffer, size, \"[\");\n", TokC " bufferPtr += 1;\n", TokC "\n", TokC " for(int i = 0; i < a->len; i++) {\n", @@ -677,7 +677,7 @@ strTy typeEnv env (StructTy _ [innerType]) = TokC " }\n", TokC "\n", TokC " if(a->len > 0) { bufferPtr -= 1; }\n", - TokC " sprintf(bufferPtr, \"]\");\n", + TokC " snprintf(bufferPtr, size - (bufferPtr - buffer), \"]\");\n", TokC " return buffer;\n" ] strTy _ _ _ = [] @@ -729,7 +729,7 @@ insideArrayStr typeEnv env t = FunctionFound functionFullName -> unlines [ " temp = " ++ strcall functionFullName, - " sprintf(bufferPtr, \"%s \", temp);", + " snprintf(bufferPtr, size - (bufferPtr - buffer), \"%s \", temp);", " bufferPtr += strlen(temp) + 1;", " if(temp) {", " CARP_FREE(temp);", diff --git a/src/BoxTemplates.hs b/src/BoxTemplates.hs index d31bd3919..cbac7872b 100644 --- a/src/BoxTemplates.hs +++ b/src/BoxTemplates.hs @@ -194,7 +194,7 @@ prn = [ "$DECL {", " if(!box){", " String buffer = CARP_MALLOC(4);", - " sprintf(buffer, \"Nil\");", + " snprintf(buffer, 4, \"Nil\");", " return buffer;", " }", innerStr tenv env boxT, @@ -231,7 +231,7 @@ str = [ "$DECL {", " if(!box){", " String buffer = CARP_MALLOC(4);", - " sprintf(buffer, \"Nil\");", + " snprintf(buffer, 4, \"Nil\");", " return buffer;", " }", innerStr tenv env boxT, @@ -257,7 +257,7 @@ innerStr tenv env (StructTy _ [t]) = [ " char* temp = " ++ functionFullName ++ "(*box);", " int size = snprintf(NULL, 0, \"(Box %s)\", temp);", " String buffer = CARP_MALLOC(size);", - " sprintf(buffer, \"(Box %s)\", temp);", + " snprintf(buffer, size, \"(Box %s)\", temp);", " if(temp) {", " CARP_FREE(temp);", " temp = NULL;", @@ -266,7 +266,7 @@ innerStr tenv env (StructTy _ [t]) = FunctionNotFound _ -> unlines [ " String buffer = CARP_MALLOC(14);", - " sprintf(buffer, \"(Box unknown)\");" + " snprintf(buffer, 14, \"(Box unknown)\");" ] FunctionIgnored -> " /* Ignore type inside Box: '" ++ show t ++ "' ??? */\n" innerStr _ _ _ = "" diff --git a/src/Deftype.hs b/src/Deftype.hs index 6987fc2f3..381e11ccd 100644 --- a/src/Deftype.hs +++ b/src/Deftype.hs @@ -509,11 +509,11 @@ strGenerator = TG.mkTemplateGenerator genT decl body deps " String buffer = CARP_MALLOC(size);", " String bufferPtr = buffer;", "", - " sprintf(bufferPtr, \"(%s \", \"" ++ typeName ++ "\");", + " snprintf(bufferPtr, size - (bufferPtr - buffer), \"(%s \", \"" ++ typeName ++ "\");", " bufferPtr += strlen(\"" ++ typeName ++ "\") + 2;\n", joinLines (map (memberPrn typeEnv env) members), " bufferPtr--;", - " sprintf(bufferPtr, \")\");", + " snprintf(bufferPtr, size - (bufferPtr - buffer), \")\");", " return buffer;", "}" ] diff --git a/src/StructUtils.hs b/src/StructUtils.hs index feb62fc26..9189be1be 100644 --- a/src/StructUtils.hs +++ b/src/StructUtils.hs @@ -36,14 +36,14 @@ memberPrn typeEnv env (memberName, memberTy) = (FuncTy [UnitTy] _ _) -> unlines [ " temp = " ++ pathToC strFunctionPath ++ "();", - " sprintf(bufferPtr, \"%s \", temp);", + " snprintf(bufferPtr, size - (bufferPtr - buffer), \"%s \", temp);", " bufferPtr += strlen(temp) + 1;", " if(temp) { CARP_FREE(temp); temp = NULL; }" ] _ -> unlines [ " temp = " ++ pathToC strFunctionPath ++ "(" ++ prefix ++ "p->" ++ memberName ++ ");", - " sprintf(bufferPtr, \"%s \", temp);", + " snprintf(bufferPtr, size - (bufferPtr - buffer), \"%s \", temp);", " bufferPtr += strlen(temp) + 1;", " if(temp) { CARP_FREE(temp); temp = NULL; }" ] diff --git a/src/Sumtypes.hs b/src/Sumtypes.hs index a9760432a..f91bb4bb6 100644 --- a/src/Sumtypes.hs +++ b/src/Sumtypes.hs @@ -422,11 +422,11 @@ tokensForStr typeEnv env generic concrete fields = let (name, tys, correctedTagName) = namesFromCase theCase concrete in unlines [ " if(p->_tag == " ++ correctedTagName ++ ") {", - " sprintf(bufferPtr, \"(%s \", \"" ++ name ++ "\");", + " snprintf(bufferPtr, size - (bufferPtr - buffer), \"(%s \", \"" ++ name ++ "\");", " bufferPtr += strlen(\"" ++ name ++ "\") + 2;\n", joinLines $ memberPrn typeEnv env <$> unionMembers name tys, " bufferPtr--;", - " sprintf(bufferPtr, \")\");", + " snprintf(bufferPtr, size - (bufferPtr - buffer), \")\");", " }" ] calculateStructStrSize :: [TC.TypeField] -> String