Skip to content

Commit 8c4a709

Browse files
LiquidFenrirWinterMuteDacoTaco
authored
console: fix writing out of bounds and writing unused memory (#229)
* consolidate console documentation and offset usage introduces helper functions to avoid the repetition and mistakes logic was verified equivalent or better by hand but not runtime-tested fixes various issues having to do with out of bounds writes misc changes to extraneous whitespace noticed during the refactor * remove somehow broken character * forgot a word * remove false doc (mindless copy-paste), extra punctuation * reword comment for readability * remove spurious commas * remove more spurious commas * ensure con_xres,con_yres multiples of font size avoids an unwritten right/bottom edge that's not part of a complete tile this caused graphical issues and inconsistencies between Init and InitEx document in the function doxygen comment make values in __console_vipostcb only computed once ptr directly from the global equal to destbuffer, less indirection * make new functions inline, making them faster in the callback --------- Co-authored-by: Dave Murphy <[email protected]> Co-authored-by: DacoTaco <[email protected]>
1 parent f5ea85d commit 8c4a709

File tree

2 files changed

+169
-100
lines changed

2 files changed

+169
-100
lines changed

gc/ogc/console.h

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@
5555
* \brief Initializes the console subsystem with given parameters
5656
*
5757
* \param[in] framebuffer pointer to the framebuffer used for drawing the characters
58-
* \param[in] xstart,ystart start position of the console output in pixel
59-
* \param[in] xres,yres size of the console in pixel
58+
* \param[in] xstart,ystart start position of the console output in pixels
59+
* \param[in] xres,yres size of the console in pixels. Truncated to a multiple of font size if not already one
6060
* \param[in] stride size of one line of the framebuffer in bytes
6161
*
6262
* \return none
@@ -69,8 +69,8 @@ void CON_Init(void *framebuffer,int xstart,int ystart,int xres,int yres,int stri
6969
* \param[in] rmode pointer to the video/render mode configuration
7070
* \param[in] conXOrigin starting pixel in X direction of the console output on the external framebuffer
7171
* \param[in] conYOrigin starting pixel in Y direction of the console output on the external framebuffer
72-
* \param[in] conWidth width of the console output 'window' to be drawn
73-
* \param[in] conHeight height of the console output 'window' to be drawn
72+
* \param[in] conWidth width of the console output 'window' to be drawn in pixels. Truncated to a multiple of font width if not already one
73+
* \param[in] conHeight height of the console output 'window' to be drawn in pixels. Truncated to a multiple of font height if not already one
7474
*
7575
* \return 0 on success, <0 on error
7676
*/
@@ -80,7 +80,7 @@ s32 CON_InitEx(GXRModeObj *rmode, s32 conXOrigin,s32 conYOrigin,s32 conWidth,s32
8080
* \fn CON_GetMetrics(int *cols, int *rows)
8181
* \brief retrieve the columns and rows of the current console
8282
*
83-
* \param[out] cols,rows number of columns and rows of the current console
83+
* \param[out] cols,rows number of columns and rows of the current console in tiles
8484
*
8585
* \return none
8686
*/
@@ -90,7 +90,7 @@ void CON_GetMetrics(int *cols, int *rows);
9090
* \fn CON_GetPosition(int *col, int *row)
9191
* \brief retrieve the current cursor position of the current console
9292
*
93-
* \param[out] col,row current cursor position
93+
* \param[out] col,row current cursor position in tiles, 1-indexed
9494
*
9595
* \return none
9696
*/
@@ -100,7 +100,7 @@ void CON_GetPosition(int *cols, int *rows);
100100
* \fn CON_EnableGecko(int channel, int safe)
101101
* \brief Enable or disable the USB gecko console.
102102
*
103-
* \param[in] channel EXI channel, or -1 ¨to disable the gecko console
103+
* \param[in] channel EXI channel, or -1 to disable the gecko console
104104
* \param[in] safe If true, use safe mode (wait for peer)
105105
*
106106
* \return none
@@ -152,22 +152,24 @@ typedef struct PrintConsole
152152
ConsoleFont font; ///< Font of the console
153153

154154
void *destbuffer; ///< Framebuffer address
155-
int con_xres,con_yres,con_stride;
156-
int target_x,target_y, tgt_stride;
155+
int con_xres, con_yres; ///< Console buffer width/height in pixels
156+
int con_stride; ///< Size of one row in the console buffer in bytes
157+
int target_x, target_y; ///< Target buffer x/y offset to start the console in pixels
158+
int tgt_stride; ///< Size of one row in the target buffer in bytes
157159

158-
int cursorX; ///< Current X location of the cursor (as a tile offset by default)
159-
int cursorY; ///< Current Y location of the cursor (as a tile offset by default)
160+
int cursorX; ///< Current X location of the cursor in the window in tiles, 1-indexed: 1 <= cursorX <= windowWidth, cursorX > windowWidth wraps to the next line and resets to cursorX = 1
161+
int cursorY; ///< Current Y location of the cursor in the window in tiles, 1-indexed
160162

161163
int prevCursorX; ///< Internal state
162164
int prevCursorY; ///< Internal state
163165

164-
int con_cols; ///< Width of the console hardware layer in characters
165-
int con_rows; ///< Height of the console hardware layer in characters
166+
int con_cols; ///< Width of the console hardware layer in characters (aka tiles)
167+
int con_rows; ///< Height of the console hardware layer in characters (aka tiles)
166168

167-
int windowX; ///< Window X location in characters (not implemented)
168-
int windowY; ///< Window Y location in characters (not implemented)
169-
int windowWidth; ///< Window width in characters (not implemented)
170-
int windowHeight; ///< Window height in characters (not implemented)
169+
int windowX; ///< Window X location in tiles, 1-indexed, 1 <= windowX < con_cols
170+
int windowY; ///< Window Y location in tiles, 1-indexed
171+
int windowWidth; ///< Window width in tiles, 1 <= windowWidth <= con_cols
172+
int windowHeight; ///< Window height in tiles
171173

172174
int tabSize; ///< Size of a tab
173175
unsigned int fg; ///< Foreground color
@@ -196,10 +198,10 @@ void consoleSetFont(PrintConsole* console, ConsoleFont* font);
196198
/**
197199
* @brief Sets the print window.
198200
* @param console Console to set, if NULL it will set the current console window.
199-
* @param x X location of the window.
200-
* @param y Y location of the window.
201-
* @param width Width of the window.
202-
* @param height Height of the window.
201+
* @param x X location of the window in tiles, 1-indexed
202+
* @param y Y location of the window in tiles, 1-indexed
203+
* @param width Width of the window in tiles
204+
* @param height Height of the window in tiles
203205
*/
204206
void consoleSetWindow(PrintConsole* console, unsigned int x, unsigned int y, unsigned int width, unsigned int height);
205207

0 commit comments

Comments
 (0)