[
https://issues.apache.org/jira/browse/PLC4X-318?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17449587#comment-17449587
]
Markus Franke commented on PLC4X-318:
-------------------------------------
With PLC4X 0.9.0 I also experience problems when trying to write a string to my
S7 1200 device. I use the following code
{code:java}
PlcWriteRequest.Builder writeRequestBuilder =
plcConnection.writeRequestBuilder();
writeRequestBuilder.addItem("someString", "%DB100.DBX856:STRING", "Hello
World");
PlcWriteRequest writeRequest = writeRequestBuilder.build();
PlcWriteResponse writeResponse = writeRequest.execute().get();{code}
With this I get a
{code:java}
Exception in thread "main" java.lang.NegativeArraySizeException: -6
at
org.apache.plc4x.java.s7.utils.StaticHelper.serializeS7String(StaticHelper.java:234)
at
org.apache.plc4x.java.s7.readwrite.io.DataItemIO.staticSerialize(DataItemIO.java:418)
at
org.apache.plc4x.java.s7.readwrite.io.DataItemIO.staticSerialize(DataItemIO.java:275)
at
org.apache.plc4x.java.s7.readwrite.protocol.S7ProtocolLogic.serializePlcValue(S7ProtocolLogic.java:862)
at
org.apache.plc4x.java.s7.readwrite.protocol.S7ProtocolLogic.write(S7ProtocolLogic.java:288)
at
org.apache.plc4x.java.spi.optimizer.BaseOptimizer.lambda$optimizedWrite$2(BaseOptimizer.java:114)
at
org.apache.plc4x.java.spi.optimizer.BaseOptimizer.send(BaseOptimizer.java:151)
at
org.apache.plc4x.java.spi.optimizer.BaseOptimizer.optimizedWrite(BaseOptimizer.java:114)
at
org.apache.plc4x.java.spi.connection.AbstractPlcConnection.write(AbstractPlcConnection.java:159)
at
org.apache.plc4x.java.spi.messages.DefaultPlcWriteRequest.execute(DefaultPlcWriteRequest.java:65)
at Siemens.main(Siemens.java:50)
{code}
*Analysis:*
The function {{S7ProtocolLogic.serializePlcValue() }}determines a string length
of 254 because the created field does not seem to be of type S7StringField.
{code:java}
int stringLength = (field instanceof S7StringField) ? ((S7StringField)
field).getStringLength() : 254; {code}
Not sure if this is correct but it looks already weird to me.
Thereafter, the function {{StaticHelper.serializeS7String() contains}}
{code:java}
byte k = (byte) ((stringLength > 250)?250:stringLength);
byte m = (byte) value.getString().length();
m = (m > k)?k:m;
byte[] chars = new byte[m];{code}
"byte" is signed in Java therefore k will be set to -6. As the string length of
the value surely is > -6, m will be set to -6.
Allocating an array of size -6 will lead to the exception above.
Not sure if this is worth a ticket in its own but for the while I decided to
drop it here.
> S7 Cannot write string
> ----------------------
>
> Key: PLC4X-318
> URL: https://issues.apache.org/jira/browse/PLC4X-318
> Project: Apache PLC4X
> Issue Type: Bug
> Components: Driver-S7
> Affects Versions: 0.9.0
> Reporter: LiuYuHang
> Priority: Major
> Attachments: image-2021-10-19-10-59-25-868.png
>
>
> Exception thrown when writing string type: java.nio.bufferoverflow exception
--
This message was sent by Atlassian Jira
(v8.20.1#820001)