Skip to content

Commit f402911

Browse files
committed
Helpers: Add file path validation
Add vk::IsValidFilePath() helper to validate file paths before opening. Checks for null/empty paths, file existence, and regular file type.
1 parent 1222fe4 commit f402911

9 files changed

Lines changed: 68 additions & 23 deletions

File tree

common/libs/VkCodecUtils/DecoderConfig.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -200,16 +200,17 @@ struct DecoderConfig {
200200
}},
201201
{"--input", "-i", 1, "Input filename to decode",
202202
[this](const char **args, const ProgramArgs &a) {
203-
videoFileName = args[0];
204-
std::ifstream inputFile(videoFileName, std::ifstream::in);
205-
if(!inputFile.is_open()) {
206-
std::cerr << "Error: Cannot open input file \"" << videoFileName << "\"" << std::endl;
203+
if (!vk::IsValidFilePath(args[0], true)) {
207204
return false;
208205
}
206+
videoFileName = args[0];
209207
return true;
210208
}},
211209
{"--output", "-o", 1, "Output filename to dump raw video to",
212210
[this](const char **args, const ProgramArgs &a) {
211+
if (!vk::IsValidFilePath(args[0], false)) {
212+
return false;
213+
}
213214
outputFileName = args[0];
214215
return true;
215216
}},
@@ -310,6 +311,9 @@ struct DecoderConfig {
310311
}},
311312
{"--crcoutfile", nullptr, 1, "Output file to store the CRC output into.",
312313
[this](const char **args, const ProgramArgs &a) {
314+
if (!vk::IsValidFilePath(args[0], false)) {
315+
return false;
316+
}
313317
crcOutputFileName = args[0];
314318
return true;
315319
}},

common/libs/VkCodecUtils/Helpers.h

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <iostream>
2525
#include <cstring>
2626
#include <iomanip>
27+
#include <filesystem>
2728
#include "HelpersDispatchTable.h"
2829

2930
namespace vk {
@@ -443,6 +444,47 @@ class DeviceUuidUtils
443444
uint32_t m_deviceUuidIsValid : 1;
444445
};
445446

447+
// File validation helper
448+
inline bool IsValidFilePath(const char *pFilePath, bool checkExists = true) {
449+
if (!pFilePath || pFilePath[0] == '\0') {
450+
std::cerr << "Error: File path is null or empty" << std::endl;
451+
return false;
452+
}
453+
454+
try {
455+
std::filesystem::path path(pFilePath);
456+
457+
if (checkExists) {
458+
if (!std::filesystem::exists(path)) {
459+
std::cerr << "Error: File does not exist: " << pFilePath << std::endl;
460+
return false;
461+
}
462+
463+
if (std::filesystem::is_directory(path)) {
464+
std::cerr << "Error: Path is a directory, not a file: " << pFilePath << std::endl;
465+
return false;
466+
}
467+
468+
if (!std::filesystem::is_regular_file(path)) {
469+
std::cerr << "Error: Path is not a regular file: " << pFilePath << std::endl;
470+
return false;
471+
}
472+
} else {
473+
// For output files, verify parent directory exists
474+
auto parent = path.parent_path();
475+
if (!parent.empty() && !std::filesystem::exists(parent)) {
476+
std::cerr << "Error: Parent directory does not exist: " << parent << std::endl;
477+
return false;
478+
}
479+
}
480+
481+
return true;
482+
} catch (const std::filesystem::filesystem_error& e) {
483+
std::cerr << "Error: Invalid file path (" << e.what() << "): " << pFilePath << std::endl;
484+
return false;
485+
}
486+
}
487+
446488
} // namespace vk
447489

448490
#endif // HELPERS_H

vk_video_decoder/demos/vk-video-dec/Main.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ int main(int argc, const char **argv)
4343
decoderConfig.initialBitdepth,
4444
videoStreamDemuxer);
4545
if (result != VK_SUCCESS) {
46-
assert(!"Can't initialize the VideoStreamDemuxer!");
46+
fprintf(stderr, "Error: Failed to initialize VideoStreamDemuxer for file: %s\n",
47+
decoderConfig.videoFileName.c_str());
4748
return -1;
4849
}
4950

vk_video_decoder/libs/VkDecoderUtils/VideoStreamDemuxer.cpp

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,6 @@
1818
#include <fstream>
1919
#include "VkDecoderUtils/VideoStreamDemuxer.h"
2020

21-
bool VideoStreamDemuxer::CheckFile(const char* szInFilePath)
22-
{
23-
std::ifstream fpIn(szInFilePath, std::ios::in | std::ios::binary);
24-
if (fpIn.fail()) {
25-
std::ostringstream err;
26-
err << "Unable to open input file: " << szInFilePath << std::endl;
27-
throw std::invalid_argument(err.str());
28-
}
29-
return true;
30-
}
3121

