@@ -115,16 +115,43 @@ char *get_pyenvcfg_command(const char *pyenv_cfg_path) {
115
115
exit (1 );
116
116
}
117
117
118
- void split_venv_command_into_args (char * venv_command , char * result []) {
118
+ char * * split_venv_command_into_args (char * venv_command , int * argc_out ) {
119
+
120
+ char * copy = strdup (venv_command );
121
+ size_t capacity = 5 ;
122
+ char * * args = malloc (capacity * sizeof (char * ));
123
+ if (!args ) {
124
+ fprintf (stderr , "allocation failed failed\n" );
125
+ free (copy );
126
+ exit (1 );
127
+ }
128
+
129
+ size_t count = 0 ;
119
130
char * current_token = strtok (venv_command , " " );
120
- for (int i = 0 ; i < 5 ; i ++ ) {
121
- result [i ] = current_token ;
131
+ while (current_token ) {
132
+ if (count >= capacity ) {
133
+ capacity *= 2 ;
134
+ char * * tmp = realloc (args , capacity * sizeof (char * ));
135
+ if (!tmp ) {
136
+ // allocation failed
137
+ fprintf (stderr , "reallocation failed\n" );
138
+ for (size_t i = 0 ; i < count ; i ++ ) {
139
+ free (args [i ]);
140
+ }
141
+ free (args );
142
+ free (copy );
143
+ exit (1 );
144
+ }
145
+ args = tmp ;
146
+ }
147
+
148
+ args [count ++ ] = strdup (current_token );
122
149
current_token = strtok (NULL , " " );
123
150
}
124
- if ( current_token != NULL ) {
125
- fprintf ( stderr , "Unexpected venv_command size!\n" );
126
- exit ( 1 ) ;
127
- }
151
+
152
+ free ( copy );
153
+ * argc_out = ( int ) count ;
154
+ return args ;
128
155
}
129
156
130
157
void find_pyvenv (char * pyvenv_cfg_path , size_t path_size ) {
@@ -157,10 +184,9 @@ int main(int argc, char *argv[]) {
157
184
find_pyvenv (pyvenv_cfg_path , sizeof (pyvenv_cfg_path ));
158
185
159
186
char * venv_command = get_pyenvcfg_command (pyvenv_cfg_path );
160
- char * venv_command_dup = strdup (venv_command );
161
187
162
- char * venv_args [ 5 ] ;
163
- split_venv_command_into_args (venv_command_dup , venv_args );
188
+ int venv_argc = 0 ;
189
+ char * * venv_args = split_venv_command_into_args (venv_command , & venv_argc );
164
190
165
191
// Adds "--python.VenvlauncherCommand="
166
192
size_t python_base_exec_size = strlen (venv_command ) + strlen (GRAAL_PYTHON_BASE_EXE_ARG ) + 2 + 1 ;
@@ -176,13 +202,13 @@ int main(int argc, char *argv[]) {
176
202
GRAAL_PYTHON_EXE_ARG ,
177
203
argv [0 ]);
178
204
179
- // venv_args (5) + "--python.VenvlauncherCommand=" + Adds "--python.Executable=" + rest of argc + NULL
180
- size_t args_size = 5 + 2 + (argc - 1 ) + 1 ;
205
+ // venv_args + "--python.VenvlauncherCommand=" + Adds "--python.Executable=" + rest of argc (-1 because we are not interested in argv[0]) + NULL
206
+ size_t args_size = venv_argc + 2 + (argc - 1 ) + 1 ;
181
207
char * args [args_size ];
182
208
183
209
// copy venv_args
184
210
int k = 0 ;
185
- for (k = 0 ; k < 5 ; k ++ ) {
211
+ for (k = 0 ; k < venv_argc ; k ++ ) {
186
212
args [k ] = venv_args [k ];
187
213
}
188
214
@@ -200,4 +226,3 @@ int main(int argc, char *argv[]) {
200
226
perror ("execv failed" ); // only runs if execv fails
201
227
return 1 ;
202
228
}
203
-
0 commit comments