Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch to snprintf throughout codebase #664

Merged
merged 1 commit into from
Jun 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions src/lparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -961,14 +961,14 @@ static void doquote(LexState *ls, int isexp) {
}

// buf should be at least 128 chars
static void number_type(LexState *ls, int flags, char *buf) {
static void number_type(LexState *ls, int flags, char *buf, size_t bufsiz) {
if (ls->in_terra) {
if (flags & F_ISINTEGER) {
const char *sign = (flags & F_ISUNSIGNED) ? "u" : "";
const char *sz = (flags & F_IS8BYTES) ? "64" : "";
sprintf(buf, "%sint%s", sign, sz);
snprintf(buf, bufsiz, "%sint%s", sign, sz);
} else {
sprintf(buf, "%s", (flags & F_IS8BYTES) ? "double" : "float");
snprintf(buf, bufsiz, "%s", (flags & F_IS8BYTES) ? "double" : "float");
}
}
}
Expand Down Expand Up @@ -997,11 +997,11 @@ static void simpleexp(LexState *ls) {
case TK_NUMBER: {
char buf[128];
int flags = ls->t.seminfo.flags;
number_type(ls, flags, &buf[0]);
number_type(ls, flags, &buf[0], sizeof(buf));
if (flags & F_ISINTEGER) {
push_integer(ls, ls->t.seminfo.i);
push_literal(ls, buf);
sprintf(buf, "%" PRIu64, ls->t.seminfo.i);
snprintf(buf, sizeof(buf), "%" PRIu64, ls->t.seminfo.i);
push_string(ls, buf);
add_field(ls, -2, "stringvalue");

Expand Down Expand Up @@ -2053,7 +2053,7 @@ static void converttokentolua(LexState *ls, Token *t) {
lua_pushstring(ls->L, "<number>");
lua_setfield(ls->L, -2, "type");
int flags = t->seminfo.flags;
number_type(ls, flags, &buf[0]);
number_type(ls, flags, &buf[0], sizeof(buf));
push_type(ls, buf);
lua_setfield(ls->L, -2, "valuetype");
if (flags & F_IS8BYTES && flags & F_ISINTEGER) {
Expand Down
2 changes: 1 addition & 1 deletion src/tcwrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ class IncludeCVisitor : public RecursiveASTVisitor<IncludeCVisitor> {
std::string declstr;
if (it->isAnonymousStructOrUnion()) {
char buf[32];
sprintf(buf, "_%d", anonname++);
snprintf(buf, sizeof(buf), "_%d", anonname++);
declstr = buf;
} else {
declstr = declname.getAsString();
Expand Down
5 changes: 3 additions & 2 deletions src/terra.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -572,8 +572,9 @@ int terra_loadfile(lua_State *L, const char *file) {
if (c == '\n') ungetc(c, ctx.fp); /* keep line count accurate */
}
if (file) {
char *name = (char *)malloc(strlen(file) + 2);
sprintf(name, "@%s", file);
size_t name_size = strlen(file) + 2;
char *name = (char *)malloc(name_size);
snprintf(name, name_size, "@%s", file);
int r = terra_load(L, reader_file, &ctx, name);
free(name);
fclose(ctx.fp);
Expand Down
Loading