You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "Gary Gregory (JIRA)" <ji...@apache.org> on 2014/11/06 14:39:34 UTC

[jira] [Closed] (EXEC-80) NPE in EnvironmentUtils.toString(map)

     [ https://issues.apache.org/jira/browse/EXEC-80?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Gary Gregory closed EXEC-80.
----------------------------

Closing: We released version 1.3 today.

> NPE in EnvironmentUtils.toString(map)
> -------------------------------------
>
>                 Key: EXEC-80
>                 URL: https://issues.apache.org/jira/browse/EXEC-80
>             Project: Commons Exec
>          Issue Type: Bug
>    Affects Versions: 1.2
>         Environment: all
>            Reporter: Karl Heinz Marbaise
>            Assignee: Gary Gregory
>             Fix For: 1.3
>
>         Attachments: EXEC-80.patch
>
>
> I have drilled down a problem in the above class method which can be checked by using the following unit test:
> {code}
> 	@Test
> 	public void toStringTest() {
> 		Map<String,String> map = new HashMap<String, String>();
> 		map.put("key", null);
> 		String[] result = EnvironmentUtils.toStrings(map);
> 		assertThat(result.length).isEqualTo(1);
> 	}
> {code}
> The above test will fail with an NullPointerException..as expected, cause the method accesses the key as well as the value by using the following code line:
> {code}
> result[i] = entry.getKey().toString() + "=" + entry.getValue().toString();
> {code}
> It should be checked if key and/or value are null. If you would use the following code:
> {code}
>     public static String[] toStrings(final Map environment) {
>         if (environment == null) {
>             return null;
>         }
>         final String[] result = new String[environment.size()];
>         int i = 0;
>         for (final Iterator iter = environment.entrySet().iterator(); iter.hasNext();) {
>             final Map.Entry entry = (Map.Entry) iter.next();
>             String key  = entry.getKey() == null ? "" : entry.getKey().toString();
>             String value = entry.getValue() == null ? "" : entry.getValue().toString();
>             result[i] = key + "=" + value;
>             i++;
>         }
>         return result;
>     }
> {code}
> it will solve the issue.



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)