Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compile warnings on OpenBSD #6075

Closed
vicuna opened this issue Jul 14, 2013 · 8 comments
Closed

Compile warnings on OpenBSD #6075

vicuna opened this issue Jul 14, 2013 · 8 comments
Assignees
Milestone

Comments

@vicuna
Copy link

vicuna commented Jul 14, 2013

Original bug ID: 6075
Reporter: jfc
Assigned to: @xavierleroy
Status: closed (set by @xavierleroy on 2015-12-11T18:26:35Z)
Resolution: open
Priority: low
Severity: minor
Platform: amd64
OS: OpenBSD
OS Version: 5.3
Version: 4.00.1
Target version: 4.01.1+dev
Fixed in version: 4.02.0+dev
Category: runtime system and C interface
Tags: patch
Monitored by: @gasche @avsm

Bug description

OpenBSD prints warnings when unsafe library functions like strcpy are used. These functions are used internally in ocaml, so linking any native code program will cause warnings like

/usr/local/lib/ocaml/libasmrun.a(sys.o)(.text+0x7f7): In function caml_sys_open': : warning: strcpy() is almost always misused, please use strlcpy() /usr/local/lib/ocaml/libasmrun.a(unix.o)(.text+0x265): In function caml_search_in_path':
: warning: strcat() is almost always misused, please use strlcat()
/usr/local/lib/ocaml/libasmrun.a(ints.o)(.text+0x622): In function `caml_nativeint_format':
: warning: sprintf() is often misused, please use snprintf()

The functions are used safely, but the linker has no way to know they are used safely.

Fixing this for modern Unix systems is simple. However, Windows does not have snprintf and strlcpy. The strcpy and strcat warnings can be portably avoided by using strlen+memcpy. Avoiding the warning for sprintf would require #ifdef to test which C runtime library is available.

File attachments

@vicuna
Copy link
Author

vicuna commented Jul 15, 2013

Comment author: @xavierleroy

There is something unpleasant in having to rewrite working code (I think) just to silence linker warnings. Moreover, strlcpy and strlcat are BSD-only functions: they are not part of any standard (C, POSIX, SUS, etc) that I know of, and are not even available in GlibC. This adds insult to injury.

In more details: I agree the uses of strcpy and strcat mentioned in the PR can easily be rewritten without. I also agree that snprintf would make the number formatting functions in ints.c and floats.c not only safer, but also simpler. (We could let snprintf tell us how many bytes of output are really needed, instead of guessing beforehand.)

So, Windows is, as usual, the limiting factor. Note that recent (how recent?) MS CRT libraries have _snprintf, which is close except that it doesn't compute the number of characters to be written...

@vicuna
Copy link
Author

vicuna commented Jul 15, 2013

Comment author: @avsm

One option is to bundle a copy of bsd-snprintf.c from the openssh-portable distribution, and use the local copy if the function isn't detected in a configure script. Is this even an option for OCaml, or is the licensing a problem?

Without that, all portability paths seem to require keeping the format length calculation code around behind an #ifdef, thanks to Windows' remarkable choice of behaviour if the buffer is too small in _sprintf.

Incidentally, the linker warning was introduced after sweeping most of the OpenBSD source tree clean of strcpy/strcat use. The recommendation to use strlcpy is a local one only -- we obviously can't control whether glibc chooses to adopt those interfaces or not. A quick glance around the uses in OCaml do look like they always know the string length just before the strcpy, and can be easily rewritten using a more explicit memmove.

I can introduce a local patch to the OpenBSD port for snprintf very easily (or even use asprintf as a slower but simpler change), but would prefer that the strcpy/strcat use is fixed upstream before adding a port-patch.

@vicuna
Copy link
Author

vicuna commented Jul 29, 2013

Comment author: @xavierleroy

More investigation suggests that MS CRT provides not just non-C99-compliant _snprintf and _vsnprintf functions, but also a _vscprintf function that computes the length of the output without generating it. Stitching the two together, we may have a solution.

Attached is a proof-of-concept C implementation of a caml_alloc_sprintf() function that works just like sprintf(), but allocates its result in the Caml heap and returns it. Untested yet; code review and comments most welcome. This new function would be a very welcome replacement to the hacks we currently do around sprintf for formatting numbers.

@vicuna
Copy link
Author

vicuna commented Aug 1, 2013

Comment author: jfc

It is necessary to call va_end and va_start before calling vsnprintf a second time. Diff uploaded.

I may be away from my OpenBSD system for a few weeks. I will do more testing when I return.

@vicuna
Copy link
Author

vicuna commented Jan 17, 2014

Comment author: @damiendoligez

It is necessary to call va_end and va_start before calling vsnprintf a second time. Diff uploaded.

I don't know anything about varargs, but this is probably also the case for the _vscprintf and second _vsnprintf in the Windows branch.

@vicuna
Copy link
Author

vicuna commented Feb 5, 2014

Comment author: @oandrieu

FYI, mingw does provide C99-compliant snprintf & vsnprintf. So both versions should work for mingw.

I attached a sprintf.c version with va_start/va_end wrapping all *vprintf calls. calls added.

@vicuna
Copy link
Author

vicuna commented Feb 5, 2014

Comment author: @xavierleroy

Thanks, Olivier, for the info. I have a first cut of a patch that replaces strcpy, sprintf et al with safer code. Just need to clean it and have it reviewed. But after seeing http://natashenka.ca/posters/ I agree this is worth fixing by the next major release :-)

@vicuna
Copy link
Author

vicuna commented Apr 15, 2014

Comment author: @xavierleroy

Tentative fix in commit 14607 on trunk. Works fine under Linux and BSD but needs testing under Windows. Should normally go in OCaml 4.02.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants