AdvanceLevel implementation; sprintf hardening

This commit is contained in:
Sebastian Krzyszkowiak 2013-02-25 01:39:41 +01:00
parent 3ba92508e2
commit 840202f758
7 changed files with 29 additions and 29 deletions

View file

@ -50,7 +50,7 @@ void AnimPage(struct Game *game, struct IntroResources *data, int page, ALLEGRO_
void FillPage(struct Game *game, struct IntroResources *data, int page) {
char filename[30] = { };
sprintf(filename, "intro/%d.flac", page);
snprintf(filename, 30, "intro/%d.flac", page);
data->audiostream = al_load_audio_stream(GetDataFilePath(game, filename), 4, 1024);
al_attach_audio_stream_to_mixer(data->audiostream, game->audio.voice);

View file

@ -20,6 +20,7 @@
*/
#include "../utils.h"
#include "../timeline.h"
#include "../config.h"
#include "level.h"
#include <stdio.h>
@ -63,7 +64,7 @@ void RegisterSpritesheet(struct Game *game, struct Character *character, char* n
}
PrintConsole(game, "Registering %s spritesheet: %s", character->name, name);
char filename[255] = { };
sprintf(filename, "levels/%s/%s.ini", character->name, name);
snprintf(filename, 255, "levels/%s/%s.ini", character->name, name);
ALLEGRO_CONFIG *config = al_load_config_file(GetDataFilePath(game, filename));
s = malloc(sizeof(struct Spritesheet));
s->name = strdup(name);
@ -85,23 +86,22 @@ void RegisterSpritesheet(struct Game *game, struct Character *character, char* n
al_destroy_config(config);
}
/*
void Level_Passed(struct Game *game) {
if (game->level.current_level<6) {
int available = atoi(GetConfigOptionDefault("MuffinAttack", "level", "1"));
void AdvanceLevel(struct Game *game, int current_level, bool last) {
if (last) {
int available = atoi(GetConfigOptionDefault(game, "MuffinAttack", "level", "1"));
available++;
if ((available<2) || (available>7)) available=1;
if (available==(game->level.current_level+1)) {
char* text = malloc(2*sizeof(char));
sprintf(text, "%d", available);
SetConfigOption("MuffinAttack", "level", text);
if (available==(current_level+1)) {
char* text = malloc(255*sizeof(char));
snprintf(text, 255, "%d", available);
SetConfigOption(game, "MuffinAttack", "level", text);
free(text);
}
} else {
SetConfigOption("MuffinAttack", "completed", "1");
SetConfigOption(game, "MuffinAttack", "completed", "1");
}
}
/*
void Level_Logic(struct Game *game) {
LEVELS(Logic, game);

View file

@ -51,7 +51,7 @@ struct Character {
void SelectSpritesheet(struct Game *game, struct Character *character, char* name);
void RegisterSpritesheet(struct Game *game, struct Character *character, char* name);
void AdvanceLevel(struct Game *game);
void AdvanceLevel(struct Game *game, int current_level, bool last);
/*! \brief Replaces first '?' char in filename with current level number. */
char* GetLevelFilename(struct Game *game, char* filename);

View file

@ -81,8 +81,8 @@ void Gamestate_ProcessEvent(struct Game *game, struct MapResources* data, ALLEGR
al_play_sample_instance(data->click);
//game->level.input.current_level = data->selected;
PrintConsole(game, "Selecting level %d...", data->selected);
char gamestate[7] = {};
sprintf(gamestate, "level%d", data->selected);
char gamestate[255] = {};
snprintf(gamestate, 255, "level%d", data->selected);
SwitchGamestate(game, "map", gamestate);
return;
} else if (ev->keyboard.keycode == ALLEGRO_KEY_ESCAPE) {
@ -106,7 +106,7 @@ void* Gamestate_Load(struct Game *game, void (*progress)(struct Game*)) {
data->map_bg = LoadScaledBitmap(game, "map/background.png", game->viewport.height*1.6, game->viewport.height);
(*progress)(game);
char filename[30] = { };
sprintf(filename, "map/highlight%d.png", data->available);
snprintf(filename, 30, "map/highlight%d.png", data->available);
data->highlight = LoadScaledBitmap(game, filename, game->viewport.height*1.6, game->viewport.height);
(*progress)(game);

View file

@ -54,15 +54,15 @@ void DrawMenuState(struct Game *game, struct MenuResources *data) {
break;
case MENUSTATE_AUDIO:
font = data->font; if (data->selected==0) font = data->font_selected;
if (game->config.music) sprintf(text, "Music volume: %d0%%", game->config.music);
if (game->config.music) snprintf(text, 255, "Music volume: %d0%%", game->config.music);
else sprintf(text, "Music disabled");
DrawTextWithShadow(font, al_map_rgb(255,255,255), game->viewport.width*0.5, game->viewport.height*0.5, ALLEGRO_ALIGN_CENTRE, text);
font = data->font; if (data->selected==1) font = data->font_selected;
if (game->config.fx) sprintf(text, "Effects volume: %d0%%", game->config.fx);
if (game->config.fx) snprintf(text, 255, "Effects volume: %d0%%", game->config.fx);
else sprintf(text, "Effects disabled");
DrawTextWithShadow(font, al_map_rgb(255,255,255), game->viewport.width*0.5, game->viewport.height*0.6, ALLEGRO_ALIGN_CENTRE, text);
font = data->font; if (data->selected==2) font = data->font_selected;
if (game->config.voice) sprintf(text, "Voice volume: %d0%%", game->config.voice);
if (game->config.voice) snprintf(text, 255, "Voice volume: %d0%%", game->config.voice);
else sprintf(text, "Voice disabled");
DrawTextWithShadow(font, al_map_rgb(255,255,255), game->viewport.width*0.5, game->viewport.height*0.7, ALLEGRO_ALIGN_CENTRE, text);
font = data->font; if (data->selected==3) font = data->font_selected;
@ -392,21 +392,21 @@ void Gamestate_ProcessEvent(struct Game *game, struct MenuResources* data, ALLEG
case 0:
game->config.music--;
if (game->config.music<0) game->config.music=10;
sprintf(text, "%d", game->config.music);
snprintf(text, 255, "%d", game->config.music);
SetConfigOption(game, "SuperDerpy", "music", text);
al_set_mixer_gain(game->audio.music, game->config.music/10.0);
break;
case 1:
game->config.fx--;
if (game->config.fx<0) game->config.fx=10;
sprintf(text, "%d", game->config.fx);
snprintf(text, 255, "%d", game->config.fx);
SetConfigOption(game, "SuperDerpy", "fx", text);
al_set_mixer_gain(game->audio.fx, game->config.fx/10.0);
break;
case 2:
game->config.voice--;
if (game->config.voice<0) game->config.voice=10;
sprintf(text, "%d", game->config.voice);
snprintf(text, 255, "%d", game->config.voice);
SetConfigOption(game, "SuperDerpy", "voice", text);
al_set_mixer_gain(game->audio.voice, game->config.voice/10.0);
break;

View file

@ -46,7 +46,7 @@ void DrawConsole(struct Game *game) {
game->_priv.fps_count.old_time = game_time;
}
char sfps[6] = { };
sprintf(sfps, "%.0f", game->_priv.fps_count.fps);
snprintf(sfps, 6, "%.0f", game->_priv.fps_count.fps);
DrawTextWithShadow(game->_priv.font, al_map_rgb(255,255,255), game->viewport.width*0.99, 0, ALLEGRO_ALIGN_RIGHT, sfps);
}
game->_priv.fps_count.frames_done++;
@ -299,7 +299,7 @@ int main(int argc, char **argv){
free(gamestate);
char libname[1024] = {};
sprintf(libname, "libsuperderpy-%s-loading.so", "muffinattack");
snprintf(libname, 1024, "libsuperderpy-%s-loading.so", "muffinattack");
void *handle = dlopen(libname,RTLD_NOW);
if (!handle) {
FatalError(&game, true, "Error while initializing loading screen %s", dlerror());
@ -360,7 +360,7 @@ int main(int argc, char **argv){
PrintConsole(&game, "Loading gamestate \"%s\"...", tmp->name);
// TODO: take proper game name
char libname[1024];
sprintf(libname, "libsuperderpy-%s-%s.so", "muffinattack", tmp->name);
snprintf(libname, 1024, "libsuperderpy-%s-%s.so", "muffinattack", tmp->name);
tmp->handle = dlopen(libname,RTLD_NOW);
if (!tmp->handle) {
//PrintConsole(&game, "Error while loading gamestate \"%s\": %s", tmp->name, dlerror());
@ -482,7 +482,7 @@ int main(int argc, char **argv){
} else if ((ev.type == ALLEGRO_EVENT_KEY_DOWN) && (game.config.debug) && (ev.keyboard.keycode == ALLEGRO_KEY_F12)) {
ALLEGRO_PATH *path = al_get_standard_path(ALLEGRO_USER_DOCUMENTS_PATH);
char filename[255] = { };
sprintf(filename, "SuperDerpy_%ld_%ld.png", time(NULL), clock());
snprintf(filename, 255, "SuperDerpy_%ld_%ld.png", time(NULL), clock());
al_set_path_filename(path, filename);
al_save_bitmap(al_path_cstr(path, ALLEGRO_NATIVE_PATH_SEP), al_get_backbuffer(game.display));
PrintConsole(&game, "Screenshot stored in %s", al_path_cstr(path, ALLEGRO_NATIVE_PATH_SEP));

View file

@ -167,7 +167,7 @@ void FatalError(struct Game *game, bool fatal, char* format, ...) {
if (!game->_priv.console) {
va_list vl;
va_start(vl, format);
vsprintf(text, format, vl);
vsnprintf(text, 1024, format, vl);
va_end(vl);
printf("%s\n", text);
if (!game->_priv.font_console) exit(1);
@ -175,7 +175,7 @@ void FatalError(struct Game *game, bool fatal, char* format, ...) {
PrintConsole(game, "Fatal Error, displaying BSOD...");
va_list vl;
va_start(vl, format);
vsprintf(text, format, vl);
vsnprintf(text, 1024, format, vl);
va_end(vl);
PrintConsole(game, text);
}
@ -282,7 +282,7 @@ void PrintConsole(struct Game *game, char* format, ...) {
va_list vl;
va_start(vl, format);
char text[1024] = {};
vsprintf(text, format, vl);
vsnprintf(text, 1024, format, vl);
va_end(vl);
if (game->config.debug) { printf("%s\n", text); fflush(stdout); }
ALLEGRO_BITMAP *con = al_create_bitmap(al_get_bitmap_width(game->_priv.console), al_get_bitmap_height(game->_priv.console));