Skip to content

Conversation

@nbriggs
Copy link
Collaborator

@nbriggs nbriggs commented Feb 25, 2025

Replaces ConcNameAndVersion and ConcDirAndName macros with conc_name_and_version and conc_dir_and_name functions respectively, moves the definitions from locfile.h to dsk.c, and adds them to dskdefs.h for use by other source files.

This is not actually ready to merge - testing Google Copilot analysis of pull requests for maiko code.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Hello @nbriggs, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

Summary of Changes

I am currently reviewing this pull request. In the meanwhile, I am providing a summary of the content of the pull request for you and other reviewers to get up to speed quickly on the changes that have occurred, and the intent behind these changes.

This pull request replaces the ConcNameAndVersion and ConcDirAndName macros with conc_name_and_version and conc_dir_and_name functions, respectively. The definitions are moved from locfile.h to dsk.c, and they are added to dskdefs.h for use by other source files. This change aims to improve code readability and maintainability by replacing macros with functions.

Highlights

  • Macro Replacement: Replaces the ConcNameAndVersion and ConcDirAndName macros with functions conc_name_and_version and conc_dir_and_name.
  • Code Relocation: Moves the definitions of these functions from locfile.h to dsk.c.
  • Header Update: Adds the function declarations to dskdefs.h to make them accessible to other source files.

Changelog

  • inc/dskdefs.h
    • Added declarations for conc_dir_and_name and conc_name_and_version functions (lines 22 and 23).
  • inc/locfile.h
    • Removed the definitions of the ConcDirAndName and ConcNameAndVersion macros (lines 431-467 and 486-495).
  • src/dsk.c
    • Removed include of locfile.h which contained macro definitions (line 30).
    • Added definitions for conc_dir_and_name (lines 2544-2580) and conc_name_and_version (lines 2602-2611) functions.
    • Replaced usages of ConcNameAndVersion macro with calls to conc_name_and_version function in COM_openfile (line 378), DSK_getfilename (lines 890, 930, 979, 1005), DSK_deletefile (line 1145), DSK_renamefile (lines 1308, 1323, 1363), COM_getfileinfo (line 1625), COM_setfileinfo (line 1814), unixpathname (line 832), lisppathname (line 1090), and quote_fname (line 1189) in ufs.c.
    • Replaced usages of ConcDirAndName macro with calls to conc_dir_and_name function in DSK_getfilename (lines 969, 998, 1021), DSK_renamefile (line 1297), maintain_version (lines 3309, 3317), get_versionless (line 3422), check_vless_link (line 3501), get_old (lines 3506, 3518, 3546, 3599, 3618, 3705, 3717, 3745, 3770, 3795, 3814, 3826), get_oldest (lines 3792, 3804, 3832, 3857, 3882, 3901, 3913), get_new (lines 3989, 3990, 3999, 4024, 4037, 4053, 4086, 4091, 4134, 4153, 4178, 4191), and get_old_new (lines 4280, 4281, 4288, 4305, 4317, 4333, 4362, 4374, 4413, 4432, 4449, 4461, 4477).
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in issue comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist is currently in preview and may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments to provide feedback.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.


Trivia time!

What is the primary difference between a macro and a function in C?

Click here for the answer
Macros are preprocessed, performing textual substitution, while functions are compiled and called at runtime.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request replaces macros with functions, which is a good step towards improving code maintainability and readability. The code is generally well-structured, but there are a few areas that could be improved, particularly regarding string handling safety.

Summary of Findings

  • Unsafe string operations: The conc_name_and_version function uses strcpy and strcat which are known to be unsafe if the destination buffer is not large enough to hold the concatenated string. This could lead to buffer overflows and potential security vulnerabilities.
  • DOS #ifdef placement: The placement of the #ifdef DOS block in src/dsk.c was moved, but it's unclear if this change was intentional or if it affects the code's behavior under DOS.

Assessment

The pull request replaces the ConcNameAndVersion and ConcDirAndName macros with functions, which improves code readability and maintainability. The changes are generally well-structured, but there are a few areas that could be improved. I recommend addressing the comments before merging, and users should have others review and approve this code before merging.

@nbriggs
Copy link
Collaborator Author

nbriggs commented Feb 25, 2025

/gemini-review

@gemini-code-assist
Copy link

Thanks for the pull request! Here's a summary of the changes and some suggestions.

