@@ -105,10 +105,10 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
105
105
#[ allow( non_snake_case) ]
106
106
fn GetEnvironmentVariableW (
107
107
& mut self ,
108
- name_op : OpTy < ' tcx , Tag > , // LPCWSTR
109
- buf_op : OpTy < ' tcx , Tag > , // LPWSTR
110
- size_op : OpTy < ' tcx , Tag > , // DWORD
111
- ) -> InterpResult < ' tcx , u32 > {
108
+ name_op : OpTy < ' tcx , Tag > , // LPCWSTR
109
+ buf_op : OpTy < ' tcx , Tag > , // LPWSTR
110
+ size_op : OpTy < ' tcx , Tag > , // DWORD
111
+ ) -> InterpResult < ' tcx , u32 > { // Returns DWORD (u32 in Windows)
112
112
let this = self . eval_context_mut ( ) ;
113
113
this. assert_target_os ( "windows" , "GetEnvironmentVariableW" ) ;
114
114
@@ -125,17 +125,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
125
125
let buf_ptr = this. read_scalar ( buf_op) ?. not_undef ( ) ?;
126
126
// `buf_size` represents the size in characters.
127
127
let buf_size = u64:: from ( this. read_scalar ( size_op) ?. to_u32 ( ) ?) ;
128
- let ( success, len) = this. write_os_str_to_wide_str ( & var, buf_ptr, buf_size) ?;
129
-
130
- if success {
131
- // If the function succeeds, the return value is the number of characters stored in the buffer pointed to by lpBuffer,
132
- // not including the terminating null character.
133
- u32:: try_from ( len) . unwrap ( )
134
- } else {
135
- // If lpBuffer is not large enough to hold the data, the return value is the buffer size, in characters,
136
- // required to hold the string and its terminating null character and the contents of lpBuffer are undefined.
137
- u32:: try_from ( len) . unwrap ( ) . checked_add ( 1 ) . unwrap ( )
138
- }
128
+ HowWasBufferSize ( this. write_os_str_to_wide_str ( & var, buf_ptr, buf_size) ?)
139
129
}
140
130
None => {
141
131
let envvar_not_found = this. eval_windows ( "ERROR_ENVVAR_NOT_FOUND" ) ?;
@@ -326,10 +316,8 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
326
316
327
317
// If we cannot get the current directory, we return 0
328
318
match env:: current_dir ( ) {
329
- Ok ( cwd) => {
330
- let len = this. write_path_to_wide_str ( & cwd, buf, size) ?. 1 ;
331
- return Ok ( u32:: try_from ( len) . unwrap ( ) )
332
- }
319
+ Ok ( cwd) =>
320
+ return Ok ( HowWasBufferSize ( this. write_path_to_wide_str ( & cwd, buf, size) ?) ) ,
333
321
Err ( e) => this. set_last_error_from_io_error ( e) ?,
334
322
}
335
323
Ok ( 0 )
@@ -411,3 +399,17 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
411
399
Ok ( ( ) )
412
400
}
413
401
}
402
+
403
+ // Local helper function to be used in Windows shims
404
+ #[ allow( non_snake_case) ]
405
+ fn HowWasBufferSize ( ( success, len) : ( bool , u64 ) ) -> u32 {
406
+ if success {
407
+ // If the function succeeds, the return value is the number of characters stored in the buffer pointed to by lpBuffer,
408
+ // not including the terminating null character.
409
+ u32:: try_from ( len) . unwrap ( )
410
+ } else {
411
+ // If lpBuffer is not large enough to hold the data, the return value is the buffer size, in characters,
412
+ // required to hold the string and its terminating null character and the contents of lpBuffer are undefined.
413
+ u32:: try_from ( len. checked_add ( 1 ) . unwrap ( ) ) . unwrap ( )
414
+ }
415
+ }
0 commit comments