diff options
author | Raghuram Subramani <raghus2247@gmail.com> | 2025-06-12 19:56:36 +0530 |
---|---|---|
committer | Raghuram Subramani <raghus2247@gmail.com> | 2025-06-12 19:56:36 +0530 |
commit | ed96dc267247ee92bc2f25ddc617a52edc2a1db6 (patch) | |
tree | 20f1114410aac8c35063a538e2fcda16232e6b28 | |
parent | 68ae30b661af90965832a0c025af3efef59fda6a (diff) |
lexer: use asprintf instead of malloc + strncpy + \0
-rw-r--r-- | src/lexer.cc | 7 |
1 files changed, 3 insertions, 4 deletions
diff --git a/src/lexer.cc b/src/lexer.cc index 2be5cbf..96c5df9 100644 --- a/src/lexer.cc +++ b/src/lexer.cc @@ -85,13 +85,12 @@ Lexer::match(char c) void Lexer::add_token(token_type_e type) { - size_t token_length = m_current - m_start; + int token_length = m_current - m_start; if (token_length == 0) token_length++; - char *lexeme = (char *) malloc(sizeof(char) * token_length + 1); - strncpy(lexeme, m_script + m_start, token_length); - lexeme[sizeof(char) * token_length] = '\0'; + char *lexeme; + asprintf(&lexeme, "%.*s", token_length, m_script + m_start); Token token; token.init(type, lexeme, m_line); |