Bug 276304 - w(1) with --libxo=json produces invalid JSON output parameter on Croatian locale
Summary: w(1) with --libxo=json produces invalid JSON output parameter on Croatian locale
Status: New
Alias: None
Product: Base System
Classification: Unclassified
Component: bin (show other bugs)
Version: 14.0-RELEASE
Hardware: Any Any
: --- Affects Only Me
Assignee: freebsd-bugs (Nobody)
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2024-01-13 20:12 UTC by Vedran Miletic
Modified: 2024-10-23 07:26 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Vedran Miletic 2024-01-13 20:12:15 UTC
% w --libxo json
{"uptime-information": {"time-of-day":" 9:01PM","uptime":2667682,"days":30,"hours":21,"minutes":1,"seconds":22,"uptime-human":" 30 days, 21:01,","users":2,"load-average-1":0,25,"load-average-5":0,34,"load-average-15":0,30, "user-table": {"user-entry": [{"user":"vedranm","tty":"pts/1","from":"p508c7e40.dip0.t-ipconnect.de","login-time":" 8:44PM","command":"w --libxo json"}, {"user":"vedranm","tty":"pts/2","from":"tmux(40223).%0","login-time":"čet01PM","idle":20,"command":"btop"}]}}}

This is not valid JSON. One can notice the values 0,25, 0,34, and 0,30; number separator is printed as comma, while JSON requires the dot to be used regardless of locale. Setting locale to en_US.utf-8 like

% env LC_ALL=en_US.utf-8 w --libxo json

will produce correct result. Other comands, such as ps, output numbers with decimals as strings (bug 265223) and avoid this issue.
Comment 1 Mina Galić freebsd_triage 2024-01-13 20:20:55 UTC
thank you for the report, Vedran.

My guess is that this will be wrong with any Locale that uses "," for… well, commas, and for any Software that produces floating point results.

If the latter is the case it's a libxo bug, not a bug in how w(1) is using it.
Comment 2 Vedran Miletic 2024-01-13 20:23:58 UTC
That would be the correct observation. Other commands, e.g. ps(1), use strings for decimal numbers (bug 265223) and avoid this issue. Not sure if this is an intentional safety measure.
Comment 3 Mina Galić freebsd_triage 2024-01-13 21:17:51 UTC
for completeness, can you please show the output of locale for the broken behaviour?
Comment 4 Baptiste Daroussin freebsd_committer freebsd_triage 2024-01-13 21:19:47 UTC
What locale are you using: hr_HR.ISO8859-2 or hr_HR.UTF-8?
Comment 5 Baptiste Daroussin freebsd_committer freebsd_triage 2024-01-13 21:55:49 UTC
Ah it does not matter hr_HR is the problem here,
Comment 6 Vedran Miletic 2024-01-13 22:07:26 UTC
(In reply to Mina Galić from comment #3)
Sure.

% locale
LANG=hr_HR.UTF-8
LC_CTYPE="hr_HR.UTF-8"
LC_COLLATE="hr_HR.UTF-8"
LC_TIME="hr_HR.UTF-8"
LC_NUMERIC="hr_HR.UTF-8"
LC_MONETARY="hr_HR.UTF-8"
LC_MESSAGES="hr_HR.UTF-8"
LC_ALL=

(In reply to Baptiste Daroussin from comment #5)
That is correct.
Comment 7 commit-hook freebsd_committer freebsd_triage 2024-10-14 08:45:38 UTC
A commit in branch main references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=bd490be57438a82c22d1274bc58d51142b63f4a0

commit bd490be57438a82c22d1274bc58d51142b63f4a0
Author:     Baptiste Daroussin <bapt@FreeBSD.org>
AuthorDate: 2024-10-14 07:37:46 +0000
Commit:     Baptiste Daroussin <bapt@FreeBSD.org>
CommitDate: 2024-10-14 08:43:38 +0000

    w(1): fix libxo output being locale dependant

    by being locale dependant the json export is invalid in locales where
    the separator for float is a comma.
    The Json and the XML are invalid for login-time when days contains
    contains characters which are not unicode.

    Forcing locale to be C, makes this json and xml output valid and also
    identical accross locales, so reliable for parsers

    PR:             276304
    Reported by:    Vedran Miletic <vedran@miletic.net>

 usr.bin/w/w.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
Comment 8 commit-hook freebsd_committer freebsd_triage 2024-10-23 07:26:06 UTC
A commit in branch stable/14 references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=6c1a174e4c0a01a40aa1c06dfc07a37a31cabe33

commit 6c1a174e4c0a01a40aa1c06dfc07a37a31cabe33
Author:     Baptiste Daroussin <bapt@FreeBSD.org>
AuthorDate: 2024-10-14 07:37:46 +0000
Commit:     Baptiste Daroussin <bapt@FreeBSD.org>
CommitDate: 2024-10-23 07:25:50 +0000

    w(1): fix libxo output being locale dependant

    by being locale dependant the json export is invalid in locales where
    the separator for float is a comma.
    The Json and the XML are invalid for login-time when days contains
    contains characters which are not unicode.

    Forcing locale to be C, makes this json and xml output valid and also
    identical accross locales, so reliable for parsers

    PR:             276304
    Reported by:    Vedran Miletic <vedran@miletic.net>

    (cherry picked from commit bd490be57438a82c22d1274bc58d51142b63f4a0)

 usr.bin/w/w.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)