3222
VkResult VideoStreamDemuxer::Create(const char *pFilePath,
3323
VkVideoCodecOperationFlagBitsKHR codecType,
@@ -37,8 +27,6 @@ VkResult VideoStreamDemuxer::Create(const char *pFilePath,
3727
int32_t defaultBitDepth,
3828
VkSharedBaseObj<VideoStreamDemuxer>& videoStreamDemuxer)
3929
{
40-
VideoStreamDemuxer::CheckFile(pFilePath);
41-
4230
#ifdef FFMPEG_DEMUXER_SUPPORT
4331
if (requiresStreamDemuxing || (codecType == VK_VIDEO_CODEC_OPERATION_NONE_KHR)) {
4432
return FFmpegDemuxerCreate(pFilePath,

vk_video_decoder/libs/VkDecoderUtils/VideoStreamDemuxer.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,6 @@ class VideoStreamDemuxer : public VkVideoRefCountBase {
3434
int32_t defaultBitDepth = 12,
3535
VkSharedBaseObj<VideoStreamDemuxer>& videoStreamDemuxer = invalidDemuxer);
3636

37-
static bool CheckFile(const char* szInFilePath);
38-
3937
virtual int32_t AddRef()
4038
{
4139
return ++m_refCount;

vk_video_decoder/test/vulkan-video-dec/Main.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,9 @@ int main(int argc, const char** argv)
7070
decoderConfig.initialBitdepth,
7171
videoStreamDemuxer);
7272
if (result != VK_SUCCESS) {
73-
assert(!"Can't initialize the VideoStreamDemuxer!");
74-
return result;
73+
fprintf(stderr, "Error: Failed to initialize VideoStreamDemuxer for file: %s\n",
74+
decoderConfig.videoFileName.c_str());
75+
return -1;
7576
}
7677

7778
const int32_t numDecodeQueues = ((decoderConfig.queueId != 0) ||

vk_video_decoder/test/vulkan-video-simple-dec/Main.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,9 @@ int main(int argc, const char** argv)
133133
videoStreamDemuxer);
134134

135135
if (result != VK_SUCCESS) {
136-
assert(!"Can't initialize the VideoStreamDemuxer!");
137-
return result;
136+
fprintf(stderr, "Error: Failed to initialize VideoStreamDemuxer for file: %s\n",
137+
decoderConfig.videoFileName.c_str());
138+
return -1;
138139
}
139140

140141
VkSharedBaseObj<VkVideoFrameOutput> frameToFile;

vk_video_encoder/libs/VkVideoEncoder/VkEncoderConfig.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,9 @@ int EncoderConfig::ParseArguments(int argc, char *argv[])
172172
fprintf(stderr, "invalid parameter for %s\n", args[i - 1].c_str());
173173
return -1;
174174
}
175+
if (!vk::IsValidFilePath(args[i].c_str(), true)) {
176+
return -1;
177+
}
175178
size_t fileSize = inputFileHandler.SetFileName(args[i].c_str());
176179
if (fileSize <= 0) {
177180
return (int)fileSize;
@@ -186,6 +189,9 @@ int EncoderConfig::ParseArguments(int argc, char *argv[])
186189
fprintf(stderr, "invalid parameter for %s\n", args[i - 1].c_str());
187190
return -1;
188191
}
192+
if (!vk::IsValidFilePath(args[i].c_str(), false)) {
193+
return -1;
194+
}
189195
size_t fileSize = outputFileHandler.SetFileName(args[i].c_str());
190196
if (fileSize <= 0) {
191197
return (int)fileSize;
@@ -498,6 +504,9 @@ int EncoderConfig::ParseArguments(int argc, char *argv[])
498504
fprintf(stderr, "Invaid paramter for %s\n", args[i - 1].c_str());
499505
return -1;
500506
}
507+
if (!vk::IsValidFilePath(args[i].c_str(), true)) {
508+
return -1;
509+
}
501510
size_t fileSize = qpMapFileHandler.SetFileName(args[i].c_str());
502511
if (fileSize <= 0) {
503512
return (int)fileSize;

vk_video_encoder/libs/VkVideoEncoder/VkEncoderConfig.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <atomic>
2323
#include "mio/mio.hpp"
2424
#include "VkCodecUtils/VkVideoRefCountBase.h"
25+
#include "VkCodecUtils/Helpers.h"
2526
#include "VkVideoEncoder/VkVideoEncoderDef.h"
2627
#include "VkVideoEncoder/VkVideoGopStructure.h"
2728
#include "VkVideoCore/VkVideoCoreProfile.h"

0 commit comments

Comments
 (0)