Skip to content

[R] Switch sprintf to snprintf #3724

Closed

Description

@coatless

From @rcurtin regarding the 4.4.0 R build on matrix, we have R CMD check returning:

* checking compiled code ... WARNING
File ‘mlpack/libs/mlpack.so’:
  Found ‘__sprintf_chk’, possibly from ‘sprintf’ (C)
    Object: ‘image_converter.o’

The quick fix here is to switch sprintf() use cases to snprintf() in the offending stb_image_write.h library to ensure a maximum number of bytes to be used in the buffer declared.

/cc @eddelbuettel

Activity

  1. eddelbuettel commented on May 30, 2024

    @eddelbuettel
    Contributor

    Good catch. This is checked for.

  2. conradsnicta commented on May 31, 2024

    @conradsnicta
    Contributor

    @coatless @eddelbuettel Looks like the problem is in the STB dependency. The simplest solution might be to build the R mlpack package without STB image support.

    Suggest to remove all stb related packages from the build environment (eg. libstb-dev on Ubuntu/Debian), or otherwise modify mlpack build scripts (CMakeLists.txt) and/or configuration to explicitly disable stb.

    It's not much of a loss, as reading/writing files is typically handled by users at the R level, not at the C++ level. The main focus of mlpack is machine learning anyway, not image loading.

    Alternatively, you can send a PR to the STB folks fixing this issue (https://github.com/nothings/stb). It may take a while to propagate this downstream (ie. distro packages), so it won't fix the present R packaging problem any time soon.

    Ryan is time constrained, so anything that reduces the load is useful.

  3. rcurtin commented on May 31, 2024

    @rcurtin
    Member

    You are right that image_converter may not be useful to R developers, but honestly I think the easier bet is to just to bundle a manually-patched STB version (it's just two header files) with the R tarball. (In fact distributing image_converter without it is unsafe, as the user's system is not guaranteed to have stb_image.h and stb_image_write.h installed.) The sprintf() to snprintf() change should be trivial anyway, and would be a worthwhile thing to patch upstream.

    (I am time-constrained, but if someone else doesn't get to this before me I'll do it. I don't think it'll take very long.)

  4. eddelbuettel commented on May 31, 2024

    @eddelbuettel
    Contributor

    What @rcurtin suggests is what we commonly do to please CRAN. (I.e. we have edit or ifdef out some things).

    What @conradsnicta refers to also works because will not check externally linked code: only the shared library the package produces.

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

Metadata

Metadata

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions