Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(445)

Side by Side Diff: src/core/SkString.cpp

Issue 700953003: Avoid dec = -dec overflow when appending most negative signed integers. (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: negate manually Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 1
2 /* 2 /*
3 * Copyright 2006 The Android Open Source Project 3 * Copyright 2006 The Android Open Source Project
4 * 4 *
5 * Use of this source code is governed by a BSD-style license that can be 5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file. 6 * found in the LICENSE file.
7 */ 7 */
8 8
9 9
10 #include "SkString.h" 10 #include "SkString.h"
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
83 SkASSERT(p >= buffer); 83 SkASSERT(p >= buffer);
84 char* stop = buffer + sizeof(buffer); 84 char* stop = buffer + sizeof(buffer);
85 while (p < stop) { 85 while (p < stop) {
86 *string++ = *p++; 86 *string++ = *p++;
87 } 87 }
88 SkASSERT(string - start <= SkStrAppendU32_MaxSize); 88 SkASSERT(string - start <= SkStrAppendU32_MaxSize);
89 return string; 89 return string;
90 } 90 }
91 91
92 char* SkStrAppendS32(char string[], int32_t dec) { 92 char* SkStrAppendS32(char string[], int32_t dec) {
93 uint32_t udec = dec;
93 if (dec < 0) { 94 if (dec < 0) {
94 *string++ = '-'; 95 *string++ = '-';
95 dec = -dec; 96 udec = ~udec + 1; // udec = -udec, but silences some warnings that are trying to be helpful
96 } 97 }
97 return SkStrAppendU32(string, static_cast<uint32_t>(dec)); 98 return SkStrAppendU32(string, udec);
98 } 99 }
99 100
100 char* SkStrAppendU64(char string[], uint64_t dec, int minDigits) { 101 char* SkStrAppendU64(char string[], uint64_t dec, int minDigits) {
101 SkDEBUGCODE(char* start = string;) 102 SkDEBUGCODE(char* start = string;)
102 103
103 char buffer[SkStrAppendU64_MaxSize]; 104 char buffer[SkStrAppendU64_MaxSize];
104 char* p = buffer + sizeof(buffer); 105 char* p = buffer + sizeof(buffer);
105 106
106 do { 107 do {
107 *--p = SkToU8('0' + (int32_t) (dec % 10)); 108 *--p = SkToU8('0' + (int32_t) (dec % 10));
108 dec /= 10; 109 dec /= 10;
109 minDigits--; 110 minDigits--;
110 } while (dec != 0); 111 } while (dec != 0);
111 112
112 while (minDigits > 0) { 113 while (minDigits > 0) {
113 *--p = '0'; 114 *--p = '0';
114 minDigits--; 115 minDigits--;
115 } 116 }
116 117
117 SkASSERT(p >= buffer); 118 SkASSERT(p >= buffer);
118 size_t cp_len = buffer + sizeof(buffer) - p; 119 size_t cp_len = buffer + sizeof(buffer) - p;
119 memcpy(string, p, cp_len); 120 memcpy(string, p, cp_len);
120 string += cp_len; 121 string += cp_len;
121 122
122 SkASSERT(string - start <= SkStrAppendU64_MaxSize); 123 SkASSERT(string - start <= SkStrAppendU64_MaxSize);
123 return string; 124 return string;
124 } 125 }
125 126
126 char* SkStrAppendS64(char string[], int64_t dec, int minDigits) { 127 char* SkStrAppendS64(char string[], int64_t dec, int minDigits) {
128 uint64_t udec = dec;
127 if (dec < 0) { 129 if (dec < 0) {
128 *string++ = '-'; 130 *string++ = '-';
129 dec = -dec; 131 udec = ~udec + 1; // udec = -udec, but silences some warnings that are trying to be helpful
130 } 132 }
131 return SkStrAppendU64(string, static_cast<uint64_t>(dec), minDigits); 133 return SkStrAppendU64(string, udec, minDigits);
132 } 134 }
133 135
134 char* SkStrAppendFloat(char string[], float value) { 136 char* SkStrAppendFloat(char string[], float value) {
135 // since floats have at most 8 significant digits, we limit our %g to that. 137 // since floats have at most 8 significant digits, we limit our %g to that.
136 static const char gFormat[] = "%.8g"; 138 static const char gFormat[] = "%.8g";
137 // make it 1 larger for the terminating 0 139 // make it 1 larger for the terminating 0
138 char buffer[SkStrAppendScalar_MaxSize + 1]; 140 char buffer[SkStrAppendScalar_MaxSize + 1];
139 int len = SNPRINTF(buffer, sizeof(buffer), gFormat, value); 141 int len = SNPRINTF(buffer, sizeof(buffer), gFormat, value);
140 memcpy(string, buffer, len); 142 memcpy(string, buffer, len);
141 SkASSERT(len <= SkStrAppendScalar_MaxSize); 143 SkASSERT(len <= SkStrAppendScalar_MaxSize);
(...skipping 512 matching lines...) Expand 10 before | Expand all | Expand 10 after
654 const size_t len = strcspn(str, delimiters); 656 const size_t len = strcspn(str, delimiters);
655 out->push_back().set(str, len); 657 out->push_back().set(str, len);
656 str += len; 658 str += len;
657 // Skip any delimiters. 659 // Skip any delimiters.
658 str += strspn(str, delimiters); 660 str += strspn(str, delimiters);
659 } 661 }
660 } 662 }
661 663
662 #undef VSNPRINTF 664 #undef VSNPRINTF
663 #undef SNPRINTF 665 #undef SNPRINTF
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698