Skip to content

Commit

Permalink
Bug 1388789 - change return values of nsTextFormatter::vs{s,v}printf;…
Browse files Browse the repository at this point in the history
… r=froydnj

nsTextFormatter::vsnprintf is defined to return uint32_t(-1) on error.
However, it was not doing this.

nsTextFormatter::vssprintf is defined as infallible; enforce this by
having it return void.

MozReview-Commit-ID: LdOhIHzRvAT
  • Loading branch information
tromey committed Sep 5, 2017
1 parent 87b98cf commit c2f1aff
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
17 changes: 9 additions & 8 deletions xpcom/string/nsTextFormatter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1203,19 +1203,17 @@ StringStuff(SprintfStateStr* aState, const char16_t* aStr, uint32_t aLen)
return 0;
}

uint32_t
void
nsTextFormatter::ssprintf(nsAString& aOut, const char16_t* aFmt, ...)
{
va_list ap;
uint32_t rv;

va_start(ap, aFmt);
rv = nsTextFormatter::vssprintf(aOut, aFmt, ap);
nsTextFormatter::vssprintf(aOut, aFmt, ap);
va_end(ap);
return rv;
}

uint32_t
void
nsTextFormatter::vssprintf(nsAString& aOut, const char16_t* aFmt, va_list aAp)
{
SprintfStateStr ss;
Expand All @@ -1226,8 +1224,7 @@ nsTextFormatter::vssprintf(nsAString& aOut, const char16_t* aFmt, va_list aAp)
ss.stuffclosure = &aOut;

aOut.Truncate();
int n = dosprintf(&ss, aFmt, aAp);
return n ? n - 1 : n;
dosprintf(&ss, aFmt, aAp);
}

/*
Expand Down Expand Up @@ -1286,13 +1283,17 @@ nsTextFormatter::vsnprintf(char16_t* aOut, uint32_t aOutLen,
ss.base = aOut;
ss.cur = aOut;
ss.maxlen = aOutLen;
(void) dosprintf(&ss, aFmt, aAp);
int result = dosprintf(&ss, aFmt, aAp);

/* If we added chars, and we didn't append a null, do it now. */
if ((ss.cur != ss.base) && (*(ss.cur - 1) != '\0')) {
*(--ss.cur) = '\0';
}

if (result < 0) {
return -1;
}

n = ss.cur - ss.base;
return n ? n - 1 : n;
}
Expand Down
4 changes: 2 additions & 2 deletions xpcom/string/nsTextFormatter.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class nsTextFormatter
* sprintf into an existing nsAString, overwriting any contents it already
* has. Infallible.
*/
static uint32_t ssprintf(nsAString& aOut, const char16_t* aFmt, ...);
static void ssprintf(nsAString& aOut, const char16_t* aFmt, ...);

private:

Expand All @@ -64,7 +64,7 @@ class nsTextFormatter
*/
static uint32_t vsnprintf(char16_t* aOut, uint32_t aOutLen, const char16_t* aFmt,
va_list aAp);
static uint32_t vssprintf(nsAString& aOut, const char16_t* aFmt, va_list aAp);
static void vssprintf(nsAString& aOut, const char16_t* aFmt, va_list aAp);
};

#endif /* nsTextFormatter_h___ */

0 comments on commit c2f1aff

Please sign in to comment.