From 5c486d13175fcfcb9db7b5f05ff870c5e81ed828 Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Sun, 9 Jun 2024 17:33:48 +0800 Subject: [PATCH] fix vargs --- include/pocketpy/compiler/lexer.hpp | 6 +++--- src/compiler/compiler.cpp | 2 +- src/compiler/lexer.cpp | 10 +++++++--- 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/include/pocketpy/compiler/lexer.hpp b/include/pocketpy/compiler/lexer.hpp index 65460fa3..b8e9ba4a 100644 --- a/include/pocketpy/compiler/lexer.hpp +++ b/include/pocketpy/compiler/lexer.hpp @@ -126,10 +126,10 @@ struct Lexer { [[nodiscard]] Error* lex_one_token(bool* eof) noexcept; /***** Error Reporter *****/ - [[nodiscard]] Error* _error(bool lexer_err, const char* type, const char* msg, va_list args, i64 userdata=0) noexcept; + [[nodiscard]] Error* _error(bool lexer_err, const char* type, const char* msg, va_list* args, i64 userdata=0) noexcept; [[nodiscard]] Error* SyntaxError(const char* fmt, ...) noexcept; - [[nodiscard]] Error* IndentationError(const char* msg) noexcept { return _error(true, "IndentationError", msg, {}); } - [[nodiscard]] Error* NeedMoreLines() noexcept { return _error(true, "NeedMoreLines", "", {}, 0); } + [[nodiscard]] Error* IndentationError(const char* msg) noexcept { return _error(true, "IndentationError", msg, NULL); } + [[nodiscard]] Error* NeedMoreLines() noexcept { return _error(true, "NeedMoreLines", "", NULL, 0); } Lexer(VM* vm, std::shared_ptr src) noexcept; diff --git a/src/compiler/compiler.cpp b/src/compiler/compiler.cpp index 567a8f15..803690b6 100644 --- a/src/compiler/compiler.cpp +++ b/src/compiler/compiler.cpp @@ -1340,7 +1340,7 @@ Compiler::~Compiler(){ Error* Compiler::SyntaxError(const char* msg, ...) noexcept{ va_list args; va_start(args, msg); - Error* e = lexer._error(false, "SyntaxError", msg, args); + Error* e = lexer._error(false, "SyntaxError", msg, &args); e->lineno = err().line; e->cursor = err().start; va_end(args); diff --git a/src/compiler/lexer.cpp b/src/compiler/lexer.cpp index 2830497f..f303a7ba 100644 --- a/src/compiler/lexer.cpp +++ b/src/compiler/lexer.cpp @@ -500,7 +500,7 @@ Error* Lexer::lex_one_token(bool* eof) noexcept{ return NULL; } -Error* Lexer::_error(bool lexer_err, const char* type, const char* msg, va_list args, i64 userdata) noexcept{ +Error* Lexer::_error(bool lexer_err, const char* type, const char* msg, va_list* args, i64 userdata) noexcept{ PK_THREAD_LOCAL Error err; err.type = type; err.src = src; @@ -515,7 +515,11 @@ Error* Lexer::_error(bool lexer_err, const char* type, const char* msg, va_list err.lineno = -1; err.cursor = NULL; } - vsnprintf(err.msg, sizeof(err.msg), msg, args); + if(args){ + vsnprintf(err.msg, sizeof(err.msg), msg, *args); + }else{ + std::strncpy(err.msg, msg, sizeof(err.msg)); + } err.userdata = userdata; return &err; } @@ -523,7 +527,7 @@ Error* Lexer::_error(bool lexer_err, const char* type, const char* msg, va_list Error* Lexer::SyntaxError(const char* fmt, ...) noexcept{ va_list args; va_start(args, fmt); - Error* err = _error(true, "SyntaxError", fmt, args); + Error* err = _error(true, "SyntaxError", fmt, &args); va_end(args); return err; }