diff --git a/src/scanner.l b/src/scanner.l
index c7941d5a5a7fd750a310ce4b122078536426457c..4a55e1652d7d238ebaa8b9937e54f228e664a3b5 100644
--- a/src/scanner.l
+++ b/src/scanner.l
@@ -122,12 +122,13 @@ identifier   [a-zA-Z_][a-zA-Z0-9_]*
 {float_literal}   { yylval->TK_FLOAT_LITERAL = atof(yytext); return TK_FLOAT_LITERAL; }
 
 {string_literal}  { /* https://stackoverflow.com/questions/14185172/lex-how-to-eliminate-double-quotes-from-a-string-literal */
-                    char* str = (char*)malloc(strlen(yytext));
-                    strcpy(str, "\\");
-                    strncpy(str, yytext, yyleng-1);
-                    str[yyleng+2] = '\0';
-                    yylval->TK_STRING_LITERAL = str;
-                    return TK_STRING_LITERAL; }
+                    char* str = malloc(sizeof(char) * yyleng+3);
+                                        strcpy(str, "\\");
+                                        strncpy(str+1, yytext, yyleng-1);
+                                        strcpy(str+yyleng, "\\\"");
+                                        str[yyleng+2] = '\0';
+                                        yylval->TK_STRING_LITERAL = str;
+                                        return TK_STRING_LITERAL;  }