prashantmavani0706 opened a new pull request, #2605:
URL: https://github.com/apache/plc4x/pull/2605

   <html>
   <body>
   <!--StartFragment--><html><head></head><body><h2>fix(s7): Prevent NPE and 
AIOOBE when reading an undefined DB variable (fixes #2266)</h2>
   <h3>Problem</h3>
   <p>When reading a Siemens Data Block (DB) variable that does not exist in 
the PLC, the
   S7 protocol returns a per-item <code>returnCode</code> of 
<code>NOT_FOUND</code> or <code>INVALID_ADDRESS</code> inside
   <code>S7PayloadReadVarResponse</code>. In this case 
<code>S7VarPayloadDataItem.getData()</code> is either
   <code>null</code> or a zero-length <code>byte[]</code>.</p>
   <p>The previous code in <code>S7CotpConnection.applyChunkResponse</code> 
passed this value directly
   into <code>decodeBindingInto</code> without any check:</p>
   <pre><code class="language-java">// BEFORE
   byte[] data = item.getData();          // null on some PLC firmware variants
   // ...
   decodeBindingInto(out, b, data);       // NullPointerException here
   </code></pre>
   <p>In the block-merged path inside <code>decodeBindingInto</code>:</p>
   <pre><code class="language-java">// BEFORE
   System.arraycopy(data, b.payloadByteOffset(), ...);  // AIOOBE if offset 
&gt;= data.length
   </code></pre>
   <p>Both exceptions were caught by the outer <code>try/catch</code> and 
silently converted to
   <code>INTERNAL_ERROR</code>, hiding the real cause and giving the 
application no useful error
   information.</p>
   <h3>Root cause — three distinct firmware scenarios</h3>
   
   Scenario | returnCode | getData() | Old result | Fixed result
   -- | -- | -- | -- | --
   Undefined variable, Variant A | NOT_FOUND | null | NullPointerException → 
INTERNAL_ERROR | INVALID_ADDRESS
   Undefined variable, Variant B | INVALID_ADDRESS | byte[0] | Silent wrong 
answer / AIOOBE | INVALID_ADDRESS
   PLC firmware inconsistency | OK | byte[0] | AIOOBE → INTERNAL_ERROR | 
INVALID_ADDRESS
   Block-merged read, truncated response | OK | byte[N], N too short | AIOOBE 
in System.arraycopy | INTERNAL_ERROR
   
   
   <h3>Files changed</h3>
   
<pre><code>plc4j/drivers/s7/src/main/java/org/apache/plc4x/java/s7/S7CotpConnection.java
   
plc4j/drivers/s7/src/test/java/org/apache/plc4x/java/s7/S7CotpConnectionResponseDecodeTest.java
  [new]
   </code></pre>
   <p>Fixes #2266</p></body></html><!--EndFragment-->
   </body>
   </html>
   
   


-- 
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.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to