Skip to content
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

fix: panic when transport is not initialized #49

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

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
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.

1 participant