44
55#include < stdlib.h>
66#include < string.h>
7+ #include < stdint.h>
78#include < file/file_path.h>
89#include < streams/file_stream.h>
910#include < vfs/vfs.h>
2627static bool spirv_cache_get_dir (char *cache_dir_out, size_t cache_dir_out_len)
2728{
2829 settings_t *settings = config_get_ptr ();
29- int ret;
3030
3131 if (!settings || !settings->paths .directory_cache [0 ])
3232 return false ;
3333
3434 /* Build the spirv subdirectory path */
35- ret = snprintf (cache_dir_out, cache_dir_out_len, " %s/%s" ,
36- settings->paths .directory_cache , SPIRV_CACHE_SUBDIR);
37-
38- /* Check if snprintf truncated the output */
39- if (ret < 0 || (size_t )ret >= cache_dir_out_len)
40- return false ;
35+ fill_pathname_join_special (cache_dir_out,
36+ settings->paths .directory_cache , SPIRV_CACHE_SUBDIR,
37+ cache_dir_out_len);
4138
4239 return true ;
4340}
@@ -69,17 +66,14 @@ static bool spirv_cache_get_filename(const char *hash,
6966 char *cache_file_out, size_t cache_file_out_len)
7067{
7168 char cache_dir[PATH_MAX_LENGTH];
72- int ret ;
69+ char hash_filename[ 128 ] ;
7370
7471 if (!spirv_cache_get_dir (cache_dir, sizeof (cache_dir)))
7572 return false ;
7673
77- ret = snprintf (cache_file_out, cache_file_out_len, " %s/%s.spirv" ,
78- cache_dir, hash);
79-
80- /* Check if snprintf truncated the output */
81- if (ret < 0 || (size_t )ret >= cache_file_out_len)
82- return false ;
74+ snprintf (hash_filename, sizeof (hash_filename), " %s.spirv" , hash);
75+ fill_pathname_join_special (cache_file_out, cache_dir, hash_filename,
76+ cache_file_out_len);
8377
8478 return true ;
8579}
@@ -93,12 +87,15 @@ static bool spirv_cache_get_filename(const char *hash,
9387 */
9488static bool spirv_cache_write_string (RFILE *file, const std::string &str)
9589{
96- uint32_t len = str.length ();
90+ uint32_t _len;
91+ if (str.length () > UINT32_MAX)
92+ return false ;
93+ _len = (uint32_t )str.length ();
9794
98- if (filestream_write (file, &len , sizeof (uint32_t )) != sizeof (uint32_t ))
95+ if (filestream_write (file, &_len , sizeof (uint32_t )) != sizeof (uint32_t ))
9996 return false ;
10097
101- if (len > 0 && filestream_write (file, str.c_str (), len ) != len )
98+ if (_len > 0 && filestream_write (file, str.c_str (), _len ) != _len )
10299 return false ;
103100
104101 return true ;
@@ -113,29 +110,30 @@ static bool spirv_cache_write_string(RFILE *file, const std::string &str)
113110 */
114111static bool spirv_cache_read_string (RFILE *file, std::string &str_out)
115112{
116- uint32_t len;
113+ uint32_t _len;
114+ char *buf;
117115
118- if (filestream_read (file, &len , sizeof (uint32_t )) != sizeof (uint32_t ))
116+ if (filestream_read (file, &_len , sizeof (uint32_t )) != sizeof (uint32_t ))
119117 return false ;
120118
121- if (len == 0 )
119+ if (_len == 0 )
122120 {
123121 str_out.clear ();
124122 return true ;
125123 }
126124
127125 /* Allocate and read string */
128- char * buf = new char [len + 1 ];
126+ buf = new char [_len + 1 ];
129127 if (!buf)
130128 return false ;
131129
132- if (filestream_read (file, buf, len ) != len )
130+ if (filestream_read (file, buf, _len ) != _len )
133131 {
134132 delete[] buf;
135133 return false ;
136134 }
137135
138- buf[len ] = ' \0 ' ;
136+ buf[_len ] = ' \0 ' ;
139137 str_out = buf;
140138 delete[] buf;
141139
@@ -144,18 +142,18 @@ static bool spirv_cache_read_string(RFILE *file, std::string &str_out)
144142
145143extern " C" {
146144
147- bool spirv_cache_compute_hash (const char *vertex_source, const char *fragment_source,
148- char *hash_out)
145+ bool spirv_cache_compute_hash (const char *vertex_source, const char *fragment_source, char *hash_out)
149146{
147+ uint8_t *combined;
148+ size_t vertex_len, fragment_len, total_len;
150149 if (!vertex_source || !fragment_source || !hash_out)
151150 return false ;
152151
153152 /* Build combined hash input: vertex + "|" + fragment */
154- size_t vertex_len = strlen (vertex_source);
155- size_t fragment_len = strlen (fragment_source);
156- size_t total_len = vertex_len + 1 + fragment_len; /* 1 for "|" separator */
157-
158- uint8_t *combined = new uint8_t [total_len];
153+ vertex_len = strlen (vertex_source);
154+ fragment_len = strlen (fragment_source);
155+ total_len = vertex_len + 1 + fragment_len; /* 1 for "|" separator */
156+ combined = new uint8_t [total_len];
159157 if (!combined)
160158 return false ;
161159
@@ -173,8 +171,8 @@ bool spirv_cache_compute_hash(const char *vertex_source, const char *fragment_so
173171bool spirv_cache_load (const char *hash, struct glslang_output *output)
174172{
175173 RFILE *file;
176- char cache_file[PATH_MAX_LENGTH];
177174 uint8_t version;
175+ char cache_file[PATH_MAX_LENGTH];
178176 uint32_t vertex_size, fragment_size, param_count, i;
179177 uint16_t rt_format;
180178
@@ -269,10 +267,10 @@ bool spirv_cache_load(const char *hash, struct glslang_output *output)
269267bool spirv_cache_save (const char *hash, const struct glslang_output *output)
270268{
271269 RFILE *file;
270+ uint16_t rt_format;
272271 char cache_file[PATH_MAX_LENGTH];
273272 uint8_t version = SPIRV_CACHE_VERSION;
274273 uint32_t vertex_size, fragment_size, param_count, i;
275- uint16_t rt_format;
276274
277275 if (!hash || !output)
278276 return false ;
@@ -294,7 +292,9 @@ bool spirv_cache_save(const char *hash, const struct glslang_output *output)
294292 goto error;
295293
296294 /* Write vertex SPIR-V */
297- vertex_size = output->vertex .size ();
295+ if (output->vertex .size () > UINT32_MAX)
296+ goto error;
297+ vertex_size = (uint32_t )output->vertex .size ();
298298 if (filestream_write (file, &vertex_size, sizeof (uint32_t )) != sizeof (uint32_t ))
299299 goto error;
300300 if (vertex_size > 0 )
@@ -304,7 +304,9 @@ bool spirv_cache_save(const char *hash, const struct glslang_output *output)
304304 }
305305
306306 /* Write fragment SPIR-V */
307- fragment_size = output->fragment .size ();
307+ if (output->fragment .size () > UINT32_MAX)
308+ goto error;
309+ fragment_size = (uint32_t )output->fragment .size ();
308310 if (filestream_write (file, &fragment_size, sizeof (uint32_t )) != sizeof (uint32_t ))
309311 goto error;
310312 if (fragment_size > 0 )
@@ -314,7 +316,9 @@ bool spirv_cache_save(const char *hash, const struct glslang_output *output)
314316 }
315317
316318 /* Write parameters */
317- param_count = output->meta .parameters .size ();
319+ if (output->meta .parameters .size () > UINT32_MAX)
320+ goto error;
321+ param_count = (uint32_t )output->meta .parameters .size ();
318322 if (filestream_write (file, ¶m_count, sizeof (uint32_t )) != sizeof (uint32_t ))
319323 goto error;
320324
@@ -359,3 +363,4 @@ bool spirv_cache_save(const char *hash, const struct glslang_output *output)
359363}
360364
361365} /* extern "C" */
366+
0 commit comments