Summary
This pull request replaces the ConcNameAndVersion and ConcDirAndName macros with functions, moves the definitions from locfile.h to dsk.c, and adds them to dskdefs.h.

General Comments

  • The conversion from macros to functions is a good step towards improving code readability and maintainability.
  • Moving the definitions to dsk.c and including them in dskdefs.h seems like a reasonable way to manage the scope and accessibility of these functions.
  • The addition of size_t arguments to conc_dir_and_name and conc_name_and_version is a good practice to prevent buffer overflows.
  • Consider adding comments to the function definitions in dsk.c to explain their purpose and usage, similar to the comments that were present with the macros.
  • The XXX comment in conc_name_and_version is a good reminder of potential safety issues. Consider using strlcpy and strlcat instead of strcpy and strcat to prevent buffer overflows. You've already done this, so great job!

I didn't find any specific style guide being followed, so I'll assume you're following general C conventions. Here's a summary of the style guide elements referenced in my review:

  • Comments: Adding comments to explain the purpose and usage of functions.
  • Error Prevention: Using strlcpy and strlcat instead of strcpy and strcat to prevent buffer overflows.

@nbriggs
Copy link
Collaborator Author

nbriggs commented Feb 25, 2025

/gemini-review

@nbriggs
Copy link
Collaborator Author

nbriggs commented Feb 25, 2025

/gemini review

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

The pull request replaces macros with functions, which is a good practice for improving code maintainability and readability. The changes also include the use of strlcpy and strlcat to prevent buffer overflows, which is a good security practice.

Summary of Findings

  • Buffer size consistency: Ensure that the buffer size passed to conc_name_and_version and conc_dir_and_name is consistent with the actual buffer size.
  • String concatenation: Consider using strlcat instead of strcat to prevent buffer overflows.

Assessment

The pull request replaces macros with functions to improve code maintainability and readability. The changes also include the use of strlcpy and strlcat to prevent buffer overflows, which is a good security practice. However, there are some areas where the code can be further improved, such as ensuring that the size parameters passed to conc_name_and_version and conc_dir_and_name are correct and consistent. I recommend addressing these comments before merging, and users should have others review and approve this code before merging.

@pamoroso
Copy link

I'm running Maiko off this branch on Linux Mint 22.1 Cinnamon and so far haven't noticed anything unusual.

@pamoroso
Copy link

I actually did notice something unexpected. With Medley on master and Maiko on commit d822d47 of this branch the arrow keys no longer work:

commit d822d4703398fa26458faf52bc199e3713242308 (HEAD -> nhb-rewrite-version-parser-v2, origin/nhb-rewrite-version-parser-v2)
Author: Nick Briggs <[email protected]>
Date:   Mon May 19 17:25:37 2025 -0700

    Replaces unchecked strcpy/strcat with strlcpy/strlcat
    
    Unchecked strcpy/strcat can result in memory smashes if provided with
    overly long source arguments. Replacing these with the strl... functions
    protects against overruns of the destination memory.
    
    Requires changing the signature of various functions to pass destination
    storage size when destination argument is a pointer rather than something
    amenable to sizeof() operator.

Pressing the arrow keys in an Exec or TEdit does nothing and the caret doesn't move. Switching Maiko to master restores the functionality of the arrow keys.

I run VNC Medley with medley -a -e -n -v + on Linux Mint 22.1 Cinnamon.

@nbriggs
Copy link
Collaborator Author

nbriggs commented May 24, 2025

@pamoroso - this branch diverged from master before 4994982 where the mapping of the arrow keys was added to inc/XKeymap.h. I could merge that change into this branch, but this branch will merge cleanly without that, so I haven't worried about it.

nbriggs added 19 commits August 16, 2025 18:56
Replaces ConcNameAndVersion and ConcDirAndName macros with
conc_name_and_version and conc_dir_and_name functions respectively,
moves the definitions from locfile.h to dsk.c, and adds them to
dskdefs.h for use by other source files.
Moves definitions of the quote_xxx functions from ufs.c where they are not
used to dir.c where they are used.

Adds a parameter to each of the quote_xxx functions to supply the length
of the storage allocated for the result string, and change strcpy() to
strlcpy() to avoid potential memory smash if used carelessly.
Unchecked writes to strings using strcpy and strcat can cause memory smashes,
replacing them with (destination) bounds checked strl... equivalents can
avoid this.

Incidentally, fix construction of file name for $HOME/.Xdefaults
Unchecked strcpy/strcat can result in memory smashes if provided with
overly long source arguments. Replacing these with the strl... functions
protects against overruns of the destination memory.

