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 in bid adjustments #3547

Merged
merged 4 commits into from
Mar 18, 2024

Conversation

linux019
Copy link
Contributor

@linux019 linux019 commented Feb 27, 2024

If auction request doesn't have bidadjustments object, but the adjustment has been enabled in the stored requests for the account there will be a panic
#3543

{
...
    "ext": {
        "prebid": {
             "bidadjustments": {},
         }
     }
}

@bsardo bsardo changed the title Fixes panic in bids adjustment #3543 Fix: Panic in bid adjustments #3543 Feb 27, 2024
@bsardo bsardo self-assigned this Feb 27, 2024
@bsardo bsardo changed the title Fix: Panic in bid adjustments #3543 Fix: Panic in bid adjustments Feb 28, 2024
Copy link
Collaborator

@bsardo bsardo left a comment

Choose a reason for hiding this comment

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

Thanks for adding the test to confirm the fix. I have just one minor suggestion. Let me know your thoughts.

@@ -63,7 +63,7 @@ func merge(req *openrtb_ext.RequestWrapper, acct *openrtb_ext.ExtRequestPrebidBi
if extPrebid == nil && acct == nil {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Thoughts on changing this logic? I'm thinking it might be easier to understand if it were like this:

if (extPrebid == nil || extPrebid.BidAdjustments == nil) {
	if acct == nil {
		return nil, nil	
	}
	return acct, nil
}
if acct == nil {
	return extPrebid.BidAdjustments, nil
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

	if extPrebid == nil || extPrebid.BidAdjustments == nil {
		return acct, nil
	}

	if acct == nil {
		return extPrebid.BidAdjustments, nil
	}

@SyntaxNode SyntaxNode assigned guscarreon and hhhjort and unassigned bsardo Mar 4, 2024
hhhjort
hhhjort previously approved these changes Mar 5, 2024
Copy link
Collaborator

@hhhjort hhhjort left a comment

Choose a reason for hiding this comment

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

LGTM

@@ -63,7 +63,7 @@ func merge(req *openrtb_ext.RequestWrapper, acct *openrtb_ext.ExtRequestPrebidBi
if extPrebid == nil && acct == nil {
return nil, nil
}
if extPrebid == nil && acct != nil {
if (extPrebid == nil || extPrebid.BidAdjustments == nil) && acct != nil {
Copy link
Contributor

Choose a reason for hiding this comment

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

The 3rd test case in the truth table below seems to still panic under the newly added logic. Do you agree?

   | extPrebid | extPrebid.BidAdjustments | acct    || returns
---|-----------|--------------------------|---------||-----------
  1|   nil     |       nil                | nil     || nil, nil
  2|   nil     |       nil                | not-nil || acct, nil          
  3|  not-nil  |       nil                | nil     || PANIC in line 73 when dereferencing nil extPrebid.BidAdjustments
  4|  not-nil  |       nil                | not-nil || acct, nil
  5|  not-nil  |     not-nil              | nil     || extPrebid.BidAdjustments, nil         
  6|  not-nil  |     not-nil              | not-nil || acct, nil          

I believe the modification below could cover the panic scenario but I think, as written below, we have repetitive checks:

62
63     if extPrebid == nil && acct == nil {
64         return nil, nil
65     }
66     if (extPrebid == nil || extPrebid.BidAdjustments == nil) && acct != nil {
67         return acct, nil
68     }
69 -   if extPrebid != nil && acct == nil {
   +   if extPrebid != nil && extPrebid.BidAdjustments != nil && acct == nil {
70         return extPrebid.BidAdjustments, nil
71     }
   +   if extPrebid != nil && extPrebid.BidAdjustments == nil {
   +       extPrebid.BidAdjustments := &ExtRequestPrebidBidAdjustments{}
   +   }
72
73     extPrebid.BidAdjustments.MediaType.Banner = mergeForMediaType(extPrebid.BidAdjustments.MediaType.Banner, acct.MediaType.Banner)
// PANIC deferencing null    ^
// pointer in scenario 3     |
// if we don't modify    ----+
bidadjustment/build_rules.go

Honestly, the modifications proposed by you and Brian Sardo above, seem a lot better than I just proposed here. Can we please put logic between lines 63 to 71 in its own function, refactor as necessary, and unit test all the scenarios?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@guscarreon I added 6 test cases for your table, there is no panic

"dealId": []openrtb_ext.Adjustment{{Type: AdjustmentTypeCPM, Value: 3}},
},
},
VideoOutstream: map[openrtb_ext.BidderName]openrtb_ext.AdjustmentsByDealID{
Copy link
Contributor

Choose a reason for hiding this comment

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

Nitpick: Just to make the test case less verbose, can we remove some of the elements so we are only left with bidder "bidderA" and "bidderB"?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

it's necessary to test all media types banner|video-instream|video-outstream|audio|native|*

},
},
},

Copy link
Contributor

Choose a reason for hiding this comment

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

Nitpick: can we remove the extra space?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed extra line breaks, the next removal will lead to poor readability

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm sorry for the confusion. By extra space I meant line 435 only. I was ok with the rest of the line breaks. I agree that removing them leads to poor readability. I'll approve anyways.

Copy link
Contributor

@guscarreon guscarreon left a comment

Choose a reason for hiding this comment

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

LGTM. Thank you @linux019 for addressing my feedback

@hhhjort hhhjort merged commit 82ba585 into prebid:master Mar 18, 2024
3 checks passed
@linux019 linux019 deleted the fix-panic-in-bids-ajustment branch March 19, 2024 10:02
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.

5 participants