未对齐的内存上的宽字符串libc函数 [英] Wide string libc functions on unaligned memory

查看:172
本文介绍了未对齐的内存上的宽字符串libc函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,在痛苦的调试之后,我发现像wcslen这样的libc函数在处理非内存对齐的缓冲区时将无声地失败.以我为例,执行wcslen(mystr)会导致错误的长度值,该长度值稍后才产生崩溃(在wcstombs中,assert buff [-1] == 0).

So I've discovered after painful debugging that libc functions like wcslen will fail silently when dealing with non memory-aligned buffers. In my case doing a wcslen( mystr ) resulted in a faulty length value, which only later on produced a crash (in wcstombs, assert buff[-1] == 0).

对我来说,一种解决方案是重新编写我需要在未对齐内存上使用的所有宽字符串函数.这很容易,但也很肮脏,而且由于没有关于libc的哪些部分支持非内存对齐缓冲区的文档,所以我担心问题会在其他地方再次出现.

One solution would be for me to re-write all the wide string functions I need to work on non-aligned memory. This is easy enough but also dirty, and since there is not doc about which parts of libc support non memory aligned buffers, I'm afraid the problem is going to crop up again somewhere else.

确保我的所有指针都对齐不是一个容易的选择,因为我要处理很多子缓冲区缓冲区,并且必须在主缓冲区之外进行数据的手动复制会花费很长时间.例如,在我的情况下,失败的操作类型是当我在大型缓冲区中爬网(循环)字节以寻找宽字符串然后计算长度时.

Ensuring all my pointers are aligned is not an easy option since I deal with a lot of sub-buffer buffers and having to do manual copy of my data outside of the main buffer would take a long time. As an example, in my case the type of operation that fails is when I crawl (loop over) bytes in a large buffer looking for a wide string and then calculate the length.

由于我不打算需要支持太多的平台,在这些平台上指针内存对齐是一个大问题(崩溃),所以还有其他解决方案吗?也许是编译器标志?

Since I don't plan on needing to support too many platforms where pointer memory alignment is a deal-breaker (crash), is there any other solution? A compiler flag perhaps?

我知道某些平台或环境需要内存对齐,但是我正在处理基本的Windows和Linux UserMode,因此,请不要使用诸如您做错了,必须始终对齐所有内容"之类的混乱"答案,除非您有一个实际的解决方案,谢谢.

I know some platforms or environments require memory alignment, but I'm dealing with basic Windows and Linux UserMode, so please no "preachy" answers like "you're doing it wrong, you must always align everything" unless you have an actual solution, thank you.

推荐答案

C和POSIX标准都要求标准对齐,因此,如果将未对齐的指针强制输入到函数中,则会违反合同.

Well, the C and POSIX standard both mandate standard alignment, so if you forcefeed misaligned pointers into functions, you are violating the contract.

因此,我怀疑您能否说服图书馆维护者将其视为错误.

Therefore I doubt you can convince the library maintainers to see that as a bug.

此外,我认为扩大合同的功能要求不会受到太大的吸引.

Further, I think a feature request for broadening the contract won't get much traction.

因此,最后,我最好的也是最后的建议是:编写自己的代码,并明确将指针标记为未对齐.也许在每个功能开始时检查对齐方式并委派给标准实现,以实现最佳速度...

Thus in the end, my best and final advice is: Code your own, and explicitly mark your pointers as misaligned. Maybe check alignment and delegate to the standard implementation at the start of each function, where appropriate for best speed...

我认为您不需要任何复杂的功能来处理可能未对齐的数据,因此无需编写代码.

I don't think you need any of the complex functions for your potentially misaligned data, so code away.

这篇关于未对齐的内存上的宽字符串libc函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