Skip to content

Fix -Wformat issues - #5769

Merged
jangorecki merged 10 commits into
hotfix1.14.10from
format-warnings
Dec 1, 2023
Merged

jangorecki merged 10 commits into
hotfix1.14.10from
format-warnings

Conversation

@MichaelChirico

@MichaelChirico MichaelChirico commented Nov 26, 2023 •

Copy link
Copy Markdown
Member

Part of #5768

Still missing are any changes for -Wformat-security.

@MichaelChirico

Copy link
Copy Markdown
Member Author

(PS it would be great if we could add these compiler warnings to CI to prevent regressions on this in the future)

@MichaelChirico

MichaelChirico commented Nov 26, 2023 •

Copy link
Copy Markdown
Member Author

I poked around in r-devel a bit to see how they handle R_xlen_t in printf() formatted messages, but didn't see anything.

Therefore the approach in 0d57fd8 seems like the best expedient solution for now -- on most installations it will be a no-op (long -> long cast), and wherever R_xlen_t is just int, it'll cast int -> long.

See comment on the issue thread for why %lld was chosen for R_xlen_t.

@jangorecki

Copy link
Copy Markdown
Member

(PS it would be great if we could add these compiler warnings to CI to prevent regressions on this in the future)

If you would happen to that docker that reproduces that please share. It's probably just compiler version and flags but will make it easier to add.

@jangorecki

Copy link
Copy Markdown
Member

I added -Wformat -Wformat-security to ~/.R/Makevars and used gcc 13.2.0 (same as CRAN's gcc) but I am unable to reproduce those warnings from CRAN. Must be some extra flag then.

This is what we just talked about with @eddelbuettel about lack of CRAN reproducibility...

@eddelbuettel

eddelbuettel commented Nov 27, 2023 •

Copy link
Copy Markdown
Contributor

You need r-devel. I fixed two packages in two days. Works like a charm if you have r-devel and the Makevars change.

I rebuilt each day, but you should not have to. I got my first email about it while I was traveling -- Thu or Fri.

edd@rob:~/git/rcppcnpy(master)$ RD --version
R Under development (unstable) (2023-11-26 r85638) -- "Unsuffered Consequences"
Copyright (C) 2023 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under the terms of the
GNU General Public License versions 2 or 3.
For more information about these matters see
https://www.gnu.org/licenses/.

edd@rob:~/git/rcppcnpy(master)$ 

@MichaelChirico

Copy link
Copy Markdown
Member Author

h/t @HughParsonage for this r-package-devel thread getting directly to the issue:

https://stat.ethz.ch/pipermail/r-package-devel/2023q4/010123.html

That points out R itself has a new macro R_PRIdXLEN_T:

https://github.com/r-devel/r-svn/blob/a9ded8dd775dc0aa75b26a5c88c11983d6d884f9/src/include/Rinternals.h#L75-L79

But we're miles away from being able to depend on such a recent addition to R. We could define the macro ourselves; it will be fragile but seems like no better choice at the moment if we want to avoid long long.

@jangorecki

Copy link
Copy Markdown
Member

From the thread you linked it sounds long long is good enough, will work across different R versions.

@eddelbuettel

Copy link
Copy Markdown
Contributor

I eyeballed the thread too but didn't like it much. I prefer to a) not depend on a particular version or b) make the code more complicated via an #if ... for a given architecture when c) a simple cast takes care of it. So I am with @jangorecki here: a simple cast does it. I used unsigned int to convert a (C++_ sizeof() that made R on Windows bark. All quiet now.

@MichaelChirico

Copy link
Copy Markdown
Member Author

thanks all. was only wary of long long given the comments in the CRAN release notes. Agree it's best otherwise.

@jangorecki

Copy link
Copy Markdown
Member

Considering it was commented out in CRAN release we should be good

@jangorecki

jangorecki commented Nov 29, 2023 •

Copy link
Copy Markdown
Member

testing PR via

sudo docker run -it --rm rocker/r-devel /bin/bash
mkdir -p ~/.R
echo 'CFLAGS=-Wformat' > ~/.R/Makevars
echo 'CXXFLAGS=-Wformat' >> ~/.R/Makevars

## optional
wget https://cran.r-project.org/src/contrib/data.table_1.14.8.tar.gz
RD CMD INSTALL data.table_1.14.8.tar.gz
# warnings reproduced

## PR
rm -fr format-warnings.zip data.table-format-warnings
wget https://github.com/Rdatatable/data.table/archive/refs/heads/format-warnings.zip
unzip format-warnings.zip
RD CMD build data.table-format-warnings --no-build-vignettes
RD CMD INSTALL data.table_1.14.9.tar.gz

@jangorecki jangorecki added this to the 1.14.10 milestone Nov 30, 2023
@jangorecki
jangorecki merged commit 28b67ac into hotfix1.14.10 Dec 1, 2023
@MichaelChirico
MichaelChirico deleted the format-warnings branch December 1, 2023 20:24
Comment thread src/assign.c
else if (TRUELENGTH(names) != oldtncol)
// Use (long long) to cast R_xlen_t to a fixed type to robustly avoid -Wformat compiler warnings, see #5768, PRId64 didnt work
error(_("Internal error: selfrefnames is ok but tl names [%ld] != tl [%ld]"), TRUELENGTH(names), oldtncol); // # nocov
error(_("Internal error: selfrefnames is ok but tl names [%ld] != tl [%d]"), TRUELENGTH(names), oldtncol); // # nocov

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, interesting, does that mean TRUELENGTH(names) and TRUELENGTH(dt) have different types??

I noticed the Wformat error only hit this line once & was wondering why.

@jangorecki jangorecki Dec 1, 2023 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It appears to be like that. I didn't try to understand that, just followed compiler tips.

@jangorecki
jangorecki restored the format-warnings branch December 1, 2023 20:36
jangorecki added a commit that referenced this pull request Dec 2, 2023
jangorecki added a commit that referenced this pull request Dec 2, 2023
@jangorecki
jangorecki deleted the format-warnings branch December 2, 2023 14:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants