免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3341 | 回复: 6

[C] 一个文件读写导致的内存访问错误 [复制链接]

论坛徽章:
0
发表于 2013-02-26 12:12 |显示全部楼层
我写了一段代码,过程大概是这样的:
打开一个文件,加密一个字符串, 将加密数据的长度和加密数据写入文件,
写入的秩序是先写入长度(长度为一字节),后写入数据;然后再文件偏移量移到文件开始的位置,
先读取长度,然后按长度读出加密数据。以下是大致的代码:

  1. static int __crypt_wstr( int fd, xso::isymmetric_cipher* cipher, const char* str )
  2. {
  3.     int len = strlen( str );
  4.     uint8_t* ctext = cipher->encrypt( str, &len );
  5.    
  6.     if ( NULL == ctext ) return -1;
  7.    
  8.     uint8_t tmp_len = len;
  9.     int nbytes = write( fd, &tmp_len, 1 );
  10.    
  11.     if ( -1 == nbytes || 1 != nbytes ) {
  12.         free( ctext );
  13.         return -1;
  14.     }
  15.    
  16.     nbytes = write( fd, &ctext, len );
  17.    
  18.     free( ctext );
  19.     ctext = NULL;
  20.    
  21.     return  ( -1 != nbytes && len == nbytes ) ? ( 0 ) : ( -1 );
  22. }

  23. static int __crypt_rstr( int fd, xso::isymmetric_cipher* cipher, char** buf )
  24. {
  25.     uint8_t buf_len = 0;
  26.     int nbytes = read( fd, &buf_len, 1 );
  27.    
  28.     printf( "buf_len=%d\n", buf_len );
  29.    
  30.     if ( -1 == nbytes || 1 != nbytes ) return -1;
  31.    
  32.    
  33.     int len = buf_len;
  34.    
  35.     uint8_t* tmp = (uint8_t*)malloc( len );
  36.    
  37.     printf( "tmp=%p\n", tmp );
  38.    
  39.     if ( NULL == tmp ) return -1;
  40.    
  41.    
  42.     nbytes = read( fd, &tmp, len );
  43.    
  44.     printf( "tmp=%p\n", tmp );
  45.    
  46.     if ( -1 == nbytes || len != nbytes ) {
  47.         free( tmp );
  48.         return -1;
  49.     }
  50.    
  51.    
  52.     *buf = (char*)cipher->decrypt( tmp, &len );
  53.    
  54.     if ( NULL == *buf )  {
  55.         free( tmp );
  56.         return -1;
  57.     }
  58.    
  59.     free( tmp );
  60.    
  61.     *buf[len] = '\0';
  62.    
  63.     return 0;
  64. }



  65.     int fd = open( "a", O_RDWR | O_CREAT, S_IRWXU );
  66.    
  67.     if ( -1 == fd ) {
  68.         free( fp );
  69.         printf( "open error: %s\n", strerror( errno ) );
  70.         return;
  71.     }
  72.    
  73.     free( fp );
  74.    
  75.    
  76.    
  77.     if ( -1 == __crypt_wstr( fd, &c, "abcdef" ) ) {
  78.         close( fd );
  79.         return;
  80.     }
  81.    
  82.     if ( -1 == lseek( fd, SEEK_SET, 0 ) ) {
  83.         close( fd );
  84.         printf( "lseek error: %s\n", strerror( errno ) );
  85.         return;
  86.     }
  87.    
  88.     char* pt = NULL;
  89.    
  90.     if ( -1 == __crypt_rstr( fd, &c, &pt ) ) {
  91.         close( fd );
  92.         return;
  93.     }
复制代码
问题出在__crypt_rstr函数中, 读出数据长度,动态分配tmp,
将数据read到tmp后,tmp的地址被改变,导致接下来一系列
内存访问错误。

请问原因是什么?怎么解决这个问题?多谢!

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
发表于 2013-02-26 13:16 |显示全部楼层
回复 1# osmanthusgfy


   
一字节长度头域,加解密,难道是APDU报文?
还要malloc/free ?
512字节的栈内存都不够?

把文件全读到内存,处理完再覆盖写回不好吗?

论坛徽章:
323
射手座
日期:2013-08-23 12:04:38射手座
日期:2013-08-23 16:18:12未羊
日期:2013-08-30 14:33:15水瓶座
日期:2013-09-02 16:44:31摩羯座
日期:2013-09-25 09:33:52双子座
日期:2013-09-26 12:21:10金牛座
日期:2013-10-14 09:08:49申猴
日期:2013-10-16 13:09:43子鼠
日期:2013-10-17 23:23:19射手座
日期:2013-10-18 13:00:27金牛座
日期:2013-10-18 15:47:57午马
日期:2013-10-18 21:43:38
发表于 2013-02-26 13:32 |显示全部楼层
    nbytes = read( fd, &tmp, len );

--》

    nbytes = read( fd, tmp, len );

论坛徽章:
0
发表于 2013-02-26 17:24 |显示全部楼层
hellioncu 发表于 2013-02-26 13:32
nbytes = read( fd, &tmp, len );

--》
nbytes = read( fd, tmp, len );

ssize_t read(int fd, void *buf, size_t count);   

论坛徽章:
0
发表于 2013-02-26 18:27 |显示全部楼层
回复 3# hellioncu

多谢大哥的回复,原因是你说的这样。

可又问题了:去掉"&", 读出的数据和写入的不一样,
加上后,读出的数据正常了,虽然这样是错误的,
仔细看也不知道是什么原因.

论坛徽章:
323
射手座
日期:2013-08-23 12:04:38射手座
日期:2013-08-23 16:18:12未羊
日期:2013-08-30 14:33:15水瓶座
日期:2013-09-02 16:44:31摩羯座
日期:2013-09-25 09:33:52双子座
日期:2013-09-26 12:21:10金牛座
日期:2013-10-14 09:08:49申猴
日期:2013-10-16 13:09:43子鼠
日期:2013-10-17 23:23:19射手座
日期:2013-10-18 13:00:27金牛座
日期:2013-10-18 15:47:57午马
日期:2013-10-18 21:43:38
发表于 2013-02-26 20:23 |显示全部楼层
osmanthusgfy 发表于 2013-02-26 18:27
回复 3# hellioncu

多谢大哥的回复,原因是你说的这样。


我不知道cipher对象的接口,还是要靠你自己调试了

论坛徽章:
0
发表于 2016-04-26 10:41 |显示全部楼层
最后怎么解决的?加解密写入和读出的不一致,是不是由于编码问题?
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP