Skip to content

Conversation

@EJTJ3
Copy link

@EJTJ3 EJTJ3 commented Nov 6, 2024

This PR resolves a runtime panic that occurs when the Modbus transport fails to initialize. Previously, if the transport was not successfully created, attempting to read the registers would lead to a runtime panic. This fix ensures that the code checks for a valid transport before proceeding with register reads, preventing the panic and providing a error message instead.

How to reproduce

package main

import (
	"time"

	"github.com/simonvetter/modbus"
)

func main() {
	modbusClient, err := modbus.NewClient(&modbus.ClientConfiguration{
		URL:     "tcp://192.168.2.2:502", // Some fake URL 
		Timeout: time.Second,
	})

	if err = modbusClient.Open(); err != nil {
		println(err.Error())
	}

	if _, err = modbusClient.ReadRegister(100, modbus.HOLDING_REGISTER); err != nil {
		println(err.Error())
	}
}

Output without the fix:

dial tcp 192.168.2.2:502: i/o timeout
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x2 addr=0x20 pc=0x1022d2064]

Output with the fix:

dial tcp 192.168.2.2:502: i/o timeout
Transport is not initialized

@EJTJ3 EJTJ3 changed the title fix: panic when transport is not iInitialized fix: panic when transport is not initialized Nov 6, 2024
@paologaleotti
Copy link

Really hope this will get merged!

@EJTJ3
Copy link
Author

EJTJ3 commented Jan 27, 2025

@simonvetter If possible, could you take a look at this?

@paologaleotti
Copy link

paologaleotti commented Jan 27, 2025

We are about to use this package in production and we absolutely need this fixed, we cannot have panics at runtime :(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants