Skip to content

lib: Replace deprecated gets() with fgets() in test files - #7097

Merged
nilason merged 1 commit into
OSGeo:mainfrom
HUN-sp:fix-gets-to-fgets
Feb 20, 2026
Merged

lib: Replace deprecated gets() with fgets() in test files#7097
nilason merged 1 commit into
OSGeo:mainfrom
HUN-sp:fix-gets-to-fgets

Conversation

@HUN-sp

@HUN-sp HUN-sp commented Feb 17, 2026

Copy link
Copy Markdown
Contributor

Fixes #7096

@github-actions github-actions Bot added C Related code is in C libraries tests Related to Test Suite labels Feb 17, 2026
@HUN-sp

HUN-sp commented Feb 17, 2026

Copy link
Copy Markdown
Contributor Author

Hi @petrasovaa,

​I would really appreciate a review on this.
​Regarding my previous PR #7044: After the initial concerns about AI usage, I put in a lot of manual effort to completely refactor and fix it, ensuring it met the project's standards.

​I am very committed to contributing high-quality code to this organization for GSoC. Could you please let me know if there are any issues I need to address?

@nilason

nilason commented Feb 19, 2026

Copy link
Copy Markdown
Contributor

Although the update itself seems ok, I wonder if you tried to compile the file at all. It errored out for me with Clang.

@HUN-sp

HUN-sp commented Feb 19, 2026

Copy link
Copy Markdown
Contributor Author

Hi @nilason, thanks for the review!

I tested compilation with GCC and the warnings I see (incompatible pointer types, implicit function declarations in try2.c) are all pre-existing in the original code — none were introduced by this PR. I verified by compiling both the original and modified files and the warnings are identical. These test files don't have a Makefile and aren't part of the build system, which is why CI passes. Could you share the specific error you're seeing with Clang? I'd be happy to address it if it's related to my changes, or file a separate issue if it's pre-existing.

@nilason

nilason commented Feb 19, 2026

Copy link
Copy Markdown
Contributor

