Skip to content

Fix Issues in DefaultKafkaProducerFactory#updateConfigs() #2897

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

NathanQingyangXu
Copy link
Contributor

@NathanQingyangXu NathanQingyangXu commented Nov 15, 2023

There are many defects in the original DefaultKafkaProducerFactory#updateConfigs() as below:

#1 public void updateConfigs(Map<String, Object> updates) {
#2		updates.entrySet().forEach(entry -> {
#3			if (entry.getKey().equals(ProducerConfig.TRANSACTIONAL_ID_CONFIG)) {
#4				Assert.isTrue(entry.getValue() instanceof String, () -> "'" + ProducerConfig.TRANSACTIONAL_ID_CONFIG
#5						+ "' must be a String, not a " + entry.getClass().getName());
#6				Assert.isTrue(this.transactionIdPrefix != null
#7								? entry.getValue() != null
#8								: entry.getValue() == null,
#9						"Cannot change transactional capability");
#10				this.transactionIdPrefix = (String) entry.getValue();
#11			}
#12			else if (entry.getKey().equals(ProducerConfig.CLIENT_ID_CONFIG)) {
#13				Assert.isTrue(entry.getValue() instanceof String, () -> "'" + ProducerConfig.CLIENT_ID_CONFIG
#14						+ "' must be a String, not a " + entry.getClass().getName());
#15				this.clientIdPrefix = (String) entry.getValue();
#16			}
#17			else {
#18				this.configs.put(entry.getKey(), entry.getValue());
#19			}
#20		});
#21	}

It has following defects:

  1. given the input is a Map, we can't rule out the possibility that both key and value could be null (e.g. in default HashMap); when key is null, Line # 2 will thrown NPE; when value is null; Line # 18 will throw exception for ConcurrentHashMap disallows null key or value (this.configs is based on this impl class);
  2. from either Line # 10, it implies null value for ProducerConfig.TRANSACTIONAL_ID_CONFIG is allowed; but when entry.getValue() returns null, the entry.getValue() instanceof String in Line # 4 should have return false without reaching later statements (e.g. Line # 6);
  3. obviously in both Line # 5 and Line # 14, the intention was "' must be a String, not a " + entry.getValue().getClass().getName()); entry.getClass() would always return Map.Entry.

@NathanQingyangXu NathanQingyangXu force-pushed the avoid-NPE-in-DefaultKafkaProducerFactory-updateConfigs branch 2 times, most recently from 1cb2497 to 8da52f5 Compare November 15, 2023 02:45
Copy link
Contributor

@garyrussell garyrussell left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks; but it is not clear why you found it necessary for all those format changes.

@NathanQingyangXu
Copy link
Contributor Author

NathanQingyangXu commented Nov 15, 2023 via email

@artembilan
Copy link
Member

How about this ./gradlew check -x test?
Probably that checkstyleMain doesn't see our src/checkstyle/checkstyle.xml config.

@NathanQingyangXu NathanQingyangXu force-pushed the avoid-NPE-in-DefaultKafkaProducerFactory-updateConfigs branch 2 times, most recently from c4bf9df to 03e8d20 Compare November 16, 2023 00:19
@NathanQingyangXu
Copy link
Contributor Author

How about this ./gradlew check -x test? Probably that checkstyleMain doesn't see our src/checkstyle/checkstyle.xml config.

thanks. It works. Lesson learned.

@NathanQingyangXu NathanQingyangXu force-pushed the avoid-NPE-in-DefaultKafkaProducerFactory-updateConfigs branch from 03e8d20 to 1afa161 Compare November 16, 2023 01:57
@NathanQingyangXu NathanQingyangXu changed the title fix many issues in DefaultKafkaProducerFactory#updateConfigs() fix some issues in DefaultKafkaProducerFactory#updateConfigs() Nov 16, 2023
this.transactionIdPrefix = (String) entry.getValue();
updates.forEach((key, value) -> {
if (key == null) {
return;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't this mean that we are going to exit from a loop on a first key as null?
Therefore we just don't examine all the entries in the map.

@NathanQingyangXu
Copy link
Contributor Author

NathanQingyangXu commented Nov 16, 2023 via email

@garyrussell garyrussell merged commit f527f3c into spring-projects:main Nov 16, 2023
@garyrussell garyrussell added this to the 3.1.0 milestone Nov 16, 2023
@garyrussell garyrussell changed the title fix some issues in DefaultKafkaProducerFactory#updateConfigs() Fix Issues in DefaultKafkaProducerFactory#updateConfigs() Nov 16, 2023
@NathanQingyangXu NathanQingyangXu deleted the avoid-NPE-in-DefaultKafkaProducerFactory-updateConfigs branch November 16, 2023 23:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants