diff options
author | nyamatongwe <unknown> | 2012-03-27 10:17:29 +1100 |
---|---|---|
committer | nyamatongwe <unknown> | 2012-03-27 10:17:29 +1100 |
commit | d716ac0173cc608a61503ada0f907637285a5d8d (patch) | |
tree | 5f7e95dcbe5d59ce74614cece3ab747cbc0e243e /src | |
parent | 277230dd1ecc8a0f4a359160ad13a68b84dc9e9e (diff) | |
download | scintilla-mirror-d716ac0173cc608a61503ada0f907637285a5d8d.tar.gz |
Protect against modifying annotations with a negative line number or a line
number after the document end.
Diffstat (limited to 'src')
-rw-r--r-- | src/Document.cxx | 18 | ||||
-rw-r--r-- | src/PerLine.cxx | 50 |
2 files changed, 37 insertions, 31 deletions
diff --git a/src/Document.cxx b/src/Document.cxx index 0c73024d5..5ad558b8b 100644 --- a/src/Document.cxx +++ b/src/Document.cxx @@ -1773,12 +1773,14 @@ StyledText Document::AnnotationStyledText(int line) { } void Document::AnnotationSetText(int line, const char *text) { - const int linesBefore = AnnotationLines(line); - static_cast<LineAnnotation *>(perLineData[ldAnnotation])->SetText(line, text); - const int linesAfter = AnnotationLines(line); - DocModification mh(SC_MOD_CHANGEANNOTATION, LineStart(line), 0, 0, 0, line); - mh.annotationLinesAdded = linesAfter - linesBefore; - NotifyModified(mh); + if (line >= 0 && line < LinesTotal()) { + const int linesBefore = AnnotationLines(line); + static_cast<LineAnnotation *>(perLineData[ldAnnotation])->SetText(line, text); + const int linesAfter = AnnotationLines(line); + DocModification mh(SC_MOD_CHANGEANNOTATION, LineStart(line), 0, 0, 0, line); + mh.annotationLinesAdded = linesAfter - linesBefore; + NotifyModified(mh); + } } void Document::AnnotationSetStyle(int line, int style) { @@ -1788,7 +1790,9 @@ void Document::AnnotationSetStyle(int line, int style) { } void Document::AnnotationSetStyles(int line, const unsigned char *styles) { - static_cast<LineAnnotation *>(perLineData[ldAnnotation])->SetStyles(line, styles); + if (line >= 0 && line < LinesTotal()) { + static_cast<LineAnnotation *>(perLineData[ldAnnotation])->SetStyles(line, styles); + } } int Document::AnnotationLength(int line) const { diff --git a/src/PerLine.cxx b/src/PerLine.cxx index 7e716ccc0..0bdb60254 100644 --- a/src/PerLine.cxx +++ b/src/PerLine.cxx @@ -397,28 +397,28 @@ bool LineAnnotation::AnySet() const { } bool LineAnnotation::MultipleStyles(int line) const { - if (annotations.Length() && (line < annotations.Length()) && annotations[line]) + if (annotations.Length() && (line >= 0) && (line < annotations.Length()) && annotations[line]) return reinterpret_cast<AnnotationHeader *>(annotations[line])->style == IndividualStyles; else return 0; } int LineAnnotation::Style(int line) { - if (annotations.Length() && (line < annotations.Length()) && annotations[line]) + if (annotations.Length() && (line >= 0) && (line < annotations.Length()) && annotations[line]) return reinterpret_cast<AnnotationHeader *>(annotations[line])->style; else return 0; } const char *LineAnnotation::Text(int line) const { - if (annotations.Length() && (line < annotations.Length()) && annotations[line]) + if (annotations.Length() && (line >= 0) && (line < annotations.Length()) && annotations[line]) return annotations[line]+sizeof(AnnotationHeader); else return 0; } const unsigned char *LineAnnotation::Styles(int line) const { - if (annotations.Length() && (line < annotations.Length()) && annotations[line] && MultipleStyles(line)) + if (annotations.Length() && (line >= 0) && (line < annotations.Length()) && annotations[line] && MultipleStyles(line)) return reinterpret_cast<unsigned char *>(annotations[line] + sizeof(AnnotationHeader) + Length(line)); else return 0; @@ -432,7 +432,7 @@ static char *AllocateAnnotation(int length, int style) { } void LineAnnotation::SetText(int line, const char *text) { - if (text) { + if (text && (line >= 0)) { annotations.EnsureLength(line+1); int style = Style(line); if (annotations[line]) { @@ -445,7 +445,7 @@ void LineAnnotation::SetText(int line, const char *text) { pah->lines = static_cast<short>(NumberLines(text)); memcpy(annotations[line]+sizeof(AnnotationHeader), text, pah->length); } else { - if (annotations.Length() && (line < annotations.Length()) && annotations[line]) { + if (annotations.Length() && (line >= 0) && (line < annotations.Length()) && annotations[line]) { delete []annotations[line]; annotations[line] = 0; } @@ -469,35 +469,37 @@ void LineAnnotation::SetStyle(int line, int style) { } void LineAnnotation::SetStyles(int line, const unsigned char *styles) { - annotations.EnsureLength(line+1); - if (!annotations[line]) { - annotations[line] = AllocateAnnotation(0, IndividualStyles); - } else { - AnnotationHeader *pahSource = reinterpret_cast<AnnotationHeader *>(annotations[line]); - if (pahSource->style != IndividualStyles) { - char *allocation = AllocateAnnotation(pahSource->length, IndividualStyles); - AnnotationHeader *pahAlloc = reinterpret_cast<AnnotationHeader *>(allocation); - pahAlloc->length = pahSource->length; - pahAlloc->lines = pahSource->lines; - memcpy(allocation + sizeof(AnnotationHeader), annotations[line] + sizeof(AnnotationHeader), pahSource->length); - delete []annotations[line]; - annotations[line] = allocation; + if (line >= 0) { + annotations.EnsureLength(line+1); + if (!annotations[line]) { + annotations[line] = AllocateAnnotation(0, IndividualStyles); + } else { + AnnotationHeader *pahSource = reinterpret_cast<AnnotationHeader *>(annotations[line]); + if (pahSource->style != IndividualStyles) { + char *allocation = AllocateAnnotation(pahSource->length, IndividualStyles); + AnnotationHeader *pahAlloc = reinterpret_cast<AnnotationHeader *>(allocation); + pahAlloc->length = pahSource->length; + pahAlloc->lines = pahSource->lines; + memcpy(allocation + sizeof(AnnotationHeader), annotations[line] + sizeof(AnnotationHeader), pahSource->length); + delete []annotations[line]; + annotations[line] = allocation; + } } + AnnotationHeader *pah = reinterpret_cast<AnnotationHeader *>(annotations[line]); + pah->style = IndividualStyles; + memcpy(annotations[line] + sizeof(AnnotationHeader) + pah->length, styles, pah->length); } - AnnotationHeader *pah = reinterpret_cast<AnnotationHeader *>(annotations[line]); - pah->style = IndividualStyles; - memcpy(annotations[line] + sizeof(AnnotationHeader) + pah->length, styles, pah->length); } int LineAnnotation::Length(int line) const { - if (annotations.Length() && (line < annotations.Length()) && annotations[line]) + if (annotations.Length() && (line >= 0) && (line < annotations.Length()) && annotations[line]) return reinterpret_cast<AnnotationHeader *>(annotations[line])->length; else return 0; } int LineAnnotation::Lines(int line) const { - if (annotations.Length() && (line < annotations.Length()) && annotations[line]) + if (annotations.Length() && (line >= 0) && (line < annotations.Length()) && annotations[line]) return reinterpret_cast<AnnotationHeader *>(annotations[line])->lines; else return 0; |