These test were removed from Makefiles in 2008, with the release of GRASS 7.8, as they were already then considered "dead code". They originate, at the latest, in the early 90's and haven't probably been used much since. So updating them to modern code is more of an exercise than of practical importance. Until someone in the team votes for removing the files, let us make it an exercise (and only replacing gets() doesn't make much sense in this situation). Below are the logs for compilation with Clang:

clang /grass/lib/btree/test/try.c -I/grassbuild/output/lib/grass86/include -L/grassbuild/output/lib/grass86/lib -lgrass_btree
/grass/lib/btree/test/try.c:32:22: error: incompatible function pointer types passing 'int (const char *, const char *)' to parameter of type 'int (*)(const void *, const void *)' [-Wincompatible-function-pointer-types]
   32 |     btree_create(&B, strcmp, 10);
      |                      ^~~~~~
/grassbuild/output/lib/grass86/include/grass/defs/btree.h:5:33: note: passing argument to parameter here
    5 | int btree_create(BTREE *, int (*)(const void *, const void *), int);
      |                                 ^
/grass/lib/btree/test/try.c:35:14: warning: 'gets' is deprecated: This function is provided for compatibility reasons only.  Due to security concerns inherent in the design of gets(3), it is highly recommended that you use fgets(3) instead. [-Wdeprecated-declarations]
   35 |         if (!gets(key))
      |              ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/_stdio.h:260:1: note: 'gets' has been explicitly marked deprecated here
  260 | __deprecated_msg("This function is provided for compatibility reasons only.  Due to security concerns inherent in the design of gets(3), it is highly recommended that you use fgets(3) instead.")
      | ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/sys/cdefs.h:227:48: note: expanded from macro '__deprecated_msg'
  227 |         #define __deprecated_msg(_msg) __attribute__((__deprecated__(_msg)))
      |                                                       ^
/grass/lib/btree/test/try.c:36:13: error: call to undeclared library function 'exit' with type 'void (int) __attribute__((noreturn))'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
   36 |             exit(0);
      |             ^
/grass/lib/btree/test/try.c:36:13: note: include the header <stdlib.h> or explicitly provide a declaration for 'exit'
/grass/lib/btree/test/try.c:41:47: warning: format specifies type 'char *' but the argument has type 'void *' [-Wformat]
   41 |             fprintf(stdout, "%s = %s\n", key, d);
      |                                   ~~          ^
/grass/lib/btree/test/try.c:46:14: warning: 'gets' is deprecated: This function is provided for compatibility reasons only.  Due to security concerns inherent in the design of gets(3), it is highly recommended that you use fgets(3) instead. [-Wdeprecated-declarations]
   46 |         if (!gets(data))
      |              ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/_stdio.h:260:1: note: 'gets' has been explicitly marked deprecated here
  260 | __deprecated_msg("This function is provided for compatibility reasons only.  Due to security concerns inherent in the design of gets(3), it is highly recommended that you use fgets(3) instead.")
      | ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/sys/cdefs.h:227:48: note: expanded from macro '__deprecated_msg'
  227 |         #define __deprecated_msg(_msg) __attribute__((__deprecated__(_msg)))
      |                                                       ^
3 warnings and 2 errors generated.
clang /grass/lib/linkm/test/try2.c -I/grassbuild/output/lib/grass86/include -L/grassbuild/output/lib/grass86/lib -lgrass_linkm
/grass/lib/linkm/test/try2.c:39:20: warning: 'gets' is deprecated: This function is provided for compatibility reasons only.  Due to security concerns inherent in the design of gets(3), it is highly recommended that you use fgets(3) instead. [-Wdeprecated-declarations]
   39 |     while (NULL != gets(buf)) {
      |                    ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/_stdio.h:260:1: note: 'gets' has been explicitly marked deprecated here
  260 | __deprecated_msg("This function is provided for compatibility reasons only.  Due to security concerns inherent in the design of gets(3), it is highly recommended that you use fgets(3) instead.")
      | ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/sys/cdefs.h:227:48: note: expanded from macro '__deprecated_msg'
  227 |         #define __deprecated_msg(_msg) __attribute__((__deprecated__(_msg)))
      |                                                       ^
/grass/lib/linkm/test/try2.c:41:43: warning: incompatible pointer types passing 'char *' to parameter of type 'struct link_head *' [-Wincompatible-pointer-types]
   41 |             tmp = (struct link *)link_new(head);
      |                                           ^~~~
/grassbuild/output/lib/grass86/include/grass/defs/linkm.h:17:36: note: passing argument to parameter here
   17 | VOID_T *link_new(struct link_head *);
      |                                    ^
/grass/lib/linkm/test/try2.c:44:17: error: call to undeclared function 'add_link_rev'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
   44 |                 add_link_rev(&List, tmp);
      |                 ^
/grass/lib/linkm/test/try2.c:46:17: error: call to undeclared function 'add_link'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
   46 |                 add_link(&List, tmp);
      |                 ^
/grass/lib/linkm/test/try2.c:49:9: error: call to undeclared function 'dumplist'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
   49 |         dumplist(&List);
      |         ^
/grass/lib/linkm/test/try2.c:55:26: warning: incompatible pointer types passing 'char *' to parameter of type 'struct link_head *' [-Wincompatible-pointer-types]
   55 |             link_dispose(head, p);
      |                          ^~~~
/grassbuild/output/lib/grass86/include/grass/defs/linkm.h:8:37: note: passing argument to parameter here
    8 | void link_dispose(struct link_head *, VOID_T *);
      |                                     ^
/grass/lib/linkm/test/try2.c:55:32: warning: incompatible pointer types passing 'struct link *' to parameter of type 'char *' [-Wincompatible-pointer-types]
   55 |             link_dispose(head, p);
      |                                ^
/grassbuild/output/lib/grass86/include/grass/defs/linkm.h:8:47: note: passing argument to parameter here
    8 | void link_dispose(struct link_head *, VOID_T *);
      |                                               ^
/grass/lib/linkm/test/try2.c:61:18: warning: incompatible pointer types passing 'char *' to parameter of type 'struct link_head *' [-Wincompatible-pointer-types]
   61 |     link_cleanup(head);
      |                  ^~~~
/grassbuild/output/lib/grass86/include/grass/defs/linkm.h:14:37: note: passing argument to parameter here
   14 | void link_cleanup(struct link_head *);
      |                                     ^
/grass/lib/linkm/test/try2.c:63:5: error: call to undeclared library function 'exit' with type 'void (int) __attribute__((noreturn))'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
   63 |     exit(0);
      |     ^
/grass/lib/linkm/test/try2.c:63:5: note: include the header <stdlib.h> or explicitly provide a declaration for 'exit'
/grass/lib/linkm/test/try2.c:66:6: error: conflicting types for 'add_link_rev'
   66 | void add_link_rev(struct link *List, struct link *link)
      |      ^
/grass/lib/linkm/test/try2.c:44:17: note: previous implicit declaration is here
   44 |                 add_link_rev(&List, tmp);
      |                 ^
/grass/lib/linkm/test/try2.c:75:6: error: conflicting types for 'add_link'
   75 | void add_link(struct link *List, struct link *link)
      |      ^
/grass/lib/linkm/test/try2.c:46:17: note: previous implicit declaration is here
   46 |                 add_link(&List, tmp);
      |                 ^
/grass/lib/linkm/test/try2.c:86:6: error: conflicting types for 'dumplist'
   86 | void dumplist(struct link *List)
      |      ^
/grass/lib/linkm/test/try2.c:49:9: note: previous implicit declaration is here
   49 |         dumplist(&List);
      |         ^
5 warnings and 7 errors generated.

@HUN-sp

HUN-sp commented Feb 19, 2026

Copy link
Copy Markdown
Contributor Author

Hi @nilason,
Thanks for the detailed error log — it made the fixes straightforward to track down. Here's a summary of what was corrected in both files: try.c and try2.c:

Replaced gets() with fgets() + strcspn() to strip the newline (the main security fix)
Added #include <stdlib.h> for exit()
Changed head from VOID_T * / char * to struct link_head * to match the API
Added forward declarations for add_link_rev, add_link, and dumplist before main()
Fixed link_dispose() call to pass correct types (struct link_head * and VOID_T *)
Verified with gcc -Wall -Werror — compiles cleanly with zero warnings and zero errors.

Happy to make any changes if something looks off!

@HUN-sp
HUN-sp force-pushed the fix-gets-to-fgets branch 6 times, most recently from b982bcc to d3128eb Compare February 20, 2026 01:25
Comment thread lib/linkm/test/try2.c Outdated
Comment thread lib/linkm/test/try2.c
@HUN-sp
HUN-sp requested a review from nilason February 20, 2026 07:55

@nilason nilason left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks!

@nilason
nilason enabled auto-merge (squash) February 20, 2026 08:43
@HUN-sp

HUN-sp commented Feb 20, 2026

Copy link
Copy Markdown
Contributor Author

Thank you @nilason for the review and guidance!

@nilason
nilason merged commit 3dce098 into OSGeo:main Feb 20, 2026
26 checks passed
@github-actions github-actions Bot added this to the 8.6.0 milestone Feb 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

C Related code is in C libraries tests Related to Test Suite

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] Replace deprecated gets() with fgets() in test files

2 participants