浏览代码

core: new param to give outbut buffer size

- int2strbuf requires now outbut buffer size as parameter
- safer against misuses, suggested by Andrei Pelinescu-Onciul
- if size is less than INT2STR_MAX_LEN, return null pointer
(cherry picked from commit a765213ffa3769577dd7438c95737cb6b98bff74)
Daniel-Constantin Mierla 15 年之前
父节点
当前提交
c8cb74d1b1
共有 1 个文件被更改,包括 8 次插入3 次删除
  1. 8 3
      ut.h

+ 8 - 3
ut.h

@@ -294,10 +294,15 @@ static inline char* int2str_base(unsigned int l, int* len, int base)
 
 /* print int to asciiz in a string buffer
  * - be sure result buffer is at least INT2STR_MAX_LEN in size */
-static inline char* int2strbuf(unsigned int l, char *r, int* len)
+static inline char* int2strbuf(unsigned int l, char *r, int r_size, int* len)
 {
 	int i;
-	
+
+	if(unlikely(r_size<INT2STR_MAX_LEN)) {
+		if (len)
+			*len = 0;
+		return 0; /* => if someone misuses it => crash (feature no. 1) */
+	}
 	i=INT2STR_MAX_LEN-2;
 	r[INT2STR_MAX_LEN-1]=0; /* null terminate */
 	do{
@@ -316,7 +321,7 @@ extern char ut_buf_int2str[INT2STR_MAX_LEN];
 /* returns a pointer to a static buffer containing l in asciiz & sets len */
 static inline char* int2str(unsigned long l, int* len)
 {
-	return int2strbuf(l, ut_buf_int2str, len);
+	return int2strbuf(l, ut_buf_int2str, INT2STR_MAX_LEN, len);
 }
 
 /* Signed INTeger-TO-STRing: convers a long to a string