Skip to content

Commit a9ceb8d

Browse files
committed
fix bug when overflow of the fp ptr occurs
1 parent 9dabbec commit a9ceb8d

File tree

1 file changed

+44
-42
lines changed

1 file changed

+44
-42
lines changed

src/functions/files/XOPEN.asm

Lines changed: 44 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
.proc XOPEN_ROUTINE
22

3+
; $ed31
34
.out .sprintf("|MODIFY:RES:XOPEN_ROUTINE")
45
.out .sprintf("|MODIFY:RESB:XOPEN_ROUTINE")
56
.out .sprintf("|MODIFY:TR5:XOPEN_ROUTINE")
6-
.out .sprintf("|MODIFY:TR7:XOPEN_ROUTINE")
7+
.out .sprintf("|MODIFY:TR6:XOPEN_ROUTINE (used by XMALLOC)")
8+
.out .sprintf("|MODIFY:TR7:XOPEN_ROUTINE (used by XMALLOC)")
79
.out .sprintf("|MODIFY:XOPEN_SAVE:XOPEN_ROUTINE")
810
.out .sprintf("|MODIFY:XOPEN_FLAGS:XOPEN_ROUTINE")
911
.out .sprintf("|MODIFY:XOPEN_RES_SAVE:XOPEN_ROUTINE")
@@ -57,7 +59,7 @@
5759
; O_CREAT | Yes | open and return FD
5860

5961
sta RES
60-
stx RES+1
62+
stx RES + 1
6163
; Save ptr
6264
sta XOPEN_RES_SAVE
6365
stx XOPEN_RES_SAVE + 1
@@ -67,21 +69,14 @@
6769

6870
;
6971
; Close current file if we already open a file
70-
lda kernel_process+kernel_process_struct::kernel_fd_opened ; if there is already a file open on ch376 if value <> $FF, if it's equal to $ff, there is no file opened
72+
lda kernel_process + kernel_process_struct::kernel_fd_opened ; if there is already a file open on ch376 if value <> $FF, if it's equal to $ff, there is no file opened
7173
cmp #$FF
7274
bne @open_new_file
7375

7476
; close it
7577
jsr _ch376_file_close
7678

7779
@open_new_file:
78-
.ifdef WITH_DEBUG2
79-
jsr kdebug_save
80-
ldy XOPEN_RES_SAVE + 1
81-
ldx #XDEBUG_XOPEN_ENTER
82-
jsr xdebug_print_with_ay_string
83-
jsr kdebug_restore
84-
.endif
8580

8681
lda #EOK
8782
sta KERNEL_ERRNO
@@ -97,9 +92,8 @@
9792
ldx #$FF
9893
txa
9994
rts
100-
@L1:
101-
10295

96+
@L1:
10397
ldy #$00
10498
lda (RES),y
10599
;
@@ -126,24 +120,25 @@
126120
rts
127121

128122
@not_null_2:
123+
129124
sta KERNEL_XOPEN_PTR1
130-
sty KERNEL_XOPEN_PTR1+1
125+
sty KERNEL_XOPEN_PTR1 + 1
131126
; now concat
132127
; reach the end of string in the pointer
133128
ldy #_KERNEL_FILE::f_path
134129
@L3:
135130
lda (KERNEL_XOPEN_PTR1),y
136131
beq @end_of_string_found
137132
iny
138-
cpy #KERNEL_MAX_PATH_LENGTH+_KERNEL_FILE::f_path
133+
cpy #KERNEL_MAX_PATH_LENGTH + _KERNEL_FILE::f_path
139134
bne @L3
140135

141136
; at this step, we cannot detect the end of string : BOF, return null
142137
jmp @exit_open_with_null
143138

144139
@end_of_string_found:
145140
; This solution avoid to compute pointer and to create another zp address
146-
cpy #_KERNEL_FILE::f_path+$01 ; is it slash "/",0 ?
141+
cpy #_KERNEL_FILE::f_path + $01 ; is it slash "/",0 ?
147142
beq @don_t_add_slash ; yes
148143
; it's a relative path and we are still in a folder (except /)
149144
; add slash then
@@ -172,7 +167,7 @@
172167
; Be careful BOF can occurs if
173168
iny
174169
sty RES
175-
cpy #KERNEL_MAX_PATH_LENGTH+_KERNEL_FILE::f_path
170+
cpy #KERNEL_MAX_PATH_LENGTH + _KERNEL_FILE::f_path
176171

177172
bne @L4
178173
; Bof return NULL
@@ -189,7 +184,7 @@
189184
; Pass arg to createfile_pointer
190185

191186
lda RES
192-
ldy RES+1
187+
ldy RES + 1
193188
; and XOPEN_FLAGS too at this step
194189

195190
jsr _create_file_pointer
@@ -203,7 +198,7 @@
203198

204199
@not_null_1:
205200
sta KERNEL_XOPEN_PTR1
206-
sty KERNEL_XOPEN_PTR1+1
201+
sty KERNEL_XOPEN_PTR1 + 1
207202

208203
@open_from_device:
209204
; Reset flag to say that end of string is reached
@@ -218,7 +213,7 @@
218213

219214
jsr send_0_to_ch376_and_open
220215

221-
ldy #_KERNEL_FILE::f_path+1 ; skip /
216+
ldy #_KERNEL_FILE::f_path + 1 ; skip /
222217

223218
@next_filename:
224219
lda #CH376_SET_FILE_NAME ;$2F
@@ -256,7 +251,7 @@
256251
iny
257252
lda (KERNEL_XOPEN_PTR1),y
258253
bne @next_filename
259-
cpy #_KERNEL_FILE::f_path+1
254+
cpy #_KERNEL_FILE::f_path + 1
260255
beq @open_and_register_fp
261256

