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>
@@ -83,12 +84,15 @@ static bool spirv_cache_get_filename(const char *hash,
8384 */
8485static bool spirv_cache_write_string (RFILE *file, const std::string &str)
8586{
86- uint32_t len = str.length ();
87+ uint32_t _len;
88+ if (str.length () > UINT32_MAX)
89+ return false ;
90+ _len = (uint32_t )str.length ();
8791
88- if (filestream_write (file, &len , sizeof (uint32_t )) != sizeof (uint32_t ))
92+ if (filestream_write (file, &_len , sizeof (uint32_t )) != sizeof (uint32_t ))
8993 return false ;
9094
91- if (len > 0 && filestream_write (file, str.c_str (), len ) != len )
95+ if (_len > 0 && filestream_write (file, str.c_str (), _len ) != _len )
9296 return false ;
9397
9498 return true ;
@@ -103,29 +107,30 @@ static bool spirv_cache_write_string(RFILE *file, const std::string &str)
103107 */
104108static bool spirv_cache_read_string (RFILE *file, std::string &str_out)
105109{
106- uint32_t len;
110+ uint32_t _len;
111+ char *buf;
107112
108- if (filestream_read (file, &len , sizeof (uint32_t )) != sizeof (uint32_t ))
113+ if (filestream_read (file, &_len , sizeof (uint32_t )) != sizeof (uint32_t ))
109114 return false ;
110115
111- if (len == 0 )
116+ if (_len == 0 )
112117 {
113118 str_out.clear ();
114119 return true ;
115120 }
116121
117122 /* Allocate and read string */
118- char * buf = new char [len + 1 ];
123+ buf = new char [_len + 1 ];
119124 if (!buf)
120125 return false ;
121126
122- if (filestream_read (file, buf, len ) != len )
127+ if (filestream_read (file, buf, _len ) != _len )
123128 {
124129 delete[] buf;
125130 return false ;
126131 }
127132
128- buf[len ] = ' \0 ' ;
133+ buf[_len ] = ' \0 ' ;
129134 str_out = buf;
130135 delete[] buf;
131136
@@ -134,18 +139,18 @@ static bool spirv_cache_read_string(RFILE *file, std::string &str_out)
134139
135140extern " C" {
136141
137- bool spirv_cache_compute_hash (const char *vertex_source, const char *fragment_source,
138- char *hash_out)
142+ bool spirv_cache_compute_hash (const char *vertex_source, const char *fragment_source, char *hash_out)
139143{
144+ uint8_t *combined;
145+ size_t vertex_len, fragment_len, total_len;
140146 if (!vertex_source || !fragment_source || !hash_out)
141147 return false ;
142148
143149 /* Build combined hash input: vertex + "|" + fragment */
144- size_t vertex_len = strlen (vertex_source);
145- size_t fragment_len = strlen (fragment_source);
146- size_t total_len = vertex_len + 1 + fragment_len; /* 1 for "|" separator */
147-
148- uint8_t *combined = new uint8_t [total_len];
150+ vertex_len = strlen (vertex_source);
151+ fragment_len = strlen (fragment_source);
152+ total_len = vertex_len + 1 + fragment_len; /* 1 for "|" separator */
153+ combined = new uint8_t [total_len];
149154 if (!combined)
150155 return false ;
151156
@@ -163,8 +168,8 @@ bool spirv_cache_compute_hash(const char *vertex_source, const char *fragment_so
163168bool spirv_cache_load (const char *hash, struct glslang_output *output)
164169{
165170 RFILE *file;
166- char cache_file[PATH_MAX_LENGTH];
167171 uint8_t version;
172+ char cache_file[PATH_MAX_LENGTH];
168173 uint32_t vertex_size, fragment_size, param_count, i;
169174 uint16_t rt_format;
170175
@@ -259,10 +264,10 @@ bool spirv_cache_load(const char *hash, struct glslang_output *output)
259264bool spirv_cache_save (const char *hash, const struct glslang_output *output)
260265{
261266 RFILE *file;
267+ uint16_t rt_format;
262268 char cache_file[PATH_MAX_LENGTH];
263269 uint8_t version = SPIRV_CACHE_VERSION;
264270 uint32_t vertex_size, fragment_size, param_count, i;
265- uint16_t rt_format;
266271
267272 if (!hash || !output)
268273 return false ;
@@ -284,7 +289,9 @@ bool spirv_cache_save(const char *hash, const struct glslang_output *output)
284289 goto error;
285290
286291 /* Write vertex SPIR-V */
287- vertex_size = output->vertex .size ();
292+ if (output->vertex .size () > UINT32_MAX)
293+ goto error;
294+ vertex_size = (uint32_t )output->vertex .size ();
288295 if (filestream_write (file, &vertex_size, sizeof (uint32_t )) != sizeof (uint32_t ))
289296 goto error;
290297 if (vertex_size > 0 )
@@ -294,7 +301,9 @@ bool spirv_cache_save(const char *hash, const struct glslang_output *output)
294301 }
295302
296303 /* Write fragment SPIR-V */
297- fragment_size = output->fragment .size ();
304+ if (output->fragment .size () > UINT32_MAX)
305+ goto error;
306+ fragment_size = (uint32_t )output->fragment .size ();
298307 if (filestream_write (file, &fragment_size, sizeof (uint32_t )) != sizeof (uint32_t ))
299308 goto error;
300309 if (fragment_size > 0 )
@@ -304,7 +313,9 @@ bool spirv_cache_save(const char *hash, const struct glslang_output *output)
304313 }
305314
306315 /* Write parameters */
307- param_count = output->meta .parameters .size ();
316+ if (output->meta .parameters .size () > UINT32_MAX)
317+ goto error;
318+ param_count = (uint32_t )output->meta .parameters .size ();
308319 if (filestream_write (file, ¶m_count, sizeof (uint32_t )) != sizeof (uint32_t ))
309320 goto error;
310321
0 commit comments