Commit 68e11e9a authored by DailyShana's avatar DailyShana

use snprintf

strncpy do not add terminal ‘\0' if the src is too long
parent 3c4fd547
...@@ -5,7 +5,6 @@ ...@@ -5,7 +5,6 @@
* Author: Argon * Author: Argon
*/ */
#include <stdio.h>
#include "duel.h" #include "duel.h"
#include "group.h" #include "group.h"
#include "card.h" #include "card.h"
...@@ -720,7 +719,7 @@ int32 interpreter::load_script(const char* script_name) { ...@@ -720,7 +719,7 @@ int32 interpreter::load_script(const char* script_name) {
no_action++; no_action++;
int32 error = luaL_loadbuffer(current_state, (char*)buffer, len, script_name) || lua_pcall(current_state, 0, 0, 0); int32 error = luaL_loadbuffer(current_state, (char*)buffer, len, script_name) || lua_pcall(current_state, 0, 0, 0);
if (error) { if (error) {
interpreter::strcpy(pduel->strbuffer, lua_tostring(current_state, -1)); sprintf(pduel->strbuffer, "%s", lua_tostring(current_state, -1));
handle_message(pduel, 1); handle_message(pduel, 1);
lua_pop(current_state, 1); lua_pop(current_state, 1);
no_action--; no_action--;
...@@ -850,7 +849,7 @@ int32 interpreter::call_function(int32 f, uint32 param_count, int32 ret_count) { ...@@ -850,7 +849,7 @@ int32 interpreter::call_function(int32 f, uint32 param_count, int32 ret_count) {
call_depth++; call_depth++;
push_param(current_state); push_param(current_state);
if (lua_pcall(current_state, param_count, ret_count, 0)) { if (lua_pcall(current_state, param_count, ret_count, 0)) {
interpreter::strcpy(pduel->strbuffer, lua_tostring(current_state, -1)); sprintf(pduel->strbuffer, "%s", lua_tostring(current_state, -1));
handle_message(pduel, 1); handle_message(pduel, 1);
lua_pop(current_state, 1); lua_pop(current_state, 1);
no_action--; no_action--;
...@@ -890,7 +889,7 @@ int32 interpreter::call_card_function(card* pcard, const char* f, uint32 param_c ...@@ -890,7 +889,7 @@ int32 interpreter::call_card_function(card* pcard, const char* f, uint32 param_c
lua_remove(current_state, -2); lua_remove(current_state, -2);
push_param(current_state); push_param(current_state);
if (lua_pcall(current_state, param_count, ret_count, 0)) { if (lua_pcall(current_state, param_count, ret_count, 0)) {
interpreter::strcpy(pduel->strbuffer, lua_tostring(current_state, -1)); sprintf(pduel->strbuffer, "%s", lua_tostring(current_state, -1));
handle_message(pduel, 1); handle_message(pduel, 1);
lua_pop(current_state, 1); lua_pop(current_state, 1);
no_action--; no_action--;
...@@ -930,7 +929,7 @@ int32 interpreter::call_code_function(uint32 code, const char* f, uint32 param_c ...@@ -930,7 +929,7 @@ int32 interpreter::call_code_function(uint32 code, const char* f, uint32 param_c
call_depth++; call_depth++;
push_param(current_state); push_param(current_state);
if (lua_pcall(current_state, param_count, ret_count, 0)) { if (lua_pcall(current_state, param_count, ret_count, 0)) {
interpreter::strcpy(pduel->strbuffer, lua_tostring(current_state, -1)); sprintf(pduel->strbuffer, "%s", lua_tostring(current_state, -1));
handle_message(pduel, 1); handle_message(pduel, 1);
lua_pop(current_state, 1); lua_pop(current_state, 1);
no_action--; no_action--;
...@@ -985,7 +984,7 @@ int32 interpreter::check_matching(card* pcard, int32 findex, int32 extraargs) { ...@@ -985,7 +984,7 @@ int32 interpreter::check_matching(card* pcard, int32 findex, int32 extraargs) {
for(int32 i = 0; i < extraargs; ++i) for(int32 i = 0; i < extraargs; ++i)
lua_pushvalue(current_state, (int32)(-extraargs - 2)); lua_pushvalue(current_state, (int32)(-extraargs - 2));
if (lua_pcall(current_state, 1 + extraargs, 1, 0)) { if (lua_pcall(current_state, 1 + extraargs, 1, 0)) {
interpreter::strcpy(pduel->strbuffer, lua_tostring(current_state, -1)); sprintf(pduel->strbuffer, "%s", lua_tostring(current_state, -1));
handle_message(pduel, 1); handle_message(pduel, 1);
lua_pop(current_state, 1); lua_pop(current_state, 1);
no_action--; no_action--;
...@@ -1016,7 +1015,7 @@ int32 interpreter::get_operation_value(card* pcard, int32 findex, int32 extraarg ...@@ -1016,7 +1015,7 @@ int32 interpreter::get_operation_value(card* pcard, int32 findex, int32 extraarg
for(int32 i = 0; i < extraargs; ++i) for(int32 i = 0; i < extraargs; ++i)
lua_pushvalue(current_state, (int32)(-extraargs - 2)); lua_pushvalue(current_state, (int32)(-extraargs - 2));
if (lua_pcall(current_state, 1 + extraargs, 1, 0)) { if (lua_pcall(current_state, 1 + extraargs, 1, 0)) {
interpreter::strcpy(pduel->strbuffer, lua_tostring(current_state, -1)); sprintf(pduel->strbuffer, "%s", lua_tostring(current_state, -1));
handle_message(pduel, 1); handle_message(pduel, 1);
lua_pop(current_state, 1); lua_pop(current_state, 1);
no_action--; no_action--;
...@@ -1156,7 +1155,7 @@ int32 interpreter::call_coroutine(int32 f, uint32 param_count, uint32 * yield_va ...@@ -1156,7 +1155,7 @@ int32 interpreter::call_coroutine(int32 f, uint32 param_count, uint32 * yield_va
return COROUTINE_YIELD; return COROUTINE_YIELD;
} else { } else {
coroutines.erase(f); coroutines.erase(f);
interpreter::strcpy(pduel->strbuffer, lua_tostring(rthread, -1)); sprintf(pduel->strbuffer, "%s", lua_tostring(rthread, -1));
handle_message(pduel, 1); handle_message(pduel, 1);
lua_pop(rthread, 1); lua_pop(rthread, 1);
current_state = lua_state; current_state = lua_state;
......
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
#include <unordered_map> #include <unordered_map>
#include <list> #include <list>
#include <vector> #include <vector>
#include <cstdio>
#include <cstring> #include <cstring>
#include <cmath> #include <cmath>
...@@ -72,9 +73,9 @@ public: ...@@ -72,9 +73,9 @@ public:
static void set_duel_info(lua_State* L, duel* pduel); static void set_duel_info(lua_State* L, duel* pduel);
static duel* get_duel_info(lua_State* L); static duel* get_duel_info(lua_State* L);
template <size_t N> template <size_t N, typename... TR>
static char* strcpy(char (&dst)[N], const char* src) { static int sprintf(char (&buffer)[N], const char* format, TR... args) {
return std::strncpy(reinterpret_cast<char*>(&dst), src, N); return std::snprintf(buffer, N, format, args...);
} }
}; };
......
...@@ -18,7 +18,7 @@ int32 scriptlib::debug_message(lua_State *L) { ...@@ -18,7 +18,7 @@ int32 scriptlib::debug_message(lua_State *L) {
lua_getglobal(L, "tostring"); lua_getglobal(L, "tostring");
lua_pushvalue(L, -2); lua_pushvalue(L, -2);
lua_pcall(L, 1, 1, 0); lua_pcall(L, 1, 1, 0);
interpreter::strcpy(pduel->strbuffer, lua_tostring(L, -1)); interpreter::sprintf(pduel->strbuffer, "%s", lua_tostring(L, -1));
handle_message(pduel, 2); handle_message(pduel, 2);
return 0; return 0;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment