Use checked allocation everywhere
Signed-off-by: Yuxuan Shui <yshuiv7@gmail.com>
This commit is contained in:
@@ -1,12 +1,13 @@
|
||||
#include <string.h>
|
||||
|
||||
#include "compiler.h"
|
||||
#include "string_utils.h"
|
||||
#include "utils.h"
|
||||
/**
|
||||
* Allocate the space and copy a string.
|
||||
*/
|
||||
char *mstrcpy(const char *src) {
|
||||
char *str = cmalloc(strlen(src) + 1, char);
|
||||
auto str = ccalloc(strlen(src) + 1, char);
|
||||
|
||||
strcpy(str, src);
|
||||
|
||||
@@ -17,7 +18,7 @@ char *mstrcpy(const char *src) {
|
||||
* Allocate the space and copy a string.
|
||||
*/
|
||||
char *mstrncpy(const char *src, unsigned len) {
|
||||
char *str = cmalloc(len + 1, char);
|
||||
auto str = ccalloc(len + 1, char);
|
||||
|
||||
strncpy(str, src, len);
|
||||
str[len] = '\0';
|
||||
@@ -29,7 +30,7 @@ char *mstrncpy(const char *src, unsigned len) {
|
||||
* Allocate the space and join two strings.
|
||||
*/
|
||||
char *mstrjoin(const char *src1, const char *src2) {
|
||||
char *str = cmalloc(strlen(src1) + strlen(src2) + 1, char);
|
||||
auto str = ccalloc(strlen(src1)+strlen(src2)+1, char);
|
||||
|
||||
strcpy(str, src1);
|
||||
strcat(str, src2);
|
||||
@@ -42,8 +43,7 @@ char *mstrjoin(const char *src1, const char *src2) {
|
||||
*/
|
||||
char *
|
||||
mstrjoin3(const char *src1, const char *src2, const char *src3) {
|
||||
char *str = cmalloc(strlen(src1) + strlen(src2)
|
||||
+ strlen(src3) + 1, char);
|
||||
auto str = ccalloc(strlen(src1)+strlen(src2)+strlen(src3)+1, char);
|
||||
|
||||
strcpy(str, src1);
|
||||
strcat(str, src2);
|
||||
@@ -56,8 +56,7 @@ mstrjoin3(const char *src1, const char *src2, const char *src3) {
|
||||
* Concatenate a string on heap with another string.
|
||||
*/
|
||||
void mstrextend(char **psrc1, const char *src2) {
|
||||
*psrc1 = crealloc(*psrc1, (*psrc1 ? strlen(*psrc1): 0) + strlen(src2) + 1,
|
||||
char);
|
||||
*psrc1 = crealloc(*psrc1, (*psrc1 ? strlen(*psrc1) : 0)+strlen(src2)+1);
|
||||
|
||||
strcat(*psrc1, src2);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user