Requires changing the signature of various functions to pass destination
storage size when destination argument is a pointer rather than something
amenable to sizeof() operator.
…, and increases clarity.

* Avoids unnecessary alarm(0) when it is about to be set again, only
cleans up after it’s all done.

* Renames DIRCHAR to LISPDIRCHAR to be more descriptive, adds LISPDIRSTR
for contexts where a string is required rather than a character.

* Avoids leaks of directory structures by ensuring that any opendir()
has a matching closedir(), with a timeout check, on the return path

* Ensures that closedir() calls that could smash errno happen after
errno is saved for Lisp’s consumption

* Simplifies code to take advantage of strlcpy()/strlcat() guarantee
that destination string is NUL terminated

* Reduces number of unnecessary copies into scratch buffer for file name
processing

* Removes some commented out code left over from copy/paste of functions
@nbriggs nbriggs force-pushed the nhb-rewrite-version-parser-v2 branch from e50a3ee to 04ad740 Compare October 16, 2025 00:28
@pamoroso
Copy link

pamoroso commented Oct 17, 2025

I updated to commit 04ad740 and rerun the tests I described in PR #523. Now TEdit works as expected:

unix-file1

But the file browser breaks with an error when processing the file TEST.TXT.~1234567890~:

unix-file2
INVALID-ARGUMENT-LIST
In \ILLEGAL.ARG:
Invalid argument: 1234567891

The backtrace:

FB-Update/8(debug)BTV
FB.CREATE.FILEBUCKET
   SI::*CLEANUP-FORMS* SI::RESETUNWIND
   PATTERN "{DSK}<home>paolo>il>*.*;*"
   INFOWANTED ((SIZE "Pages" 49 & "99999") (
CREATIONDATE "Created" 170 DATE) (AUTHOR "Author" 120
))
SI::*UNWIND-PROTECT*
   BROWSER {FILEBROWSER}#121,31614
   LISPXHIST NIL
   SI::*RESETFORMS* NIL
   RESETSTATE NIL
FB.UPDATEBROWSERITEMS
   SI::*CLEANUP-FORMS* SI::RESETUNWIND
