Skip to content

Commit 11ed748

Browse files
committed
fix(stablediffusion-ggml): NULL terminate options array to prevent segfault
Signed-off-by: Richard Palethorpe <[email protected]>
1 parent 3273afc commit 11ed748

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

backend/go/stablediffusion-ggml/gosd.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ int load_model(char *model, char* options[], int threads, int diff) {
7070
char *scheduler = "";
7171
char *sampler = "";
7272

73+
fprintf(stderr, "parsing options\n");
74+
7375
// If options is not NULL, parse options
7476
for (int i = 0; options[i] != NULL; i++) {
7577
char *optname = strtok(options[i], ":");
@@ -98,10 +100,13 @@ int load_model(char *model, char* options[], int threads, int diff) {
98100
}
99101
}
100102

103+
fprintf(stderr, "parsed options\n");
104+
101105
int sample_method_found = -1;
102106
for (int m = 0; m < SAMPLE_METHOD_COUNT; m++) {
103107
if (!strcmp(sampler, sample_method_str[m])) {
104108
sample_method_found = m;
109+
fprintf(stderr, "Found sampler: %s\n", sampler);
105110
}
106111
}
107112
if (sample_method_found == -1) {

backend/go/stablediffusion-ggml/gosd.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ func (sd *SDGGML) Load(opts *pb.ModelOptions) error {
3737

3838
size := C.size_t(unsafe.Sizeof((*C.char)(nil)))
3939
length := C.size_t(len(opts.Options))
40-
options = (**C.char)(C.malloc(length * size))
41-
view := (*[1 << 30]*C.char)(unsafe.Pointer(options))[0:len(opts.Options):len(opts.Options)]
40+
options = (**C.char)(C.malloc((length + 1) * size))
41+
view := (*[1 << 30]*C.char)(unsafe.Pointer(options))[0:len(opts.Options) + 1:len(opts.Options) + 1]
4242

4343
var diffusionModel int
4444

@@ -66,6 +66,7 @@ func (sd *SDGGML) Load(opts *pb.ModelOptions) error {
6666
for i, x := range oo {
6767
view[i] = C.CString(x)
6868
}
69+
view[len(oo)] = nil
6970

7071
sd.cfgScale = opts.CFGScale
7172

0 commit comments

Comments
 (0)