From 78faea80583e6fc903bdb8c146e8ff499b60c307 Mon Sep 17 00:00:00 2001 From: AlexGeller1 <87009702+AlexGeller1@users.noreply.github.com> Date: Fri, 6 May 2022 01:01:14 +0200 Subject: [PATCH] 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 --- .../google/zxing/pdf417/encoder/PDF417HighLevelEncoder.java | 4 ++++ .../google/zxing/pdf417/encoder/PDF417EncoderTestCase.java | 6 ++++++ 2 files changed, 10 insertions(+) diff --git a/core/src/main/java/com/google/zxing/pdf417/encoder/PDF417HighLevelEncoder.java b/core/src/main/java/com/google/zxing/pdf417/encoder/PDF417HighLevelEncoder.java index 79226e060..282cc7377 100644 --- a/core/src/main/java/com/google/zxing/pdf417/encoder/PDF417HighLevelEncoder.java +++ b/core/src/main/java/com/google/zxing/pdf417/encoder/PDF417HighLevelEncoder.java @@ -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) { diff --git a/core/src/test/java/com/google/zxing/pdf417/encoder/PDF417EncoderTestCase.java b/core/src/test/java/com/google/zxing/pdf417/encoder/PDF417EncoderTestCase.java index 8166333ed..f0c9aa5ab 100644 --- a/core/src/test/java/com/google/zxing/pdf417/encoder/PDF417EncoderTestCase.java +++ b/core/src/test/java/com/google/zxing/pdf417/encoder/PDF417EncoderTestCase.java @@ -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); + } }