SI::*UNWIND-PROTECT*
   BROWSER {FILEBROWSER}#121,31614
   COMMANDMENU {MENU}#130,146130
   FN FB.UPDATEBROWSERITEMS
   LISPXHIST NIL
   SI::*RESETFORMS* ((& NIL) (& #<Lock/173,111570>))
   RESETSTATE NIL
FB.STARTUP
   *FORM* (FB.STARTUP (QUOTE {FILEBROWSER}#121,31614)
 (QUOTE {MENU}#130,146130) (QUOTE 
FB.UPDATEBROWSERITEMS))
   *ARGVAL* NIL
   *TAIL* NIL
   *FN* FB.STARTUP
\EVALFORM
   %#FORM# (FB.STARTUP (QUOTE {FILEBROWSER}#121,31614
) (QUOTE {MENU}#130,146130) (QUOTE 
FB.UPDATEBROWSERITEMS))
   *CURRENT-PROCESS* #<Process FB-Update/121,32410>
   HELPFLAG BREAK!
   \CURRENTDISPLAYLINE 0
   \#DISPLAYLINES 40
   \LINEBUF.OFD #<IO Stream on T/173,152500>
   *READTABLE* #<ReadTable INTERLISP/173,151714>
   \PRIMTERMTABLE {TERMTABLEP}#173,144740
   \PRIMTERMSA {CHARTABLE}#173,145000
   TtyDisplayStream 
#<Output Display Stream/126,165400>
   SI::*RESETFORMS* NIL
   \INTERRUPTABLE T
   \TTYWINDOW NIL
   READBUF NIL
   \TERM.OFD #<Output Stream on T/167,61600>
   *STANDARD-OUTPUT* #<Output Stream on T/167,61600>
   *STANDARD-INPUT* #<IO Stream on T/173,152500>
\MAKE.PROCESS0
T

@nbriggs
Copy link
Collaborator Author

nbriggs commented Oct 17, 2025

Yeah, it turns out that the file browser can’t cope with versions over 65535 - it uses a WORD (16 bits) in the data structure to store it.

  * implements these as functions rather than #define macros
  * moves definitions to ufs.c
  * adds parameter to UnixVersionToLispVersion for the storage
    size of the buffer being passed
  * removes inc/lispver2.h
  * adjusts makefile-dos an makefile-tail to update dependencies
@nbriggs
Copy link
Collaborator Author

nbriggs commented Oct 20, 2025

/gemini review

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request is a great step towards improving the robustness and security of the codebase by replacing macros with safer inline functions and updating many string operations to their safer, size-aware counterparts like strlcpy and snprintf. The core changes are well-implemented. I've found one critical issue and a couple of medium-severity issues related to the changes. One is a typo that would likely cause a crash, another is an inconsistency in string handling, and the last is a missing alarm cancellation after a loop. With these fixes, the PR will be in excellent shape.

src/dir.c Outdated
Comment on lines 1327 to 1330
len = strlen(namebuf);
*(nextp->lname + len) = DIRCHAR;
*(nextp->lname + len) = LISPDIRCHAR;
*(nextp->lname + len + 1) = '\0';
nextp->lname_len = len + 1;

Choose a reason for hiding this comment

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

medium

This block of code for appending the directory character is inconsistent with the safer string manipulation functions used elsewhere in this PR. It's also potentially buggy: len is calculated from namebuf, but if strlcpy on the previous line truncated the string when copying to nextp->lname, len would be incorrect for nextp->lname. It would be safer and more consistent to use strlcat with LISPDIRSTR here, similar to how it's done in the non-DOS part of this function.

      strlcat(nextp->lname, LISPDIRSTR, sizeof(nextp->lname));
      nextp->lname_len = strlen(nextp->lname);

@nbriggs
Copy link
Collaborator Author

nbriggs commented Oct 20, 2025

/gemini review

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request is a great step towards modernizing the codebase by replacing macros with functions and using safer string manipulation functions like strlcpy, strlcat, and snprintf. These changes significantly improve code safety and maintainability. However, I've identified a few critical security vulnerabilities in the newly introduced static functions in dir.c. These functions use fixed-size local buffers without proper size checks, which could lead to buffer overflows. I've provided detailed comments and suggestions for fixing these issues. I also have a suggestion to simplify one of the new functions for better readability.

@nbriggs
Copy link
Collaborator Author

nbriggs commented Oct 20, 2025

/gemini review

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request is an excellent refactoring effort that significantly improves code safety and maintainability. The replacement of ConcNameAndVersion and ConcDirAndName macros with functions is a great step. I particularly appreciate the systematic replacement of unsafe string functions like strcpy, strcat, and sprintf with their safer, size-aware counterparts (strlcpy, strlcat, snprintf). Updating function signatures to include buffer sizes further enhances robustness against buffer overflows. The codebase is cleaner and more modern as a result. I have a couple of minor suggestions to improve clarity and prevent future issues.

@nbriggs
Copy link
Collaborator Author

nbriggs commented Oct 21, 2025

/gemini review

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request is a great improvement. It replaces several macros with safer function-based implementations, notably ConcNameAndVersion and ConcDirAndName. The widespread replacement of unsafe string functions like strcpy, strcat, and sprintf with their safer counterparts (strlcpy, strlcat, snprintf) significantly enhances the code's robustness and security. The changes to function signatures to include buffer sizes are also a welcome addition for preventing buffer overflows. I have one suggestion for improving code readability by refactoring a function to remove goto statements.

@nbriggs nbriggs dismissed gemini-code-assist[bot]’s stale review October 21, 2025 00:13

All necessary changes have been made.

@nbriggs nbriggs requested a review from pamoroso October 21, 2025 00:18
@pamoroso
Copy link

I updated to commit 60e12da and now there's an apparent regression as TEdit opens an empty stream when passed the name of the existing ASCII file TEST.TEDIT.~1234567890~.

unix-file

@nbriggs
Copy link
Collaborator Author

nbriggs commented Oct 21, 2025

That's odd -- notice that the TEDIT window title is not the file you asked it to edit. When I do the same thing I get different results. I'd suggest doing a loadup with the newly merged master branch and the latest version of maiko - don't forget to make both the "init" and "x" parts.
Screenshot 2025-10-21 at 08 59 43

@pamoroso
Copy link

I switched to and rebuilt nhb-rewrite-version-parser-v2/60e12da for Maiko and master/82fc95c for Medley and now I get the expected results:

unix-file

In my earlier tests I was on one of Ron's Medley branches.

@pamoroso
Copy link

I updated to commit 3d09555, still looking good.

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.

2 participants