Prevent encoding of the empty string for PDF417 (#1523)

* Added code so that the PDF417 encoder throws an exception when attempting to encode the empty string
This commit is contained in:
AlexGeller1 2022-05-06 01:01:14 +02:00 committed by GitHub
parent c7a7b30f04
commit 78faea8058
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 0 deletions

View file

@ -171,6 +171,10 @@ final class PDF417HighLevelEncoder {
static String encodeHighLevel(String msg, Compaction compaction, Charset encoding, boolean autoECI)
throws WriterException {
if (msg.isEmpty()) {
throw new WriterException("Empty message not allowed");
}
if (encoding == null && !autoECI) {
for (int i = 0; i < msg.length(); i++) {
if (msg.charAt(i) > 255) {

View file

@ -16,6 +16,8 @@
package com.google.zxing.pdf417.encoder;
import com.google.zxing.WriterException;
import java.nio.charset.StandardCharsets;
import org.junit.Assert;
@ -67,4 +69,8 @@ public final class PDF417EncoderTestCase extends Assert {
assertEquals("\u039f\u001A\u0385abcd", encoded);
}
@Test(expected = WriterException.class)
public void testEncodeEmptyString() throws Exception {
PDF417HighLevelEncoder.encodeHighLevel("", Compaction.AUTO, null, false);
}
}