You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@maven.apache.org by GitBox <gi...@apache.org> on 2020/12/29 10:18:25 UTC

[GitHub] [maven-shared-utils] slawekjaranowski opened a new pull request #68: [MSHARED-969] Environment variable with null value

slawekjaranowski opened a new pull request #68:
URL: https://github.com/apache/maven-shared-utils/pull/68


   


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [maven-shared-utils] pzygielo commented on a change in pull request #68: [MSHARED-969] Environment variable with null value

Posted by GitBox <gi...@apache.org>.
pzygielo commented on a change in pull request #68:
URL: https://github.com/apache/maven-shared-utils/pull/68#discussion_r549652168



##########
File path: src/test/java/org/apache/maven/shared/utils/cli/CommandLineUtilsTest.java
##########
@@ -168,4 +169,18 @@ private void assertCmdLineArgs( final String[] expected, final String cmdLine )
         assertEquals( expected.length, actual.length );
         assertEquals( Arrays.asList( expected ), Arrays.asList( actual ) );
     }
+
+    @Test
+    public void environmentVariableWithNullShouldBeAddToList() {

Review comment:
       perhaps `Added`




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [maven-shared-utils] MartinKanters merged pull request #68: [MSHARED-969] Environment variable with null value

Posted by GitBox <gi...@apache.org>.
MartinKanters merged pull request #68:
URL: https://github.com/apache/maven-shared-utils/pull/68


   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [maven-shared-utils] slawekjaranowski commented on a change in pull request #68: [MSHARED-969] Environment variable with null value

Posted by GitBox <gi...@apache.org>.
slawekjaranowski commented on a change in pull request #68:
URL: https://github.com/apache/maven-shared-utils/pull/68#discussion_r562690624



##########
File path: src/main/java/org/apache/maven/shared/utils/cli/Commandline.java
##########
@@ -241,15 +241,16 @@ public void addSystemEnvironment()
     public String[] getEnvironmentVariables()
     {
         addSystemEnvironment();
-        String[] environmentVars = new String[envVars.size()];
-        int i = 0;
+        List<String> environmentVars = new ArrayList<>();
         for ( String name : envVars.keySet() )

Review comment:
       Throwing exception in this case will has impact on caller, eg has impact on https://issues.apache.org/jira/browse/MINVOKER-273
   
   In my proposition we have new feature - when environment variable has null value will be removed from executing job environment.
   
   Look at unit tests.
   
   Different approach can be  throw exception for null values and add next method to possibility remove/exclude specific environment variables.
   
   
   




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [maven-shared-utils] slawekjaranowski commented on a change in pull request #68: [MSHARED-969] Environment variable with null value

Posted by GitBox <gi...@apache.org>.
slawekjaranowski commented on a change in pull request #68:
URL: https://github.com/apache/maven-shared-utils/pull/68#discussion_r549721164



##########
File path: src/main/java/org/apache/maven/shared/utils/cli/Commandline.java
##########
@@ -241,15 +241,16 @@ public void addSystemEnvironment()
     public String[] getEnvironmentVariables()
     {
         addSystemEnvironment();
-        String[] environmentVars = new String[envVars.size()];
-        int i = 0;
+        List<String> environmentVars = new ArrayList<>();
         for ( String name : envVars.keySet() )

Review comment:
       reply on jira issue - here we don't set system environment it happen later




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [maven-shared-utils] slawekjaranowski commented on a change in pull request #68: [MSHARED-969] Environment variable with null value

Posted by GitBox <gi...@apache.org>.
slawekjaranowski commented on a change in pull request #68:
URL: https://github.com/apache/maven-shared-utils/pull/68#discussion_r549725461



##########
File path: src/main/java/org/apache/maven/shared/utils/cli/Commandline.java
##########
@@ -241,15 +241,16 @@ public void addSystemEnvironment()
     public String[] getEnvironmentVariables()
     {
         addSystemEnvironment();
-        String[] environmentVars = new String[envVars.size()];
-        int i = 0;
+        List<String> environmentVars = new ArrayList<>();
         for ( String name : envVars.keySet() )
         {
             String value = envVars.get( name );
-            environmentVars[i] = name + "=" + value;
-            i++;
+            if ( value != null )
+            {
+                environmentVars.add( name + "=" + value );
+            }
         }
-        return environmentVars;
+        return environmentVars.toArray( new String[0] );

Review comment:
       From InteliJ inspection:
   
   > In older Java versions using pre-sized array was recommended, as the reflection call which is necessary to create an array of proper size was quite slow. However since late updates of OpenJDK 6 this call was intrinsified, making the performance of the empty array version the same and sometimes even better, compared to the pre-sized version. Also passing pre-sized array is dangerous for a concurrent or synchronized collection as a data race is possible between the size and toArray call which may result in extra nulls at the end of the array, if the collection was concurrently shrunk during the operation.
   




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [maven-shared-utils] elharo commented on a change in pull request #68: [MSHARED-969] Environment variable with null value

Posted by GitBox <gi...@apache.org>.
elharo commented on a change in pull request #68:
URL: https://github.com/apache/maven-shared-utils/pull/68#discussion_r562576989



##########
File path: src/main/java/org/apache/maven/shared/utils/cli/Commandline.java
##########
@@ -241,15 +241,16 @@ public void addSystemEnvironment()
     public String[] getEnvironmentVariables()
     {
         addSystemEnvironment();
-        String[] environmentVars = new String[envVars.size()];
-        int i = 0;
+        List<String> environmentVars = new ArrayList<>();
         for ( String name : envVars.keySet() )

Review comment:
       I agree this is a real bug. Rather than fixing it here, perhaps `addEnvironmentVariable` should throw a NullPointerException for null values? 




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [maven-shared-utils] mkarg commented on a change in pull request #68: [MSHARED-969] Environment variable with null value

Posted by GitBox <gi...@apache.org>.
mkarg commented on a change in pull request #68:
URL: https://github.com/apache/maven-shared-utils/pull/68#discussion_r549711299



##########
File path: src/main/java/org/apache/maven/shared/utils/cli/Commandline.java
##########
@@ -241,15 +241,16 @@ public void addSystemEnvironment()
     public String[] getEnvironmentVariables()
     {
         addSystemEnvironment();
-        String[] environmentVars = new String[envVars.size()];
-        int i = 0;
+        List<String> environmentVars = new ArrayList<>();
         for ( String name : envVars.keySet() )
         {
             String value = envVars.get( name );
-            environmentVars[i] = name + "=" + value;
-            i++;
+            if ( value != null )
+            {
+                environmentVars.add( name + "=" + value );
+            }
         }
-        return environmentVars;
+        return environmentVars.toArray( new String[0] );

Review comment:
       We could speed up this code a very little bit (some nanoseconds though) if we use `new String[environmentVars.size()]` instead. It prevents dropping the empty array and creating a new one, hence reduces GC pressure.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [maven-shared-utils] mkarg commented on a change in pull request #68: [MSHARED-969] Environment variable with null value

Posted by GitBox <gi...@apache.org>.
mkarg commented on a change in pull request #68:
URL: https://github.com/apache/maven-shared-utils/pull/68#discussion_r549710348



##########
File path: src/main/java/org/apache/maven/shared/utils/cli/Commandline.java
##########
@@ -241,15 +241,16 @@ public void addSystemEnvironment()
     public String[] getEnvironmentVariables()
     {
         addSystemEnvironment();
-        String[] environmentVars = new String[envVars.size()];
-        int i = 0;
+        List<String> environmentVars = new ArrayList<>();
         for ( String name : envVars.keySet() )

Review comment:
       While I think the solution is working, I wonder whether it solves the actual cause: At least on Windows, it is impossible to stored environment variables with a `NULL` value. Try this effectly removes it from the environment, according to the Windows API docs. So at least on Windows the correct solution would be that this variable name-value-pair must not be put into `envVars` right from the start. In turn, *here* is no change to be made.
   
   Can someone conform that it is valid on other OS to actually have an environment variable in the environment that stores `NULL` (not the String but `null` in the Java sense)?




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [maven-shared-utils] mkarg commented on a change in pull request #68: [MSHARED-969] Environment variable with null value

Posted by GitBox <gi...@apache.org>.
mkarg commented on a change in pull request #68:
URL: https://github.com/apache/maven-shared-utils/pull/68#discussion_r549711685



##########
File path: src/test/java/org/apache/maven/shared/utils/cli/CommandLineUtilsTest.java
##########
@@ -168,4 +169,18 @@ private void assertCmdLineArgs( final String[] expected, final String cmdLine )
         assertEquals( expected.length, actual.length );
         assertEquals( Arrays.asList( expected ), Arrays.asList( actual ) );
     }
+
+    @Test
+    public void environmentVariableWithNullShouldNotBeAddedToList() {
+
+        Commandline commandline = new Commandline();
+        commandline.addEnvironment("TEST_NULL_ENV", null);

Review comment:
       With reard to my previous comments I'd like to ask the OS experts what sense it make to store `null` in an environment variable?




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [maven-shared-utils] pzygielo commented on a change in pull request #68: [MSHARED-969] Environment variable with null value

Posted by GitBox <gi...@apache.org>.
pzygielo commented on a change in pull request #68:
URL: https://github.com/apache/maven-shared-utils/pull/68#discussion_r549652414



##########
File path: src/test/java/org/apache/maven/shared/utils/cli/CommandLineUtilsTest.java
##########
@@ -168,4 +169,18 @@ private void assertCmdLineArgs( final String[] expected, final String cmdLine )
         assertEquals( expected.length, actual.length );
         assertEquals( Arrays.asList( expected ), Arrays.asList( actual ) );
     }
+
+    @Test
+    public void environmentVariableWithNullShouldBeAddToList() {
+
+        Commandline commandline = new Commandline();
+        commandline.addEnvironment("TEST_NULL_ENV", null);
+
+        String[] environmentVariables = commandline.getEnvironmentVariables();
+        assertNotNull(environmentVariables);
+        for (String variable: environmentVariables) {
+            assertFalse("Variable '" + variable + "' shouldn't be set",

Review comment:
       conflicts with test name




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [maven-shared-utils] slawekjaranowski commented on a change in pull request #68: [MSHARED-969] Environment variable with null value

Posted by GitBox <gi...@apache.org>.
slawekjaranowski commented on a change in pull request #68:
URL: https://github.com/apache/maven-shared-utils/pull/68#discussion_r549748775



##########
File path: src/test/java/org/apache/maven/shared/utils/cli/CommandLineUtilsTest.java
##########
@@ -168,4 +169,18 @@ private void assertCmdLineArgs( final String[] expected, final String cmdLine )
         assertEquals( expected.length, actual.length );
         assertEquals( Arrays.asList( expected ), Arrays.asList( actual ) );
     }
+
+    @Test
+    public void environmentVariableWithNullShouldNotBeAddedToList() {
+
+        Commandline commandline = new Commandline();
+        commandline.addEnvironment("TEST_NULL_ENV", null);

Review comment:
       As I wrote before Command Line object has local map of environment variable which are provided to Runtime.exec after translate this map to list of `name=value`. Translation is done in getEnvironmentVariables.
   
   Please look at my last comment at jira and new test in last update.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [maven-shared-utils] pzygielo commented on a change in pull request #68: [MSHARED-969] Environment variable with null value

Posted by GitBox <gi...@apache.org>.
pzygielo commented on a change in pull request #68:
URL: https://github.com/apache/maven-shared-utils/pull/68#discussion_r549718880



##########
File path: src/main/java/org/apache/maven/shared/utils/cli/Commandline.java
##########
@@ -241,15 +241,16 @@ public void addSystemEnvironment()
     public String[] getEnvironmentVariables()
     {
         addSystemEnvironment();
-        String[] environmentVars = new String[envVars.size()];
-        int i = 0;
+        List<String> environmentVars = new ArrayList<>();
         for ( String name : envVars.keySet() )
         {
             String value = envVars.get( name );
-            environmentVars[i] = name + "=" + value;
-            i++;
+            if ( value != null )
+            {
+                environmentVars.add( name + "=" + value );
+            }
         }
-        return environmentVars;
+        return environmentVars.toArray( new String[0] );

Review comment:
       > We could speed up this code a very little bit (some nanoseconds though) if we use `new String[environmentVars.size()]` instead. It prevents dropping the empty array and creating a new one, hence reduces GC pressure.
   
   It seems, even across Apache repositories (https://github.com/apache/kafka/pull/9750, https://github.com/apache/tika/pull/372), that using empty array exactly is popular now.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [maven-shared-utils] slawekjaranowski commented on pull request #68: [MSHARED-969] Environment variable with null value

Posted by GitBox <gi...@apache.org>.
slawekjaranowski commented on pull request #68:
URL: https://github.com/apache/maven-shared-utils/pull/68#issuecomment-752031706


   @pzygielo thanks for review, I did fix


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org