sort: Fix message catalogue usage

- Check that catopen() succeeded before calling catclose().  musl will
  crash in the latter if the catalogue descriptor is -1.
- Keep the message catalogue open for most of sort(1)'s actual
  operation.
- Don't use catgets(3) to print error messages if catopen(3) had failed.

Reviewed by:	arichardson, emaste
MFC after:	1 week
Sponsored by:	The FreeBSD Foundation
Differential Revision:	https://reviews.freebsd.org/D34081
This commit is contained in:
Mark Johnston 2022-01-28 12:49:28 -05:00
parent 29d481ae6a
commit 8d8b9b560a
2 changed files with 12 additions and 10 deletions

View File

@ -57,7 +57,7 @@ __FBSDID("$FreeBSD$");
#ifndef WITHOUT_NLS
#include <nl_types.h>
nl_catd catalog;
nl_catd catalog = (nl_catd)-1;
#endif
#define OPTIONS "bcCdfghik:Mmno:RrsS:t:T:uVz"
@ -1011,6 +1011,10 @@ main(int argc, char **argv)
set_tmpdir();
set_sort_opts();
#ifndef WITHOUT_NLS
catalog = catopen("sort", NL_CAT_LOCALE);
#endif
fix_obsolete_keys(&argc, argv);
while (((c = getopt_long(argc, argv, OPTIONS, long_options, NULL))
@ -1194,17 +1198,9 @@ main(int argc, char **argv)
argv = argv_from_file0;
}
#ifndef WITHOUT_NLS
catalog = catopen("sort", NL_CAT_LOCALE);
#endif
if (sort_opts_vals.cflag && sort_opts_vals.mflag)
errx(1, "%c:%c: %s", 'm', 'c', getstr(1));
#ifndef WITHOUT_NLS
catclose(catalog);
#endif
if (keys_num == 0) {
keys_num = 1;
keys = sort_realloc(keys, sizeof(struct key_specs));
@ -1343,5 +1339,10 @@ main(int argc, char **argv)
sort_free(outfile);
#ifndef WITHOUT_NLS
if (catalog != (nl_catd)-1)
catclose(catalog);
#endif
return (result);
}

View File

@ -49,7 +49,8 @@
#include <nl_types.h>
extern nl_catd catalog;
#define getstr(n) catgets(catalog, 1, n, nlsstr[n])
#define getstr(n) \
(catalog == (nl_catd)-1 ? nlsstr[n] : catgets(catalog, 1, n, nlsstr[n]))
#endif
extern const char *nlsstr[];