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

Tx is nil bug!! #116

Closed
zhushh opened this issue Feb 27, 2019 · 1 comment
Closed

Tx is nil bug!! #116

zhushh opened this issue Feb 27, 2019 · 1 comment
Assignees
Labels

Comments

@zhushh
Copy link

zhushh commented Feb 27, 2019

@js-ojus

Hi, I have found that the following code is a bug:

	var tx *sql.Tx
	if otx == nil {
		tx, err := db.Begin()
		if err != nil {
			return 0, err
		}
		defer tx.Rollback()
	} else {
		tx = otx
	}

here tx, err := db.Begin() will make a temporary variable tx, so out of the if statement, tx also will be a nil value. Thus, when you call the interface which need parameter *sql.Tx and you give a nil to it, it will cause a panic. For example:

        gid, err := flow.Groups.New(nil, "nike", "G")

I give nil to Groups.New because I have called RegisterDB to make db is not null.

We can use this code to fix it:

    var tx *sql.Tx
    if otx == nil {
        var err error
        tx, err = db.Begin()
        if err != nil {
            return 0, err
        }
        defer tx.Rollback()
    } else {
        tx = otx
    }
@js-ojus js-ojus self-assigned this Feb 27, 2019
@js-ojus js-ojus added the bug label Feb 27, 2019
@js-ojus
Copy link
Owner

js-ojus commented Feb 27, 2019

@zhushh Ouch, ouch! Good catch! I fell into a pretty basic Go trap. :-(

I never discovered this because, in all my code, I created a top-level transaction that I passed in.

I shall correct this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants