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

Issue 9702002: Enable positional parameters for base::vsnprintf and base::vswprintf on Windows. (Closed)

Created:
8 years, 9 months ago by alexeypa (please no reviews)
Modified:
8 years, 9 months ago
CC:
Wez, Jamie, chromium-reviews, jshin+watch_chromium.org, brettw-cc_chromium.org
Visibility:
Public.

Description

Enable positional parameters for base::vsnprintf and base::vswprintf on Windows. This is to avoid bugs like http://code.google.com/p/chromium/issues/detail?id=118064 in the future. Positional parameters are supported by the VC++ runtime via the set of "_p" flavored printf-like routines. MSDN claims that "By default the positional functions behave identically to the non position ones, if no positional formatting is present." There is a little difference that require some attention though. Currently base::vsnprintf and base::vswprintf wrappers use "_s" functions (so called "security enhanced versions"). Judging by looking at the CRT code, _p functions implement the same checks as _s ones do. The base wrappers call the CRT routines so that count == (sizeOfBuffer - 1). This reduces most of the additional code in _vsnprintf_s (comparing to _vsprintf_p) to no-op. Namely: 1. When truncation happens the tail of the buffer is filled with a pattern: _SECURECRT__FILL_STRING(string, sizeInBytes, count + 1); This does not happen in our case because sizeInBytes == count + 1. 2. The special case check shown below was never true since sizeInBytes != count in our case: if (count == 0 && string == NULL && sizeInBytes == 0) 3. The following checks in _vsnprintf_s are also implemented in _vsnprintf_helper: _VALIDATE_RETURN(format != NULL, EINVAL, -1); _VALIDATE_RETURN(string != NULL && sizeInBytes > 0, EINVAL, -1); The only remaining difference between _vsnprintf_s and _vsprintf_p is that the former NULL-terminates the buffer and fills the rest a pattern if _vsnprintf_helper failed because of any reason other than truncation: string[0] = 0; _SECURECRT__FILL_STRING(string, sizeInBytes, 1); This CL write NULL to the end of the buffer in any error case (truncation or other reason). Committed: http://src.chromium.org/viewvc/chrome?view=rev&revision=127788

Patch Set 1 #

Patch Set 2 : NULL terminate the beginning of the buffer in case of a failure + a unit test. #

Unified diffs Side-by-side diffs Delta from patch set Stats (+25 lines, -6 lines) Patch
M base/string_util_win.h View 1 2 chunks +12 lines, -6 lines 0 comments Download
M base/stringprintf_unittest.cc View 1 1 chunk +13 lines, -0 lines 0 comments Download

Messages

Total messages: 14 (0 generated)
alexeypa (please no reviews)
Please take a look.
8 years, 9 months ago (2012-03-13 23:55:16 UTC) #1
willchan no longer on Chromium
I'm inclined to approve this. Before I do so, let me voice a concern: http://connect.microsoft.com/VisualStudio/feedback/details/98913/support-positional-parameters-in-mainstream-printf-functionsclaims ...
8 years, 9 months ago (2012-03-14 09:47:12 UTC) #2
alexeypa (please no reviews)
On 2012/03/14 09:47:12, willchan wrote: > I'm inclined to approve this. Before I do so, ...
8 years, 9 months ago (2012-03-14 17:36:03 UTC) #3
alexeypa (please no reviews)
ping.
8 years, 9 months ago (2012-03-16 17:46:06 UTC) #4
willchan no longer on Chromium
Sorry. I agree with you analysis. Thanks for doing it and sorry for the latency. ...
8 years, 9 months ago (2012-03-16 17:47:27 UTC) #5
commit-bot: I haz the power
CQ is trying da patch. Follow status at https://chromium-status.appspot.com/cq/alexeypa@chromium.org/9702002/1
8 years, 9 months ago (2012-03-16 18:04:55 UTC) #6
commit-bot: I haz the power
Try job failure for 9702002-1 (retry) on win_rel for step "base_unittests". It's a second try, ...
8 years, 9 months ago (2012-03-16 20:31:13 UTC) #7
alexeypa (please no reviews)
Could you please have another look? The first version broke base_unittests!StringPrintfTest.Invalid that expected NULL in ...
8 years, 9 months ago (2012-03-17 00:26:44 UTC) #8
alexeypa (please no reviews)
ping
8 years, 9 months ago (2012-03-20 15:44:55 UTC) #9
willchan no longer on Chromium
lgtm
8 years, 9 months ago (2012-03-20 17:08:08 UTC) #10
commit-bot: I haz the power
CQ is trying da patch. Follow status at https://chromium-status.appspot.com/cq/alexeypa@chromium.org/9702002/14001
8 years, 9 months ago (2012-03-20 17:08:43 UTC) #11
commit-bot: I haz the power
Try job failure for 9702002-14001 on linux_rel for step "update". http://build.chromium.org/p/tryserver.chromium/buildstatus?builder=linux_rel&number=10559 Step "update" is always ...
8 years, 9 months ago (2012-03-20 17:10:32 UTC) #12
commit-bot: I haz the power
CQ is trying da patch. Follow status at https://chromium-status.appspot.com/cq/alexeypa@chromium.org/9702002/14001
8 years, 9 months ago (2012-03-20 18:55:00 UTC) #13
commit-bot: I haz the power
8 years, 9 months ago (2012-03-20 21:22:25 UTC) #14
Change committed as 127788

Powered by Google App Engine
This is Rietveld 408576698