What should be the correct approach(es) to solve __strcpy_sse2_unaligned()
problems?
ie. strcpy(enc_buf, base64_encode(buf));
leads to __strcpy_sse2_unaligned()
in gdb
So what are the precautionary measures to prevent such things?
Asked
Active
Viewed 6,453 times
1

ninja.stop
- 410
- 1
- 10
- 24
-
1Prevent _what_, exactly? – edmz Apr 28 '17 at 17:43
-
@black I meant to say what should we do to solve misalignment problem as given in gdb output – ninja.stop Apr 28 '17 at 17:46
-
Why is it a _problem_ that you find yourself in `__strcpy_sse2_unaligned` in gdb? What exactly would you like to prevent? – zwol Apr 28 '17 at 17:46
-
Till now I have never done specific anything for this. It never gives problem to me. You can assume too much C coding I have done. – Austin Apr 28 '17 at 17:47
-
Are you asking why the compiler generates `__strcpy_sse2_unaligned` rather than, say, `__strcpy_sse2_aligned`? – edmz Apr 28 '17 at 17:48
-
@zwol a code got core dump and strcpy is crashing. so why this unaligned problem came. as I read this is due to misalignment of data in words. So what is the approach we should take to prevent this. – ninja.stop Apr 28 '17 at 17:48
-
No, the crash is not due to misalignment. Use [`valgrind`](http://valgrind.org/) to find the true bug. – zwol Apr 28 '17 at 17:50
-
@black code is crashing due to strcpy. so I wanted to know is there any solution for nonalignment issues – ninja.stop Apr 28 '17 at 17:50
-
Is the problem due to `base64_encode(buf)` or `strcpy(enc_buf, ...);` or the alignments of `buf, end_buf`? I think it may be the first. – chux - Reinstate Monica Apr 28 '17 at 17:51
-
@black I don't have the code, i just have the core dump and the snippet where crashing. – ninja.stop Apr 28 '17 at 17:53
-
1I should explain that `strcpy` is _required_ not to crash due to alignment issues, because `char`s do not need to be aligned. The crash happens to have happened inside a function with `unaligned` in its name, but that doesn't mean anything interesting. The bug is probably either that `enc_buf` is an invalid pointer or `base64_encode` is returning an invalid pointer. Again, `valgrind` will tell you what's really going on. – zwol Apr 28 '17 at 17:57
-
1if it crashes, buf may not have a NUL ending character. It could be also base64_encode() returns without a NUL ending character. – Nguai al Apr 28 '17 at 18:16
1 Answers
2
The compiler is unable to determine whether your source and destination char*
are aligned (usually word or even quad-word aligned) and therefore is backing up to the unaligned yet possibly optimized strcpy
routine. You'd need to explicitly hint about the missed guess it is doing; otherwise, it'll just give up: unaligned access is often disallowed for SSE or still very slow.
But, if you're getting a segfault it is very unlikely it's a library issue (it's a largely used function) and it just cannot be due to misalignment. It's much likely your code exhibits undefined behavior somewhere, due to buffer overrun, for example. Check your pointers with valgrind
or -fsanitize=address
.

edmz
- 8,220
- 2
- 26
- 45