strlcpy和strlcat
strncpy 等主要的问题还是虽然不会溢出,但是满了就不给缓冲区添加0结束符了,以前在项目里面自己还写了个 safe_strcpy 现在发现早就有了
http://blog.csdn.net/linyt/article/details/4383328
找了一下,代码可以在 libbsd 里面有
/* * Appends src to string dst of size siz (unlike strncat, siz is the * full size of dst, not space left). At most siz-1 characters * will be copied. Always NUL terminates (unless siz <= strlen(dst)). * Returns strlen(src) + MIN(siz, strlen(initial dst)). * If retval >= siz, truncation occurred. */ size_t strlcat( char *dst, const char *src, size_t siz) { char *d = dst; const char *s = src; size_t n = siz; size_t dlen; /* Find the end of dst and adjust bytes left but don't go past end */ while (n-- != 0 && *d != '\0' ) d++; dlen = d - dst; n = siz - dlen; if (n == 0) return (dlen + strlen (s)); while (*s != '\0' ) { if (n != 1) { *d++ = *s; n--; } s++; } *d = '\0' ; return (dlen + (s - src)); /* count does not include NUL */ } /* * Copy src to string dst of size siz. At most siz-1 characters * will be copied. Always NUL terminates (unless siz == 0). * Returns strlen(src); if retval >= siz, truncation occurred. */ size_t strlcpy( char *dst, const char *src, size_t siz) { char *d = dst; const char *s = src; size_t n = siz; /* Copy as many bytes as will fit */ if (n != 0) { while (--n != 0) { if ((*d++ = *s++) == '\0' ) break ; } } /* Not enough room in dst, add NUL and traverse rest of src */ if (n == 0) { if (siz != 0) *d = '\0' ; /* NUL-terminate dst */ while (*s++) ; } return (s - src - 1); /* count does not include NUL */ } |
strlcpy和strlcat的更多相关文章
- Strlcpy和strlcat——一致的、安全的字符串拷贝和串接函数【转】
转自:http://blog.csdn.net/kailan818/article/details/6731772 英文原文: http://www.gratisoft.us/todd/papers/ ...
- C语言strcpy,strncpy和strlcpy讲解
前言 C风格的字符串处理函数有很多,如strcpy().strcat()等等. strcpy与strcat char* strcpy (char* dest, const char* src); ch ...
- coredump简介与coredump原因总结
from:http://www.cnblogs.com/doctorqbw/archive/2011/12/21/2295962.html 千兵卫博士 coredump简介与coredump原 ...
- coredump
COREDUMP调试的使用 一,什么是coredump 跑程序的时候经常碰到SIGNAL 或者 call trace的问题,需要定位解决,这里说的大部分是指对应程序由于各种异常或者bug导致在运行过程 ...
- linux core dump 文件 gdb分析
core dump又叫核心转储, 当程序运行过程中发生异常, 程序异常退出时, 由操作系统把程序当前的内存状况存储在一个core文件中, 叫core dump. (linux中如果内存越界会收到SIG ...
- coredump调试的使用
一,什么是coredump 跑程序的时候经常碰到SIGNAL 或者 call trace的问题,需要定位解决,这里说的大部分是指对应程序由于各种异常或者bug导致在运行过程中异常退出或者中止,并且在满 ...
- core dump gdb调试
core dump又叫核心转储, 当程序运行过程中发生异常, 程序异常退出时, 由操作系统把程序当前的内存状况存储在一个core文件中, 叫core dump. (linux中如果内存越界会收到SIG ...
- 出现segment fault 错误的几种原因
segment fault 段异常各种原因www.MyException.Cn 发布于:2012-11-26 11:48:50 浏览:24次 0 segment fault 段错误各种原因一 造成se ...
- [转]coredump简介与coredump原因总结
[转]coredump简介与coredump原因总结 http://blog.sina.com.cn/s/blog_54f82cc201013srb.html 什么是coredump? 通常情况下co ...
随机推荐
- Boost学习笔记(六) progress_display注意事项
progress_display可以用作基本的进度显示,但它有个固有的缺陷:无法把进度显示输出与程序的输出分离. 这是因为progress_display和所有C++程序一样,都向标准输出(cout) ...
- 让Xcode 8.x能够调试iOS 7.x真机
首先需要 Xcode 7.x.可以去 苹果开发者官网 下载. 打开Finder, 进入文件夹: Xcode .x.app/Contents/Developer/Platforms/iPhoneOS.p ...
- iframe布局
代码如下: <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF ...
- Spring的注解方式实现AOP
Spring对AOP的实现提供了很好的支持.下面我们就使用Spring的注解来完成AOP做一个例子. 首先,为了使用Spring的AOP注解功能,必须导入如下几个包.aspectjrt.jar,asp ...
- Python_Day7_面向对象学习
1.面向对象编程介绍 2.为什么要用面向对象进行开发? 3.面向对象的特性:封装.继承.多态 4.类.方法. 面向过程 VS 面向对象 编程范式 编程是程序员用特定的语法+数据结构+算法组成的代码来告 ...
- (转)javascript 奇淫巧技44招
1.首次为变量赋值时务必使用var关键字 变量没有声明而直接赋值得话,默认会作为一个新的全局变量,要尽量避免使用全局变量. 2.使用===取代== ==和!=操作符会在需要的情况下自动转换数据类型.但 ...
- RabbitMQ之入门
生成者: #coding:utf-8 import sys import pika credentials=pika.PlainCredentials("guest"," ...
- jquery写的ajax
1.jsp <%@ page language="java" import="java.util.*" pageEncoding="utf-8& ...
- Yii2使用教程
安装 中文文档:http://www.yiichina.com/doc/guide/2.0/start-installation 1,安装 这里我直接下载归档文件,压缩包安装了.composer各种麻 ...
- Python ZIP压缩
ru=lambda x:x.decode('u8') rp=lambda x:x.replace('\\','/') gb=lambda x:x.decode('gbk') class ZIP: de ...