]> git.eli173.com Git - klapaucius/commitdiff
fixed a memory error in strcat
authorElijah Cohen <cohen@eli173.com>
Fri, 19 Sep 2025 01:59:54 +0000 (01:59 +0000)
committerElijah Cohen <cohen@eli173.com>
Fri, 19 Sep 2025 01:59:54 +0000 (01:59 +0000)
src/builtins/strings.c
src/test.c

index b067d6baff345effbf549692e2f9302c73764492..e94d67bff8df9dcd9f04562942bbfd2233a7b9af 100644 (file)
@@ -48,7 +48,7 @@ Sexpr* s_strcat(Sexpr* b, Sexpr* rest, Sexpr* env) {
 #endif // typecheck
        char* fs = unquote(fst)->value.str;
        char* ss = unquote(snd)->value.str;
-       char* out = malloc(sizeof(char)*(strlen(fs)+strlen(ss)));
+       char* out = malloc(sizeof(char)*(strlen(fs)+strlen(ss)+1));
        strcpy(out, fs);
        strcat(out, ss);
        sexpr_free(snd);
index a273ec219b222f8262cee4df62cc3f0b63478c68..8f91df0581950f4d504662fc76124fe01644845b 100644 (file)
@@ -382,6 +382,13 @@ void eval_tests() {
        
 }
 
+void diagnose_strcat() {
+       Sexpr* env = init_dict();
+       env = load_env(env);
+       assert_eq(env, "strcat \"hell\" \"o\"", "\"hello\"");
+       sexpr_free(env);
+}
+
 void many_asserts() {
        Sexpr* env = init_dict();
        env = load_env(env);
@@ -440,6 +447,8 @@ void many_asserts() {
        assert_eq(env, "strexpand \"test\"", "list \"t\" \"e\" \"s\" \"t\"");
        assert_eq(env, "substr \"oo\" \"looopy", "list 2 3");
        assert_eq(env, "strtok \" /\" \"this is/a test\"", "list \"this\" \"is\" \"a\" \"test\"");
+       assert_eq(env, "strlen \"hey\"", "3");
+       assert_eq(env, "strcat \"hey \" \"there\"", "\"hey there\"");
 
        printf("meta\n");
        assert_eq(env, "utob 512", "+");
@@ -536,6 +545,7 @@ void run_tests(){
        //test_string_parsing();
        //eval_tests();
        many_asserts();
+       //diagnose_strcat();
        //memtest_eval();
        //mem_testing();
        //mem_parser();