33#define CACHE_TTL_SECONDS 3600
44#define PACKAGE_CACHE_FILE "packages.cache"
55
6+ static void free_command_cache (char * * commands , int count ) {
7+ if (!commands ) {
8+ return ;
9+ }
10+
11+ for (int i = 0 ; i < count ; i ++ ) {
12+ free (commands [i ]);
13+ }
14+ free (commands );
15+ }
16+
617static int is_cache_valid (const char * cache_path ) {
718 struct stat cache_stat ;
819 if (stat (cache_path , & cache_stat ) != 0 ) {
@@ -35,12 +46,16 @@ static void load_cache_from_file(const char *cache_path) {
3546 char * * new_commands =
3647 realloc (cached_commands , sizeof (char * ) * (command_count + 1 ));
3748 if (!new_commands ) {
49+ free_command_cache (cached_commands , command_count - 1 );
50+ cached_commands = NULL ;
3851 fclose (fp );
3952 return ;
4053 }
4154 cached_commands = new_commands ;
4255 cached_commands [command_count - 1 ] = strdup (line );
4356 if (!cached_commands [command_count - 1 ]) {
57+ free_command_cache (cached_commands , command_count - 1 );
58+ cached_commands = NULL ;
4459 fclose (fp );
4560 return ;
4661 }
@@ -54,11 +69,15 @@ static void load_cache_from_file(const char *cache_path) {
5469 char * * new_commands =
5570 realloc (cached_commands , sizeof (char * ) * (command_count + 1 ));
5671 if (!new_commands ) {
72+ free_command_cache (cached_commands , command_count - 1 );
73+ cached_commands = NULL ;
5774 return ;
5875 }
5976 cached_commands = new_commands ;
6077 cached_commands [command_count - 1 ] = strdup (custom_cmds [i ]);
6178 if (!cached_commands [command_count - 1 ]) {
79+ free_command_cache (cached_commands , command_count - 1 );
80+ cached_commands = NULL ;
6281 return ;
6382 }
6483 }
@@ -145,12 +164,16 @@ void cache_pacman_commands(void) {
145164 char * * new_commands =
146165 realloc (cached_commands , sizeof (char * ) * (command_count + 1 ));
147166 if (!new_commands ) {
167+ free_command_cache (cached_commands , command_count - 1 );
168+ cached_commands = NULL ;
148169 pclose (fp );
149170 return ;
150171 }
151172 cached_commands = new_commands ;
152173 cached_commands [command_count - 1 ] = strdup (path );
153174 if (!cached_commands [command_count - 1 ]) {
175+ free_command_cache (cached_commands , command_count - 1 );
176+ cached_commands = NULL ;
154177 pclose (fp );
155178 return ;
156179 }
@@ -164,11 +187,15 @@ void cache_pacman_commands(void) {
164187 char * * new_commands =
165188 realloc (cached_commands , sizeof (char * ) * (command_count + 1 ));
166189 if (!new_commands ) {
190+ free_command_cache (cached_commands , command_count - 1 );
191+ cached_commands = NULL ;
167192 return ;
168193 }
169194 cached_commands = new_commands ;
170195 cached_commands [command_count - 1 ] = strdup (custom_cmds [i ]);
171196 if (!cached_commands [command_count - 1 ]) {
197+ free_command_cache (cached_commands , command_count - 1 );
198+ cached_commands = NULL ;
172199 return ;
173200 }
174201 }
0 commit comments