You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

73 lines
1.6 KiB

/*
* Copyright (c) 2018-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#include <string>
char last_char1_Bad(char* s, int i) {
char buf[1024];
int n = snprintf(buf, sizeof(buf), "%s%d", s, i);
return buf[n - 1];
}
char last_char1_Good(char* s, int i) {
char buf[1024];
int n = snprintf(buf, sizeof(buf), "%s%d", s, i);
if (n > 0 && n <= sizeof(buf)) {
return buf[n - 1];
} else {
return '\0';
}
}
char last_char2_Bad(const char* fmt, ...) {
char buf[1024];
va_list args;
va_start(args, fmt);
int n = vsnprintf(buf, sizeof(buf), fmt, args);
va_end(args);
return buf[n - 1];
}
std::string to_string1_Bad(char* s, int i) {
char buf[1024];
int n = snprintf(buf, sizeof(buf), "%s%d", s, i);
return std::string(buf, n);
}
std::string to_string1_Good(char* s, int i) {
char buf[1024];
int n = snprintf(buf, sizeof(buf), "%s%d", s, i);
if (n < 0) {
return NULL;
} else if (n > sizeof(buf)) {
n = sizeof(buf);
}
return std::string(buf, n);
}
std::string to_string2_Bad(const char* fmt, ...) {
char buf[1024];
va_list args;
va_start(args, fmt);
int n = vsnprintf(buf, sizeof(buf), fmt, args);
va_end(args);
return std::string(buf, n);
}
std::string to_string2_Good(const char* fmt, ...) {
char buf[1024];
va_list args;
va_start(args, fmt);
int n = vsnprintf(buf, sizeof(buf), fmt, args);
va_end(args);
if (n < 0) {
return NULL;
} else if (n > sizeof(buf)) {
n = sizeof(buf);
}
return std::string(buf, n);
}