Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 369468

Summary: RDTCommandLauncher#execute doesn't properly process environment variables
Product: [Tools] Linux Tools Reporter: Corey Ashford <cjashfor>
Component: ProjectAssignee: Linux Distros Inbox <linux.distros-inbox>
Status: RESOLVED FIXED QA Contact:
Severity: normal    
Priority: P3 CC: jjohnstn, obusatto
Version: unspecified   
Target Milestone: ---   
Hardware: PC   
OS: Linux   
Whiteboard:
Attachments:
Description Flags
Fix bugs in env var processing by using indexOf instead of split.
none
Fix two bugs in environment variable processing
none
RDTCommandLauncher#execute: Fix problems with environment variables jjohnstn: iplog+

Description Corey Ashford CLA 2012-01-23 21:16:39 EST
Created attachment 209951 [details]
Fix bugs in env var processing by using indexOf instead of split.

The execute method in RDTCommandLauncher incorrectly deals with environment variables in two cases:

1) If the value of the variable is null, the code will crash with an index-out-of-range error due to indexing the tokens[] array (which is of length 1) with the index [1].

2) If the value of the env var contains one or more embedded '=' characters, only the portion of the string leading up to the first '=' character is taken as the value.
Comment 1 Jeff Johnston CLA 2012-01-30 18:52:58 EST
(In reply to comment #0)
> Created attachment 209951 [details]
> Fix bugs in env var processing by using indexOf instead of split.
> 
> The execute method in RDTCommandLauncher incorrectly deals with environment
> variables in two cases:
> 
> 1) If the value of the variable is null, the code will crash with an
> index-out-of-range error due to indexing the tokens[] array (which is of length
> 1) with the index [1].
> 
> 2) If the value of the env var contains one or more embedded '=' characters,
> only the portion of the string leading up to the first '=' character is taken
> as the value.

The usage of the 2 in the split method should have handled case 2.  If someone has:

  a=b=2 then token[0], "the name" is "a" and the value (token[1]) is then "b=2".  The parameter 2 restricts the number of tokens created to an upper-maximum and the last token takes all characters to end of string, ignoring the tokenizer.

The code could simply add a check on the length of the result array and then set the value to null in the empty case.  I would assume that the internal code to do a String split is more efficient than us doing it ourselves manually.

So, it could be:

 			for (int i = 0; i < env.length; ++i) {
 				String s = env[i];
 				String[] tokens = s.split("=", 2);
                                if (tokens.length == 2)
 				       envMap.put(tokens[0], tokens[1]);
                                else if (tokens.length == 1)
                                       envMap.put(tokens[0], null);
                                else
                                       <raise exception or log error>
                        }
Comment 2 Corey Ashford CLA 2012-01-30 19:43:34 EST
Good point about the "split("=", 2);"  I had missed that detail and thought I had spotted a problem.

I will re-write this patch, using your technique.  I think the "right thing" to do in this case is to log a warning.  I think it won't come up often, but if it does we should let the user know, without breaking too many things.
Comment 3 Corey Ashford CLA 2012-01-30 21:35:33 EST
Created attachment 210295 [details]
Fix two bugs in environment variable processing

I wrote this patch, and I have permission from my employer (IBM Corp.) to submit it.
Comment 4 Otavio Pontes CLA 2012-05-16 09:33:39 EDT
What about the new corey's patch. Does it look fine?
Comment 5 Jeff Johnston CLA 2012-05-16 11:52:51 EDT
(In reply to comment #4)
> What about the new corey's patch. Does it look fine?

The new code needs a license/copyright notice that is EPL and Corey needs to add that his changes are EPL.  Once that is done, I can check it in.
Comment 6 Otavio Pontes CLA 2012-05-16 12:09:55 EDT
Created attachment 215719 [details]
RDTCommandLauncher#execute: Fix problems with environment variables
Comment 7 Otavio Pontes CLA 2012-05-16 12:11:18 EDT
License added. Waiting for Corey's statement.
Comment 8 Corey Ashford CLA 2012-05-16 12:43:29 EDT
(In reply to comment #7)
> License added. Waiting for Corey's statement.

The patch I made is intended to be EPL.
Comment 9 Jeff Johnston CLA 2012-05-16 13:43:18 EDT
Comment on attachment 215719 [details]
RDTCommandLauncher#execute: Fix problems with environment variables

<250 lines so iplog+
Comment 10 Jeff Johnston CLA 2012-05-16 13:43:49 EDT
Patch checked in.

commit hash: 32911ffcb217e7b3ad4b91b35cca8c361bfa20a9