mirror of
https://git.code.sf.net/p/quake/quakeforge
synced 2024-11-14 00:40:55 +00:00
[quakefs] Reimplement QFS_NextFilename to be more secure
It's not there yet as it promptly closes the file and returns only the filename (and then only the portion within the user's directory tree). However, this worked nicely as a test for Sys_UniqueFile.
This commit is contained in:
parent
a35bfef24c
commit
8cdabc8905
2 changed files with 11 additions and 17 deletions
|
@ -1557,26 +1557,19 @@ QFS_SetExtension (struct dstring_s *path, const char *extension)
|
||||||
VISIBLE int
|
VISIBLE int
|
||||||
QFS_NextFilename (dstring_t *filename, const char *prefix, const char *ext)
|
QFS_NextFilename (dstring_t *filename, const char *prefix, const char *ext)
|
||||||
{
|
{
|
||||||
char *digits;
|
|
||||||
int i;
|
|
||||||
int ret = 0;
|
int ret = 0;
|
||||||
dstring_t *full_path = dstring_new ();
|
dstring_t *full_path = dstring_new ();
|
||||||
|
|
||||||
dsprintf (filename, "%s0000%s", prefix, ext);
|
if (qfs_expand_userpath (full_path, "") == -1) {
|
||||||
digits = filename->str + strlen (prefix);
|
dsprintf (filename, "failed to expand userpath");
|
||||||
|
} else {
|
||||||
for (i = 0; i <= 9999; i++) {
|
size_t qfs_pos = strlen (full_path->str);
|
||||||
digits[0] = i / 1000 + '0';
|
dstring_appendstr (full_path, prefix);
|
||||||
digits[1] = i / 100 % 10 + '0';
|
int fd = Sys_UniqueFile (filename, full_path->str, ext, 4);
|
||||||
digits[2] = i / 10 % 10 + '0';
|
if (fd >= 0) {
|
||||||
digits[3] = i % 10 + '0';
|
dstring_snip (filename, 0, qfs_pos);
|
||||||
|
close (fd);
|
||||||
if (qfs_expand_userpath (full_path, filename->str) == -1)
|
|
||||||
break;
|
|
||||||
if (Sys_FileExists (full_path->str) == -1) {
|
|
||||||
// file doesn't exist, so we can use this name
|
|
||||||
ret = 1;
|
ret = 1;
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
dstring_delete (full_path);
|
dstring_delete (full_path);
|
||||||
|
|
|
@ -365,7 +365,8 @@ ScreenShot_f (void)
|
||||||
// find a file name to save it to
|
// find a file name to save it to
|
||||||
if (!QFS_NextFilename (name, va (0, "%s/qf",
|
if (!QFS_NextFilename (name, va (0, "%s/qf",
|
||||||
qfs_gamedir->dir.shots), ".png")) {
|
qfs_gamedir->dir.shots), ".png")) {
|
||||||
Sys_Printf ("SCR_ScreenShot_f: Couldn't create a PNG file\n");
|
Sys_Printf ("SCR_ScreenShot_f: Couldn't create a PNG file: %s\n",
|
||||||
|
name->str);
|
||||||
} else {
|
} else {
|
||||||
tex_t *tex;
|
tex_t *tex;
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue