-
-
Notifications
You must be signed in to change notification settings - Fork 112
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
use better overflow check #2768
Open
mulle-nat
wants to merge
1
commit into
dankamongmen:master
Choose a base branch
from
MulleUI:overflow-fix2
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -533,29 +533,15 @@ make_ncpile(notcurses* nc, ncplane* n){ | |
} | ||
|
||
|
||
static inline size_t ncplane_sizeof_cellarray( unsigned rows, unsigned cols) | ||
static inline size_t ncplane_sizeof_cellarray( size_t rows, size_t cols) | ||
{ | ||
size_t fbsize; | ||
size_t size; | ||
|
||
// (nat) This protects against size_t overflow and also checks | ||
// that dimensions are not zero en-passant. Why ? | ||
// Assume: size_t is 16 bit, unsigned is 8 bit (UINT_MAX: 0xFF) | ||
// nccell is sizeof( *p->fb): 0x10 (128 bit) | ||
// | ||
// 0xFF * 0xFF * 0x10 = 0xFE010, but overflows so: 0xE010 | ||
// 0x3F * 0x3F * 0x10 = 0xf810 is the biggest square possible | ||
|
||
size = (size_t) cols * (size_t) rows; | ||
if( size < (size_t) cols || size < (size_t) rows) | ||
// check if multiplication would overflow | ||
// calloc will deal with overflow due to sizeof(nccell) | ||
if( ! rows || (cols > SIZE_MAX / rows)) | ||
return 0; | ||
|
||
fbsize = sizeof( struct nccell) * size; | ||
if( fbsize <= size) | ||
return 0; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. also here There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thats deleted code though 😉 |
||
|
||
return fbsize; | ||
return rows * cols; | ||
} | ||
|
||
// create a new ncplane at the specified location (relative to the true screen, | ||
// having origin at 0,0), having the specified size, and put it at the top of | ||
// the planestack. its cursor starts at its origin; its style starts as null. | ||
|
@@ -615,8 +601,8 @@ ncplane* ncplane_new_internal(notcurses* nc, ncplane* n, | |
p->lenx = nopts->cols; | ||
} | ||
|
||
size_t fbsize = ncplane_sizeof_cellarray( p->leny, p->lenx); | ||
if( ! fbsize || (p->fb = calloc(1,fbsize)) == NULL){ | ||
size_t fbsize = ncplane_sizeof_cellarray(p->leny, p->lenx); | ||
if(!fbsize || (p->fb = calloc(sizeof(struct nccell),fbsize)) == NULL){ | ||
logerror("error allocating cellmatrix (r=%u, c=%u)", | ||
p->leny, p->lenx); | ||
free(p); | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we'll want a diagnostic. see other uses of
LOG_
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's not easy to see from the way github presents these changes, but the return 0 will leas to:
so there is a logerror there and the huge or zero values will provide ample clues as what went wrong. (IMO)