Skip to content

Commit

Permalink
Harden the parsing of ld.conf w.r.t. load and CRLF
Browse files Browse the repository at this point in the history
  • Loading branch information
dra27 committed Oct 23, 2024
1 parent f43c241 commit f739417
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 3 deletions.
4 changes: 4 additions & 0 deletions Changes
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ Working version
- #13354: Use C99 flexible array member syntax everywhere.
(Antonin Décimo, review by Miod Vallat, Gabriel Scherer, and Xavier Leroy)

- #13???: CRLF-formatted ld.conf files are correctly loaded on Unix as well as
Windows.
(David Allsopp, review by ???)

### Code generation and optimizations:

### Standard library:
Expand Down
2 changes: 1 addition & 1 deletion bytecomp/dll.ml
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ let ld_conf_contents () =
let ic = open_in (Filename.concat Config.standard_library "ld.conf") in
begin try
while true do
path := input_line ic :: !path
path := Misc.Stdlib.String.rtrim_cr (input_line ic) :: !path
done
with End_of_file -> ()
end;
Expand Down
16 changes: 14 additions & 2 deletions runtime/dynlink.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,13 @@ CAMLexport const char_os * caml_get_stdlib_location(void)
CAMLexport char_os * caml_parse_ld_conf(void)
{
const char_os * stdlib;
char_os * ldconfname, * wconfig, * p, * q;
char_os * ldconfname, * wconfig, * p, * q, * r;
char * config;
#ifdef _WIN32
#define OPEN_FLAGS _O_BINARY | _O_RDONLY
struct _stati64 st;
#else
#define OPEN_FLAGS O_RDONLY
struct stat st;
#endif
int ldconf, nread;
Expand All @@ -110,7 +112,7 @@ CAMLexport char_os * caml_parse_ld_conf(void)
caml_stat_free(ldconfname);
return NULL;
}
ldconf = open_os(ldconfname, O_RDONLY, 0);
ldconf = open_os(ldconfname, OPEN_FLAGS, 0);
if (ldconf == -1)
caml_fatal_error("cannot read loader config file %s",
caml_stat_strdup_of_os(ldconfname));
Expand All @@ -129,13 +131,23 @@ CAMLexport char_os * caml_parse_ld_conf(void)
*p = 0;
caml_ext_table_add(&caml_shared_libs_path, q);
q = p + 1;
} else if (*p == '\r') {
r = p;
/* Allow \r+ */
while (*(++r) == '\r');
if (*r == '\n') {
/* Matched \r+\n */
*p = 0;
}
p = r - 1;
}
}
if (q < p) caml_ext_table_add(&caml_shared_libs_path, q);
close(ldconf);
caml_stat_free(ldconfname);
return wconfig;
}
#undef OPEN_FLAGS

/* Open the given shared library and add it to shared_libs.
Abort on error. */
Expand Down
13 changes: 13 additions & 0 deletions utils/misc.ml
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,19 @@ module Stdlib = struct
in
loop 0

let rtrim_cr s =
if s = "" then s
else
let len = String.length s in
let i = ref len in
while !i > 0 && s.[!i - 1] = '\r' do
decr i
done;
if !i <> len then
String.sub s 0 !i
else
s

let print ppf t =
Format.pp_print_string ppf t
end
Expand Down
2 changes: 2 additions & 0 deletions utils/misc.mli
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,8 @@ module Stdlib : sig
val print : Format.formatter -> t -> unit

val for_all : (char -> bool) -> t -> bool

val rtrim_cr : string -> string
end

external compare : 'a -> 'a -> int = "%compare"
Expand Down

0 comments on commit f739417

Please sign in to comment.