summaryrefslogtreecommitdiff
path: root/user/tlecarrour/porting_guide_for_dummies.mdwn
blob: cdab35084e3b1e2a996c7a2554114cc19c8ae426 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
[[!meta copyright="Copyright © 2012 Free Software Foundation, Inc."]]

[[!meta license="""[[!toggle id="license" text="GFDL 1.2+"]][[!toggleable
id="license" text="Permission is granted to copy, distribute and/or modify this
document under the terms of the GNU Free Documentation License, Version 1.2 or
any later version published by the Free Software Foundation; with no Invariant
Sections, no Front-Cover Texts, and no Back-Cover Texts.  A copy of the license
is included in the section entitled [[GNU Free Documentation
License|/fdl]]."]]"""]]


Porting Guide for Dummies
=========================

The problems addressed here were encountered while working 
on fixing **PATH_MAX** and **MAXPATHLEN**.

[[!toc startlevel=2 levels=3]]


* * *


Test on Hurd
------------


### Installing the required files

As `apt-get source` will download and extract many files, you may want to create a dedicated folder for the package and work from there.

    mkdir PACKAGE
    cd PACKAGE
    sudo apt-get build-dep PACKAGE
    apt-get source PACKAGE


### Trying to build the package

    cd PACKAGE_SOURCE
    dpkg-buildpackage -us -uc -rfakeroot -tc


### Test a quick fix

In all the files that use **PATH_MAX**, include those lines at the beginning.

    #ifndef PATH_MAX
    #define PATH_MAX 4196
    #endif

Try to rebuild the package and see if it's solved the problem.  
If yes, you can start working on the package.


* * *


Basic things
------------

### Maintaining a original version

    mkdir old
    cp -r PACKAGE_SOURCE old/


### Coding style

Follow the conventions used in the source code!

    if (condition) {
        do_smthg();
    }

is not the same as:

    if (condition) 
    {
        do_smthg();
    }

and is not the same as:

    if (condition)
        do_smthg();

Pay attention to spaces surrounding, or not, arithmetic signs and symbols:

    a = do_smthg( b + c );
    a = do_smthg(b+c);


### Indentation

By default use 8 spaces as the size for 1 tab. 
Then figure out if the code uses tab + 1/2 tab:

    ....if (condition) {
    ------->do_smthg();
    ....}

or tab only:

    ------->if (condition) {
    ------->------->do_smthg();
    ------->}


### Creating a patch

    diff -Naur old/PACKAGE-VERSION PACKAGE-VERSION > fix_FTBFS4Hurd.patch


* * *


Known problems
--------------

### Dynamically allocated buffer returned by a function

Use a static buffer

### Buffer used to format an expression containing an INTEGER

The length of an INTEGER in a string can be up to sizeof (int) * 3 + 1.

> The usual trick for "%d" is to use the constant 'sizeof (int) * 3 + 1'. 
I included + 1 for the sign, but it's not really necessary 
if we exepect sizeof(int) >= 2, which we probably should. 
**Jérémie Koenig**

    log(MAX_INT)
     = log(2 ^ 32)
     = 32 * log(2)
     = 4 * 8 * log(2)
     = sizeof(int) * 2.40823997
     < sizeof(int) * 3


### Proper use of realloc()

use a new_buff to check if everything went fine Free buf if realloc failed (and prog doesn't exit)


### Reading lines from file

Function to read line (no size limit, ending with "\n") from a file.

    static char *get_line(FILE *f)
    {
        char *buff  = NULL;
        char *new_buff  = NULL;
        size_t buff_size = 0;
        size_t last = 0;
    
        while (!feof(f)) {
            buff_size = buff_size ? buff_size * 2 : BUFSIZ;
            new_buff = realloc(buff, buff_size);
            if (new_buff == NULL) {
                free(buff);
                return NULL;
            }
            buff = new_buff;
            if (fgets(buff + last, buff_size - last, f) == NULL) {
                free(buff);
                return NULL;
            }
            last = strlen(buff);
            if (buff[last - 1] == '\n')
                return buff;
        }
        return buff;
    }


### Proper use of readlink()

One has to rely on lstat() to get the size of the link that readlink() returns.

Declare what you need:

    char *linkname = NULL;
    struct stat sb;
    ssize_t len = -1;

Call lstat() and check return value:

    if (lstat(filename, &sb) == -1) {

Create a buffer of the appropriate size and check the return value:

    linkname = malloc(sb.st_size + 1);
    if (linkname == NULL) {

Call readlink(), check return value and set the null char in the linkname:

    len = readlink(filename, linkname, sb.st_size + 1);
    if (len < 0 || len > sb.st_size) {
    ...
    linkname[sb.st_size] = '\0';


### Alternative use of readlink(): readlink_malloc()

In some cases the above approch doesn't work.for instance when reading from 
**/proc/*/exe** on Linux. In this case you can try the following function.  
The code comes from [[https://buildsecurityin.us-cert.gov/bsi/articles/knowledge/coding/806-BSI.html]]

    static char *readlink_malloc(const char *filename)
    {
        int size = 100;
    
        while (1) {
            char *buff = malloc(size);
            if (buff == NULL)
                return NULL;
            int nchars = readlink(filename, buff, size);
            if (nchars < 0) {
                free(buff);
                return NULL;
            }
            if (nchars < size) {
                buff[nchars] = '\0';
                return buff;
            }
            free (buff);
            size *= 2;
        }
    }