262257
bne @next_filename
@@ -282,9 +277,15 @@
282277
beq @exit_open_with_null ; yes, return NULL
283278

284279

280+
; save KERNEL_XOPEN_PTR1 (modifued by open_full_filename)
281+
lda KERNEL_XOPEN_PTR1
282+
sta TR6
283+
lda KERNEL_XOPEN_PTR1 + 1
284+
sta TR7
285+
285286
; Le fichier n'a pas été trouvé,
286287
; On va vérifier qu'on avait un fichier ouvert avant
287-
lda kernel_process+kernel_process_struct::kernel_fd_opened ; if there is already a file open on ch376 if value <> $FF, if it's equal to $ff, there is no file opened
288+
lda kernel_process + kernel_process_struct::kernel_fd_opened ; if there is already a file open on ch376 if value <> $FF, if it's equal to $ff, there is no file opened
288289
cmp #$FF
289290
beq @exit_open_with_null
290291

@@ -293,18 +294,14 @@
293294
jsr open_full_filename
294295

295296
@exit_open_with_null:
296-
lda KERNEL_XOPEN_PTR1
297-
ldy KERNEL_XOPEN_PTR1+1
297+
298+
lda TR6 ; KERNEL_XOPEN_PTR1 restored
299+
ldy TR7 ; KERNEL_XOPEN_PTR1 restored
298300
jsr XFREE_ROUTINE
299301
; No such file_or_directy
300302
lda #ENOENT
301303
sta KERNEL_ERRNO
302304

303-
.ifdef WITH_DEBUG2
304-
ldx #XDEBUG_XOPEN_FILE_NOT_FOUND
305-
lda #$FF
306-
jsr xdebug_print_with_a
307-
.endif
308305

309306
lda #$FF
310307
tax
@@ -319,16 +316,16 @@
319316
@open_and_register_fp:
320317
; Register fp in process struct
321318
; store pointer in process struct
322-
ldx kernel_process+kernel_process_struct::kernel_current_process ; Get current process
319+
ldx kernel_process + kernel_process_struct::kernel_current_process ; Get current process
323320
jsr kernel_get_struct_process_ptr
324321
sta RES
325-
sty RES+1
322+
sty RES + 1
326323

327324
; Fill the address of the fp
328325
; Manage only 1 FP for instance FIXME bug
329326
ldx #$00
330-
ldy #(kernel_one_process_struct::fp_ptr+1)
331-
327+
ldy #(kernel_one_process_struct::fp_ptr + 1)
328+
; $ee53
332329
@try_to_find_a_free_fp_for_current_process:
333330
lda (RES),y ; Load high
334331
beq @fp_is_not_busy ; If it's equal to $00, it means that it's empty because it's impossible to have a fp registered in zp
@@ -339,14 +336,15 @@
339336
inx
340337
cpx #KERNEL_MAX_FP_PER_PROCESS
341338
bne @try_to_find_a_free_fp_for_current_process
339+
; At this step, we did not have any free fp for the current process
342340

343341
lda #KERNEL_ERRNO_REACH_MAX_FP_FOR_A_PROCESS
344342
sta KERNEL_ERRNO
345343

346-
beq @exit_open_with_null
344+
jmp @exit_open_with_null
347345
;
348346
@fp_is_not_busy:
349-
lda KERNEL_XOPEN_PTR1+1
347+
lda KERNEL_XOPEN_PTR1 + 1
350348
sta (RES),y
351349
dey
352350
lda KERNEL_XOPEN_PTR1
@@ -357,15 +355,15 @@
357355
ldx #$00
358356

359357
@init_fp:
360-
lda kernel_process+kernel_process_struct::kernel_fd,x
358+
lda kernel_process + kernel_process_struct::kernel_fd,x
361359
beq @found_fp_slot
362360
inx
363361
cpx #KERNEL_MAX_FP
364362
bne @init_fp
365363

366364
; No available fd
367365
lda KERNEL_XOPEN_PTR1
368-
ldy KERNEL_XOPEN_PTR1+1
366+
ldy KERNEL_XOPEN_PTR1 + 1
369367
jsr XFREE_ROUTINE
370368

371369
lda #EMFILE
@@ -377,21 +375,25 @@
377375

378376
; not found
379377
@found_fp_slot:
380-
lda kernel_process+kernel_process_struct::kernel_current_process ; Get the current process
381-
sta kernel_process+kernel_process_struct::kernel_fd,x ; and store in fd slot the id of the process
378+
lda kernel_process + kernel_process_struct::kernel_current_process ; Get the current process
379+
sta kernel_process + kernel_process_struct::kernel_fd,x ; and store in fd slot the id of the process
382380
txa
383381
pha ; save Id of the fd
384382
asl
385383
tax
386384

387385
; Store fp in main process
386+
387+
388+
388389
lda KERNEL_XOPEN_PTR1
389-
sta kernel_process+kernel_process_struct::fp_ptr,x
390+
sta kernel_process + kernel_process_struct::fp_ptr,x
390391
inx
391-
lda KERNEL_XOPEN_PTR1+1
392-
sta kernel_process+kernel_process_struct::fp_ptr,x
392+
lda KERNEL_XOPEN_PTR1 + 1
393+
sta kernel_process + kernel_process_struct::fp_ptr,x
393394
pla ; restore Id of the fd
394-
sta kernel_process+kernel_process_struct::kernel_fd_opened ; Define that it's the new current fd
395+
sta kernel_process + kernel_process_struct::kernel_fd_opened ; Define that it's the new current fd
396+
395397
clc
396398
adc #KERNEL_FIRST_FD
397399

0 commit comments

Comments
 (0)