mirror of
				https://github.com/RGBCube/serenity
				synced 2025-10-31 11:12:45 +00:00 
			
		
		
		
	 162a2b66eb
			
		
	
	
		162a2b66eb
		
	
	
	
	
		
			
			Depending on stack values being correctly and deterministically overwritten was a bit too optimistic, to be honest. This new logic uses a value on the heap.
		
			
				
	
	
		
			43 lines
		
	
	
	
		
			1.5 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
			
		
		
	
	
			43 lines
		
	
	
	
		
			1.5 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
| /*
 | |
|  * Copyright (c) 2023, Jelle Raaijmakers <jelle@gmta.nl>
 | |
|  *
 | |
|  * SPDX-License-Identifier: BSD-2-Clause
 | |
|  */
 | |
| 
 | |
| #include <LibTest/TestCase.h>
 | |
| #include <stdlib.h>
 | |
| #include <string.h>
 | |
| 
 | |
| TEST_CASE(putenv_overwrite_invalid_value)
 | |
| {
 | |
|     // Write an environment variable using the heap
 | |
|     auto* heap_environment_value = new char[12];
 | |
|     VERIFY(snprintf(heap_environment_value, 12, "TESTVAR=123") == 11);
 | |
|     auto result = putenv(heap_environment_value);
 | |
|     EXPECT_EQ(result, 0);
 | |
| 
 | |
|     // Try to retrieve the variable after overwriting the heap value
 | |
|     memset(heap_environment_value, 0, 12);
 | |
|     auto* environment_variable = getenv("TESTVAR");
 | |
|     EXPECT_EQ(environment_variable, nullptr);
 | |
| 
 | |
|     // Try to overwrite the variable now that it's zeroed out
 | |
|     auto* new_environment_value = new char[12];
 | |
|     VERIFY(snprintf(new_environment_value, 12, "TESTVAR=456") == 11);
 | |
|     result = putenv(new_environment_value);
 | |
|     EXPECT_EQ(result, 0);
 | |
| 
 | |
|     // Retrieve the variable and verify that it's set correctly
 | |
|     environment_variable = getenv("TESTVAR");
 | |
|     EXPECT_NE(environment_variable, nullptr);
 | |
|     EXPECT_EQ(strcmp(environment_variable, "456"), 0);
 | |
| 
 | |
|     // Overwrite and retrieve it again to test correct search behavior for '='
 | |
|     auto* final_environment_value = new char[12];
 | |
|     VERIFY(snprintf(final_environment_value, 12, "TESTVAR=789") == 11);
 | |
|     result = putenv(final_environment_value);
 | |
|     EXPECT_EQ(result, 0);
 | |
|     environment_variable = getenv("TESTVAR");
 | |
|     EXPECT_NE(environment_variable, nullptr);
 | |
|     EXPECT_EQ(strcmp(environment_variable, "789"), 0);
 | |
| }
 |